diff options
author | Gareth Rees <gareth@mysociety.org> | 2015-04-29 12:48:05 +0100 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2015-05-12 16:13:16 +0100 |
commit | 4e568aab076f818ea5d011d05779edfa2bca68ec (patch) | |
tree | f50b32e2ed9ac2d5becc6bb4f983ba64c896514e | |
parent | 4a244582f4da2247cedbf2b4ec0a0ee64b50ca55 (diff) |
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.
-rw-r--r-- | app/models/censor_rule.rb | 5 | ||||
-rw-r--r-- | spec/models/censor_rule_spec.rb | 36 |
2 files changed, 41 insertions, 0 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/spec/models/censor_rule_spec.rb b/spec/models/censor_rule_spec.rb index 4ecd2d3e1..77b8cd07a 100644 --- a/spec/models/censor_rule_spec.rb +++ b/spec/models/censor_rule_spec.rb @@ -17,6 +17,42 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +describe CensorRule do + + describe :apply_to_text do + + it 'applies the rule to the text' do + rule = FactoryGirl.build(:censor_rule, :text => 'secret') + text = 'Some secret text' + expect(rule.apply_to_text(text)).to eq('Some [REDACTED] text') + end + + it 'does not mutate the input' do + rule = FactoryGirl.build(:censor_rule, :text => 'secret') + text = 'Some secret text' + rule.apply_to_text(text) + expect(text).to eq('Some secret text') + end + + it 'returns the text if the rule is unmatched' do + rule = FactoryGirl.build(:censor_rule, :text => 'secret') + text = 'Some text' + expect(rule.apply_to_text(text)).to eq('Some text') + end + end + + describe :apply_to_text! do + + it 'mutates the input' do + rule = FactoryGirl.build(:censor_rule, :text => 'secret') + text = 'Some secret text' + rule.apply_to_text!(text) + expect(text).to eq('Some [REDACTED] text') + end + + end +end + describe CensorRule, "substituting things" do describe 'when using a text rule' do |