Guest User

Untitled

a guest
Oct 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. class Question < ActiveRecord::Base
  2. attr_accessible :crowd_id, :question_type, :body, :priority, :choices_attributes
  3.  
  4. belongs_to :crowd
  5.  
  6. has_many :choices
  7.  
  8. accepts_nested_attributes_for(
  9. :choices,
  10. :reject_if => lambda{ |c| c[:body].blank? }
  11. )
  12.  
  13. validates_presence_of :crowd_id, :body, :question_type
  14.  
  15. TYPES = {
  16. 'True/False' => 'boolean',
  17. 'Poll' => 'poll',
  18. 'Score' => 'score',
  19. 'Video' => 'video'
  20. }
  21.  
  22.  
  23. ## Class Methods ##
  24.  
  25. # Convenience method for creating a new question
  26. # Returns: a saved Question object
  27. # NOTE: this method should be used throughout the app
  28. # whenever a new question needs to be created
  29. def self.set(params={})
  30. q = (id = params[:id]) ? Question.find(id) : Question.new
  31.  
  32. params.each do |k, v|
  33. q.send("#{k}=", v)
  34. end
  35.  
  36. q.save!
  37.  
  38. return q
  39. end
  40.  
  41.  
  42. ## Instance methods ##
  43.  
  44. # Add a question the given user's question queue
  45. # Requires: a User object
  46. # Returns: boolean
  47. def add_user(user, params={})
  48. params = {
  49. :question => self,
  50. :user => user
  51. }
  52. QuestionQueue.set(params)
  53. end
Add Comment
Please, Sign In to add comment