diff options
author | Robin Houston <robin.houston@gmail.com> | 2012-09-06 15:00:05 +0100 |
---|---|---|
committer | Robin Houston <robin.houston@gmail.com> | 2012-09-06 15:00:05 +0100 |
commit | 006717f1ac39557e50052bb5755d430a1fa100ee (patch) | |
tree | d07209a4ad2003111c03948d537127d40dda9799 /app/models/censor_rule.rb | |
parent | 537b97ff069cac999da3ca80ede77fedc79e39ff (diff) | |
parent | 74531a783a8ea62a80596b435bd151cec2bf82c8 (diff) |
Merge branch 'master' of git.mysociety.org:/data/git/public/alaveteli
Conflicts:
spec/controllers/api_controller_spec.rb
Diffstat (limited to 'app/models/censor_rule.rb')
-rw-r--r-- | app/models/censor_rule.rb | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/app/models/censor_rule.rb b/app/models/censor_rule.rb index a477d2568..da3f49760 100644 --- a/app/models/censor_rule.rb +++ b/app/models/censor_rule.rb @@ -9,6 +9,7 @@ # public_body_id :integer # text :text not null # replacement :text not null +# regexp :boolean # last_edit_editor :string(255) not null # last_edit_comment :text not null # created_at :datetime not null @@ -28,33 +29,59 @@ class CensorRule < ActiveRecord::Base belongs_to :user belongs_to :public_body - def binary_replacement - self.text.gsub(/./, 'x') + # a flag to allow the require_user_request_or_public_body validation to be skipped + attr_accessor :allow_global + validate :require_user_request_or_public_body, :unless => proc{ |rule| rule.allow_global == true } + validate :require_valid_regexp, :if => proc{ |rule| rule.regexp? == true } + validates_presence_of :text + + named_scope :global, {:conditions => {:info_request_id => nil, + :user_id => nil, + :public_body_id => nil}} + + def require_user_request_or_public_body + if self.info_request.nil? && self.user.nil? && self.public_body.nil? + errors.add("Censor must apply to an info request a user or a body; ") + end + end + + def require_valid_regexp + begin + self.make_regexp() + rescue RegexpError => e + errors.add(:text, e.message) + end + end + + def make_regexp + return Regexp.new(self.text, Regexp::MULTILINE) end def apply_to_text!(text) if text.nil? return nil end - text.gsub!(self.text, self.replacement) + to_replace = regexp? ? self.make_regexp() : self.text + text.gsub!(to_replace, self.replacement) end + def apply_to_binary!(binary) if binary.nil? return nil end - binary.gsub!(self.text, self.binary_replacement) + to_replace = regexp? ? self.make_regexp() : self.text + binary.gsub!(to_replace){ |match| match.gsub(/./, 'x') } end - - def validate - if self.info_request.nil? && self.user.nil? && self.public_body.nil? - errors.add("Censor must apply to an info request a user or a body; ") + def for_admin_column + self.class.content_columns.each do |column| + yield(column.human_name, self.send(column.name), column.type.to_s, column.name) end end - def for_admin_column - self.class.content_columns.each do |column| - yield(column.human_name, self.send(column.name), column.type.to_s, column.name) + def is_global? + return true if (info_request_id.nil? && user_id.nil? && public_body_id.nil?) + return false end - end + end |