Module: MakeFlaggable::Flagger

Extended by:
ActiveSupport::Concern
Defined in:
lib/make_flaggable/flagger.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#flag(flaggable, reason = nil) ⇒ Object

Flag the flaggable, but don’t raise an error if the flaggable was already flagged and :flag_once was set. If :flag_once was not set then this method behaves like flag!. The flagging is simply ignored then.



31
32
33
34
35
36
# File 'lib/make_flaggable/flagger.rb', line 31

def flag(flaggable, reason = nil)
  begin
    flag!(flaggable, reason)
  rescue Exceptions::AlreadyFlaggedError
  end
end

#flag!(flaggable, reason = nil) ⇒ Object

Flag a flaggable using the provided reason. Raises an AlreadyFlaggedError if the flagger already flagged the flaggable and :flag_once option is set. Raises an InvalidFlaggableError if the flaggable is not a valid flaggable.



18
19
20
21
22
23
24
25
26
# File 'lib/make_flaggable/flagger.rb', line 18

def flag!(flaggable, reason = nil)
  check_flaggable(flaggable)

  if (flaggable_options[:flag_once] && fetch_flaggings(flaggable).try(:first))
    raise MakeFlaggable::Exceptions::AlreadyFlaggedError.new
  end

  Flagging.create(:flaggable => flaggable, :flagger => self, :reason => reason)
end

#flagged?(flaggable) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/make_flaggable/flagger.rb', line 60

def flagged?(flaggable)
  check_flaggable(flaggable)

  fetch_flaggings(flaggable).try(:first) ? true : false
end

#unflag(flaggable) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/make_flaggable/flagger.rb', line 50

def unflag(flaggable)
  begin
    unflag!(flaggable)
    success = true
  rescue Exceptions::NotFlaggedError
    success = false
  end
  success
end

#unflag!(flaggable) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/make_flaggable/flagger.rb', line 38

def unflag!(flaggable)
  check_flaggable(flaggable)

  flaggings = fetch_flaggings(flaggable)

  raise Exceptions::NotFlaggedError if flaggings.empty?

  flaggings.destroy_all

  true
end