Files
grecia/app/models/flag.rb
kikito 909dfb4ce3 Several renamings
InappropiateFlag -> Flag
x.flag_as_inappropiate -> x.flag
x.undo_flag_as_inappropiate -> x.unflag
X.flagged_as_inappropiate -> x.flagged
flag-as-inappropiate-actions views & css -> flag-actions views & css
2015-08-27 10:48:49 +02:00

42 lines
980 B
Ruby

class Flag < ActiveRecord::Base
belongs_to :user
belongs_to :flaggable, polymorphic: true, counter_cache: true
scope(:by_user_and_flaggable, lambda do |user, flaggable|
where(user_id: user.id,
flaggable_type: flaggable.class.to_s,
flaggable_id: flaggable.id)
end)
class AlreadyFlaggedError < StandardError
def initialize
super "The flaggable was already flagged by this user"
end
end
class NotFlaggedError < StandardError
def initialize
super "The flaggable was not flagged by this user"
end
end
def self.flag!(user, flaggable)
raise AlreadyFlaggedError if flagged?(user, flaggable)
create(user: user, flaggable: flaggable)
end
def self.unflag!(user, flaggable)
flags = by_user_and_flaggable(user, flaggable)
raise NotFlaggedError if flags.empty?
flags.destroy_all
end
def self.flagged?(user, flaggable)
!! by_user_and_flaggable(user, flaggable).try(:first)
end
end