Skip to content

Instantly share code, notes, and snippets.

@critzjm
Created June 13, 2012 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save critzjm/2925187 to your computer and use it in GitHub Desktop.
Save critzjm/2925187 to your computer and use it in GitHub Desktop.
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