1 # == Schema Information
3 # Table name: user_blocks
5 # id :integer not null, primary key
6 # user_id :integer not null
7 # creator_id :integer not null
8 # reason :text not null
9 # ends_at :datetime not null
10 # needs_view :boolean default(FALSE), not null
12 # created_at :datetime
13 # updated_at :datetime
14 # reason_format :enum default("markdown"), not null
18 # index_user_blocks_on_user_id (user_id)
22 # user_blocks_moderator_id_fkey (creator_id => users.id)
23 # user_blocks_revoker_id_fkey (revoker_id => users.id)
24 # user_blocks_user_id_fkey (user_id => users.id)
27 class UserBlock < ActiveRecord::Base
28 validate :moderator_permissions
29 validates :reason, :invalid_chars => true
31 belongs_to :user, :class_name => "User", :foreign_key => :user_id
32 belongs_to :creator, :class_name => "User", :foreign_key => :creator_id
33 belongs_to :revoker, :class_name => "User", :foreign_key => :revoker_id
35 PERIODS = USER_BLOCK_PERIODS
38 # scope to match active blocks
40 where("needs_view or ends_at > ?", Time.now.getutc)
44 # return a renderable version of the reason text.
46 RichText.new(self[:reason_format], self[:reason])
50 # returns true if the block is currently active (i.e: the user can't
53 needs_view || ends_at > Time.now.getutc
57 # returns true if the block is a "zero hour" block
59 # if the times differ more than 1 minute we probably have more important issues
60 needs_view && (ends_at.to_i - updated_at.to_i) < 60
64 # revokes the block, allowing the user to use the API again. the argument
65 # is the user object who is revoking the ban.
68 :ends_at => Time.now.getutc,
69 :revoker_id => revoker.id,
77 # validate that only moderators are allowed to change the
78 # block. this should be caught and dealt with in the controller,
79 # but i've also included it here just in case.
80 def moderator_permissions
81 errors.add(:base, I18n.t("user_block.model.non_moderator_update")) if creator_id_changed? && !creator.moderator?
82 errors.add(:base, I18n.t("user_block.model.non_moderator_revoke")) unless revoker_id.nil? || revoker.moderator?