Это сложно объяснить, но я сделаю все возможное:
Я строю систему, где пользователь может проходить курсы.Курсы состоят из шагов, которые должны быть выполнены по порядку.В системе существует 6 типов шагов (загрузка, презентация, видео, текст, викторина и опрос)
Способ доступа пользователя к STEP в настоящее время:
http://example.com/courses/2/course_steps/1
Как вы можете сказать, курсовые шаги вложены в курсы.
Ниже приведен метод show в шагах курса:
def show
render "show_#{@course_step.step.step_type.name.downcase}"
end
Как вы можете сказать, он в основном выбирает представление для show_ [TYPE] (викторина, опрос, текст ... и т. д.)
Это прекрасно работает для простых шагов, таких как текст, видео или загрузка, но для сложных шагов, таких как викторина, эта модель не работает дляследующие причины:
- Как проверить форму для опроса или опроса, как если бы я использовал другой контроллер (QuizAttemptsController).
- Кажется, что участник REST разбит каквикторина, опрос .. и т. д. должны рассматриваться отдельно.(Я знаю, что это типы шагов, но они могут иметь свои собственные действия и проверки)
Модель шага
class Step < ActiveRecord::Base
belongs_to :step_type
belongs_to :client
has_one :step_quiz, :dependent => :destroy
has_one :step_survey, :dependent => :destroy
has_one :step_text, :dependent => :destroy
has_one :step_download, :dependent => :destroy
has_one :step_video, :dependent => :destroy
has_one :step_presentation, :dependent => :destroy
has_many :course_steps, :dependent => :destroy
has_many :courses, :through => :course_steps
has_many :patient_course_steps, :dependent => :destroy
attr_accessible :step_type_id, :client_id, :title, :subtitle, :summary
validates :title, :presence=>true
validates :summary, :presence=>true
def getSpecificStepObject()
case self.step_type.name.downcase
when "text"
return StepText.find_by_step_id(self.id)
when "quiz"
return StepQuiz.find_by_step_id(self.id)
when "survey"
return StepSurvey.find_by_step_id(self.id)
when "download"
return StepDownload.find_by_step_id(self.id)
when "video"
return StepVideo.find_by_step_id(self.id)
when "presentation"
return StepPresentation.find_by_step_id(self.id)
end
end
end
Модель викторины шага:
class StepQuiz < ActiveRecord::Base
belongs_to :step, :dependent => :destroy
has_many :step_quiz_questions, :dependent => :destroy
has_many :quiz_attempts, :dependent => :destroy
accepts_nested_attributes_for :step
accepts_nested_attributes_for :step_quiz_questions, :allow_destroy => true
attr_accessible :step_id, :instructions, :step_attributes, :step_quiz_questions_attributes
validates :instructions, :presence=>true
end
Модель CourseStep
class CourseStep < ActiveRecord::Base
belongs_to :step
belongs_to :course
validates_uniqueness_of :step_id, :scope => :course_id
def next_step()
Course.find(self.course.id).course_steps.order(:position).where("position >= ?", self.position).limit(1).offset(1).first
end
def previous_step()
Course.find(self.course.id).course_steps.order("position DESC").where("position <= ?", self.position).limit(1).offset(1).first
end
end
Как бы вы предложили это исправить?