Sha256: ab7d0448df91ab164e4956ca2901235af03d25cdcccf3cd83d416aad21030d57
Contents?: true
Size: 1.88 KB
Versions: 1
Compression:
Stored size: 1.88 KB
Contents
module MakeFlaggable module Flagger extend ActiveSupport::Concern included do has_many :flaggings, :class_name => "MakeFlaggable::Flagging", :as => :flagger end module ClassMethods def flagger? true end end # 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. 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 # 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. def flag(flaggable, reason = nil) begin flag!(flaggable, reason) rescue Exceptions::AlreadyFlaggedError end end def unflag!(flaggable) check_flaggable(flaggable) flaggings = fetch_flaggings(flaggable) raise Exceptions::NotFlaggedError if flaggings.empty? flaggings.destroy_all true end def unflag(flaggable) begin unflag!(flaggable) success = true rescue Exceptions::NotFlaggedError success = false end success end private def fetch_flaggings(flaggable) flaggings.where({ :flaggable_type => flaggable.class.to_s, :flaggable_id => flaggable.id }) end def check_flaggable(flaggable) raise Exceptions::InvalidFlaggableError unless flaggable.class.flaggable? end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
make_flaggable-0.0.2 | lib/make_flaggable/flagger.rb |