aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/censor_rule.rb5
-rw-r--r--app/models/info_request.rb5
-rw-r--r--app/models/outgoing_message.rb47
3 files changed, 39 insertions, 18 deletions
diff --git a/app/models/censor_rule.rb b/app/models/censor_rule.rb
index 3b5c2d805..58170f237 100644
--- a/app/models/censor_rule.rb
+++ b/app/models/censor_rule.rb
@@ -42,6 +42,11 @@ class CensorRule < ActiveRecord::Base
:user_id => nil,
:public_body_id => nil } }
+ def apply_to_text(text_to_censor)
+ return nil if text_to_censor.nil?
+ text_to_censor.gsub(to_replace, replacement)
+ end
+
def apply_to_text!(text_to_censor)
return nil if text_to_censor.nil?
text_to_censor.gsub!(to_replace, replacement)
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index 3ce89be3b..3a81860ad 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -811,8 +811,9 @@ public
# Text from the the initial request, for use in summary display
def initial_request_text
- return '' if outgoing_messages.empty? # mainly for use with incomplete fixtures
- outgoing_messages.first.get_text_for_indexing
+ return '' if outgoing_messages.empty?
+ body_opts = { :censor_rules => applicable_censor_rules }
+ outgoing_messages.first.try(:get_text_for_indexing, true, body_opts) or ''
end
# Returns index of last event which is described or nil if none described.
diff --git a/app/models/outgoing_message.rb b/app/models/outgoing_message.rb
index c2c8ef4f2..4f6318b3d 100644
--- a/app/models/outgoing_message.rb
+++ b/app/models/outgoing_message.rb
@@ -140,22 +140,28 @@ class OutgoingMessage < ActiveRecord::Base
end
end
- def body
- ret = read_attribute(:body)
- if ret.nil?
- return ret
+ # Public: The body text of the OutgoingMessage. The text is cleaned and
+ # CensorRules are applied.
+ #
+ # options - Hash of options
+ # :censor_rules - Array of CensorRules to apply. Defaults to the
+ # applicable_censor_rules of the associated
+ # InfoRequest. (optional)
+ #
+ # Returns a String
+ def body(options = {})
+ text = raw_body.dup
+ return text if text.nil?
+
+ text = clean_text(text)
+
+ # Use the given censor_rules; otherwise fetch them from the associated
+ # info_request
+ censor_rules = options.fetch(:censor_rules) do
+ info_request.try(:applicable_censor_rules) or []
end
- ret = ret.dup
- ret.strip!
- ret.gsub!(/(?:\n\s*){2,}/, "\n\n") # remove excess linebreaks that unnecessarily space it out
-
- # Remove things from censor rules
- unless info_request.nil?
- self.info_request.apply_censor_rules_to_text!(ret)
- end
-
- ret
+ censor_rules.reduce(text) { |text, rule| rule.apply_to_text(text) }
end
def raw_body
@@ -227,8 +233,12 @@ class OutgoingMessage < ActiveRecord::Base
end
# Returns text for indexing / text display
- def get_text_for_indexing(strip_salutation = true)
- text = body.strip
+ def get_text_for_indexing(strip_salutation = true, opts = {})
+ if opts.empty?
+ text = body.strip
+ else
+ text = body(opts).strip
+ end
# Remove salutation
text.sub!(/Dear .+,/, "") if strip_salutation
@@ -332,6 +342,11 @@ class OutgoingMessage < ActiveRecord::Base
errors.add(:what_doing_dummy, _('Please choose what sort of reply you are making.'))
end
end
+
+ # remove excess linebreaks that unnecessarily space it out
+ def clean_text(text)
+ text.strip.gsub(/(?:\n\s*){2,}/, "\n\n")
+ end
end