From a687b48c0ae1fe6286e5055d7c2041df191170c8 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 29 Apr 2015 11:38:43 +0100 Subject: InfoRequest#initial_request_text minor improvement system total real old 0.810000 0.300000 1.110000 ( 1.358172) new 0.970000 0.090000 1.060000 ( 1.292340) --- app/models/info_request.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 0ca3a1279..3451c0b71 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -804,8 +804,7 @@ 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 + outgoing_messages.first.try(:get_text_for_indexing) or '' end # Returns index of last event which is described or nil if none described. -- cgit v1.2.3 From f2b292f61a8494ce77dbf5bd7ced2b010d20b688 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 29 Apr 2015 12:20:50 +0100 Subject: Extract text cleaning from #body method --- app/models/outgoing_message.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'app/models') diff --git a/app/models/outgoing_message.rb b/app/models/outgoing_message.rb index c2c8ef4f2..e59ca4bd4 100644 --- a/app/models/outgoing_message.rb +++ b/app/models/outgoing_message.rb @@ -141,21 +141,17 @@ class OutgoingMessage < ActiveRecord::Base end def body - ret = read_attribute(:body) - if ret.nil? - return ret - end + text = read_attribute(:body).dup + return text if text.nil? - ret = ret.dup - ret.strip! - ret.gsub!(/(?:\n\s*){2,}/, "\n\n") # remove excess linebreaks that unnecessarily space it out + text = clean_text(text) # Remove things from censor rules unless info_request.nil? - self.info_request.apply_censor_rules_to_text!(ret) + self.info_request.apply_censor_rules_to_text!(text) end - ret + text end def raw_body @@ -332,6 +328,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 -- cgit v1.2.3 From 4a244582f4da2247cedbf2b4ec0a0ee64b50ca55 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 29 Apr 2015 12:21:47 +0100 Subject: Use #raw_body sugar method --- app/models/outgoing_message.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models') diff --git a/app/models/outgoing_message.rb b/app/models/outgoing_message.rb index e59ca4bd4..8240eb784 100644 --- a/app/models/outgoing_message.rb +++ b/app/models/outgoing_message.rb @@ -141,7 +141,7 @@ class OutgoingMessage < ActiveRecord::Base end def body - text = read_attribute(:body).dup + text = raw_body.dup return text if text.nil? text = clean_text(text) -- cgit v1.2.3 From 4e568aab076f818ea5d011d05779edfa2bca68ec Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 29 Apr 2015 12:48:05 +0100 Subject: Add a non-mutating #apply_to_text Still need to `return nil if text_to_censor.nil?` because `gsub!` returns `nil` rather than the original text if it does not make a substitution. --- app/models/censor_rule.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'app/models') 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) -- cgit v1.2.3 From dd00661123379ccb508a7422b152c17769f7501d Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 29 Apr 2015 13:44:51 +0100 Subject: Allow optional censor rules to be passed to #body MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If we’re in the context of an InfoRequest and want the OutgoingMessage#body, we can pass the rules in explicitly to avoid looking up the info request again. Otherwise it falls back to the existing behaviour. --- app/models/outgoing_message.rb | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'app/models') diff --git a/app/models/outgoing_message.rb b/app/models/outgoing_message.rb index 8240eb784..3448009bd 100644 --- a/app/models/outgoing_message.rb +++ b/app/models/outgoing_message.rb @@ -140,18 +140,28 @@ class OutgoingMessage < ActiveRecord::Base end end - def body + # 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) - # Remove things from censor rules - unless info_request.nil? - self.info_request.apply_censor_rules_to_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 - text + censor_rules.reduce(text) { |text, rule| rule.apply_to_text(text) } end def raw_body -- cgit v1.2.3 From 9329b7f06d8a36ecb901d2172836ab8f5b2cdf3b Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 29 Apr 2015 15:09:29 +0100 Subject: InfoRequest#initial_request_text performance Passes the InfoRequest's CensorRules directly to the OutgoingMessage. user system total real old 0.010000 0.010000 0.020000 ( 0.020827) new 0.010000 0.000000 0.010000 ( 0.016633) --- app/models/info_request.rb | 4 +++- app/models/outgoing_message.rb | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) (limited to 'app/models') diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 3451c0b71..01d5f5c52 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -804,7 +804,9 @@ public # Text from the the initial request, for use in summary display def initial_request_text - outgoing_messages.first.try(:get_text_for_indexing) or '' + 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 3448009bd..4f6318b3d 100644 --- a/app/models/outgoing_message.rb +++ b/app/models/outgoing_message.rb @@ -233,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 -- cgit v1.2.3