aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/censor_rule.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/censor_rule.rb')
-rw-r--r--app/models/censor_rule.rb51
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