diff options
author | Gareth Rees <gareth@mysociety.org> | 2015-05-12 16:48:44 +0100 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2015-05-12 16:48:44 +0100 |
commit | 8f9984b66c7ce5d38b07bb6dfef28e914894a92c (patch) | |
tree | 1874b9165028b6969f6274303878bd2ce6b510d0 /app | |
parent | 7d9de8a5ffe67e6bc49271a082c1d8e43dbb0f03 (diff) | |
parent | 9329b7f06d8a36ecb901d2172836ab8f5b2cdf3b (diff) |
Merge branch 'initial_request_text-2' into rails-3-develop
Diffstat (limited to 'app')
-rw-r--r-- | app/models/censor_rule.rb | 5 | ||||
-rw-r--r-- | app/models/info_request.rb | 5 | ||||
-rw-r--r-- | app/models/outgoing_message.rb | 47 |
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 0ca3a1279..01d5f5c52 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -804,8 +804,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 |