Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##base_answer.rb
- class BaseAnswer < ActiveRecord::Base
- belongs_to :user
- belongs_to :survey_question
- belongs_to :answer, :polymorphic => true, :dependent => :destroy
- end
- ##answer.rb
- class Answer < ActiveRecord::Base
- self.abstract_class = true
- has_one :base_answer, :as => :answer
- after_save :save_base_answer
- delegate :survey_question, :to => :base_page_proxy
- def base_answer_proxy
- if base_answer
- base_answer
- else
- @base_answer_proxy ||= BaseAnswer.new
- end
- end
- protected
- def save_base_answer
- base_answer_proxy.answer = self
- base_answer_proxy.save
- base_answer(true)
- end
- end
- ##user.rb
- class User < ActiveRecord::Base
- acts_as_authentic
- has_many :sites
- has_many :surveys, :through => :sites
- has_many :base_answers
- #named_scope :gender, lambda { |g| { :conditions => {:demographics => {:gender => g}} , :include => :demographic } }
- GENDER = {:Male => 'f', :Female => 'f'}
- end
- ##rating_answer.rb
- class RatingAnswer < Answer
- end
- ##essay_answer.rb
- class EssayAnswer < Answer
- end
- ##survey_question.rb
- class SurveyQuestion < ActiveRecord::Base
- belongs_to :survey
- belongs_to :question
- acts_as_list :scope => :survey
- has_many :base_answers
- #not checking survey_id due to circular dependencies when creating it
- validates_presence_of :question_id#, :survey_id
- end
- ##question.rb
- class Question < ActiveRecord::Base
- belongs_to :category
- has_many :survey_questions
- validates_presence_of :text, :category_id
- def new_answer
- type.to_s.gsub("Question", "Answer").constantize.new
- end
- def kind
- type.to_s.gsub(type.superclass.to_s,"")
- end
- def self.kinds_of
- #.Object.subclasses_of(Question).collect do |klass|
- # [klass.to_s.gsub("Question", ""), klass.to_s]
- #end
- [["Essay", "EssayQuestion"], ["Rating", "RatingQuestion"]]
- end
- end
- ##base_answer schema
- class CreateBaseAnswers < ActiveRecord::Migration
- def self.up
- create_table :base_answers do |t|
- t.integer :user_id
- t.integer :survey_question_id
- t.integer :answer_id
- t.string :answer_type
- t.timestamps
- end
- end
- def self.down
- drop_table :base_answers
- end
- end
- ##answers_controller.rb
- class AnswersController < ApplicationController
- before_filter :get_survey_question, :only => [:new, :create, :edit, :update]
- before_filter :new_answer, :only => [:new, :create]
- def new
- @frame_url = @survey_question.url.blank? ? @survey_question.survey.url : @survey_question.url
- #Send to the edit page if the user already answered the question
- #if @user.answered_question?(@question)
- # redirect_to edit_panel_question_answer_path(@panel, @question)
- #end
- end
- def create
- @answer.build_base_answer(:user_id => current_user.id, :survey_question_id => @survey_question.id)
- #@answer.base_answer.build
- #@answer.base_answer.user = current_user
- #@answer.base_answer.survey_question = @survey_question
- if @answer.save
- flash[:notice] = "Successfully created answer."
- next_survey_question = @answer.base_answer.survey_question.lower_item
- redir = next_survey_question.nil? ? survey_end_path(@survey_question.survey) : new_survey_question_answer_path(next_survey_question)
- redirect_to redir
- else
- render :action => 'new'
- end
- end
- def edit
- @answer = Answer.find(params[:id])
- end
- def update
- @answer = Answer.find(params[:id])
- if @answer.update_attributes(params[:answer])
- flash[:notice] = "Successfully updated answer."
- redirect_to @answer
- else
- render :action => 'edit'
- end
- end
- def destroy
- @answer = Answer.find(params[:id])
- @answer.destroy
- flash[:notice] = "Successfully destroyed answer."
- redirect_to answers_url
- end
- private
- def get_survey_question
- @survey_question = SurveyQuestion.find(params[:survey_question_id])
- end
- def new_answer
- @answer = @survey_question.question.new_answer
- end
- end
Add Comment
Please, Sign In to add comment