Created
June 13, 2012 16:39
-
-
Save critzjm/2925187 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Question < ActiveRecord::Base | |
attr_accessible :crowd_id, :question_type, :body, :priority, :choices_attributes | |
belongs_to :crowd | |
has_many :choices | |
accepts_nested_attributes_for( | |
:choices, | |
:reject_if => lambda{ |c| c[:body].blank? } | |
) | |
validates_presence_of :crowd_id, :body, :question_type | |
TYPES = { | |
'True/False' => 'boolean', | |
'Poll' => 'poll', | |
'Score' => 'score', | |
'Video' => 'video' | |
} | |
## Class Methods ## | |
# Convenience method for creating a new question | |
# Returns: a saved Question object | |
# NOTE: this method should be used throughout the app | |
# whenever a new question needs to be created | |
def self.set(params={}) | |
q = (id = params[:id]) ? Question.find(id) : Question.new | |
params.each do |k, v| | |
q.send("#{k}=", v) | |
end | |
q.save! | |
return q | |
end | |
## Instance methods ## | |
# Add a question the given user's question queue | |
# Requires: a User object | |
# Returns: boolean | |
def add_user(user, params={}) | |
params = { | |
:question => self, | |
:user => user | |
} | |
QuestionQueue.set(params) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment