2

我正在尝试构建一个多样化的锦标赛管理系统,该系统允许多种类型的锦标赛支架后端。目标是为锦标赛括号后端(此处为PlasTournament)提供一个非常简单的选项,并提供多个更复杂的后端(现在,只是Challonge::Tournament来自Challonge,通过其API使用challonge-api gem )。

我的模型:

class Tournament < ActiveRecord::Base
  belongs_to :event 
  validates :event, :presence => true
  has_one :remote, :as => :tournament_bracket, :dependent => :destroy
end

class PlasTournament < ActiveRecord::Base
  belongs_to :winner, :class_name => "User"
  belongs_to :creator, :class_name => "User"
  belongs_to :tournament_bracket, :polymorphic => true
  has_and_belongs_to_many :participants, :class_name => "User"
end

还有Challonge::Tournament,但我不知道如何让它适合这种风格。我需要猴子补丁吗?它是一个 ActiveResource 类,我真的只需要在类的多态关联中存储Tournament它的类和 id。

我以前有这些模型:

class Tournament < ActiveRecord::Base
  belongs_to :event 
  validates :event, :presence => true
  has_one :remote_tournament, :as => :tournament_bracket, :dependent => :destroy
end

class RemoteTournament < ActiveRecord::Base
  belongs_to :tournament_bracket, :polymorphic => true

  def tournament_bracket_type_type=(sType)
     super(sType.to_s.classify.constantize.base_class.to_s)
  end
end

class PlasTournament < RemoteTournament
  belongs_to :winner, :class_name => "User"
  belongs_to :creator, :class_name => "User"
  belongs_to :tournament_bracket, :polymorphic => true
  has_and_belongs_to_many :participants, :class_name => "User"
end

或类似的东西。它没有用,我没有像上面那样提交它。

理想情况下,我希望能够在我的控制器的create方法中做这样的事情(我知道其中一些可以通过传递params[:tournament]给#new来处理;我只是在这个例子中明确表示):

@tournament = Tournament.new
@tournament.event = params[:tournament][:event_id]
@tournament.name = params[:tournament][:name]
@remote = params[:tournament][:remote_type].constantize.new
#set its state
@tournament.remote = @remote
@tournament.save!

然后在Tournament#show做这样的事情:

@tournament = Tournament.find params[:id]
#the only things I need from Tournament are the 
#name, description, max_participants, and remote
@remote = @tournament.remote
#^this is primarily for shorthand access

然后,在视图中,我可以根据remote.

case remote.class
when PlasTournament
  render :partial => 'brackets/plas'
when Challonge::Tournament
  render :partial => 'brackets/challonge'
end

我是不是摇摆不定?这看起来相当简单,但我认为我陷入了 Rails 的实现细节中。

我正在寻找的本质上是一种将多态锦标赛包围系统隐藏在锦标赛类后面的方法。

4

0 回答 0