1 class UserBlock < ActiveRecord::Base
2 validate :moderator_permissions
4 belongs_to :user, :class_name => "User", :foreign_key => :user_id
5 belongs_to :creator, :class_name => "User", :foreign_key => :creator_id
6 belongs_to :revoker, :class_name => "User", :foreign_key => :revoker_id
8 PERIODS = USER_BLOCK_PERIODS
11 # scope to match active blocks
13 where("needs_view or ends_at > ?", Time.now.getutc)
17 # return a renderable version of the reason text.
19 RichText.new(self[:reason_format], self[:reason])
23 # returns true if the block is currently active (i.e: the user can't
26 needs_view || ends_at > Time.now.getutc
30 # returns true if the block is a "zero hour" block
32 # if the times differ more than 1 minute we probably have more important issues
33 needs_view && (ends_at.to_i - updated_at.to_i) < 60
37 # revokes the block, allowing the user to use the API again. the argument
38 # is the user object who is revoking the ban.
41 :ends_at => Time.now.getutc,
42 :revoker_id => revoker.id,
50 # validate that only moderators are allowed to change the
51 # block. this should be caught and dealt with in the controller,
52 # but i've also included it here just in case.
53 def moderator_permissions
54 errors.add(:base, I18n.t("user_block.model.non_moderator_update")) if creator_id_changed? && !creator.moderator?
55 errors.add(:base, I18n.t("user_block.model.non_moderator_revoke")) unless revoker_id.nil? || revoker.moderator?