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