]> git.openstreetmap.org Git - rails.git/blob - app/models/user_block.rb
Cleanup trailing whitespace
[rails.git] / app / models / user_block.rb
1 class UserBlock < ActiveRecord::Base
2   validate :moderator_permissions
3
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
7
8   PERIODS = USER_BLOCK_PERIODS
9
10   ##
11   # scope to match active blocks
12   def self.active
13     self.where("needs_view or ends_at > ?", Time.now.getutc)
14   end
15
16   ##
17   # return a renderable version of the reason text.
18   def reason
19     RichText.new(read_attribute(:reason_format), read_attribute(:reason))
20   end
21
22   ##
23   # returns true if the block is currently active (i.e: the user can't
24   # use the API).
25   def active?
26     needs_view or ends_at > Time.now.getutc
27   end
28
29   ##
30   # revokes the block, allowing the user to use the API again. the argument
31   # is the user object who is revoking the ban.
32   def revoke!(revoker)
33     update_attributes(
34       :ends_at => Time.now.getutc(),
35       :revoker_id => revoker.id,
36       :needs_view => false
37     )
38   end
39
40 private
41
42   ##
43   # validate that only moderators are allowed to change the
44   # block. this should be caught and dealt with in the controller,
45   # but i've also included it here just in case.
46   def moderator_permissions
47     errors.add(:base, I18n.t('user_block.model.non_moderator_update')) if creator_id_changed? and !creator.moderator?
48     errors.add(:base, I18n.t('user_block.model.non_moderator_revoke')) unless revoker_id.nil? or revoker.moderator?
49   end
50 end