Module: Gluttonberg::CanFlag::ClassMethods

Defined in:
lib/gluttonberg/can_flag.rb

Instance Method Summary collapse

Instance Method Details

#can_be_flagged(opts = {}) ⇒ Object

Call can_be_flagged from your content model, or from anything you want to flag.



23
24
25
26
27
28
29
30
31
# File 'lib/gluttonberg/can_flag.rb', line 23

def can_be_flagged(opts={})
  has_many :flags, :as => :flaggable, :dependent => :destroy
  validates_associated :flags, :message => 'failed to validate'
  include Gluttonberg::CanFlag::InstanceMethods
  extend  Gluttonberg::CanFlag::SingletonMethods
  cattr_accessor :reasons
  self.reasons = opts[:reasons] || [:inappropriate]
  (::Flag.flaggable_models ||= []) << self
end

#can_flagObject

Call can_flag from your user model, or anything that can own a flagging. That’s a paddlin! Limitation for now: you should only allow one model to own flags. This is ridiculously easy to make polymorphic, but no ponies yet.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gluttonberg/can_flag.rb', line 37

def can_flag
  # has_many :flaggables, :foreign_key => "user_id"
  # User created these flags
  has_many :flags, :foreign_key => "user_id", :order => "id desc"
  
  # User was responsible for creating this filth
  has_many :flaggings, :foreign_key => "flaggable_user_id", :class_name => "Flag"
  include CanFlagInstanceMethods
  
  # Associate the flag back here
  # Flag.belongs_to :user
  # Flag.belongs_to :owner, :foreign_key => flaggable_user_id
  ::Flag.class_eval "belongs_to :#{name.underscore}, :foreign_key => :user_id; 
    belongs_to :owner, :foreign_key => :flaggable_user_id, :class_name => '#{name}'"
end