Module: Caboose::Can::Flag::ClassMethods

Defined in:
lib/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.



15
16
17
18
19
20
21
22
23
# File 'lib/can_flag.rb', line 15

def can_be_flagged(opts={})
  has_many :flags, :as => :flaggable, :dependent => :destroy
  validates_associated :flags, :message => 'failed to validate'
  include Caboose::Can::Flag::InstanceMethods
  extend  Caboose::Can::Flag::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.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/can_flag.rb', line 29

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