Skip to content

Commit b40fd2a

Browse files
Matt Besseynettofarah
authored andcommitted
[strategies] support optional 2nd instance argument (#40)
As discussed in #39 its nice to have the option to access the context (AR instance) from the scrub strategy. this allows that.
1 parent fd9eb08 commit b40fd2a

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Warning: This is not a security feature. Fields can still easily be rearranged b
137137

138138
#### Advanced Obfuscation
139139

140-
For more advanced obfuscation, you can pass in a custom obfuscation strategy. Polo will take in a lambda that can be used to transform sensitive data.
140+
For more advanced obfuscation, you can pass in a custom obfuscation strategy. Polo will take in a lambda that can be used to transform sensitive data.
141141

142142
Using a `:symbol` as an obfuscate key targets all columns of that name. Passing an SQL selector as a `String` will target columns within the specified table.
143143

@@ -148,14 +148,21 @@ Polo.configure do
148148
first_part = email.split("@")[0]
149149
"#{first_part}@test.com"
150150
end
151-
151+
152152
credit_card_strategy = lambda do |credit_card|
153153
"4123 4567 8910 1112"
154154
end
155-
155+
156+
# If you need the context of the record for its fields, it is accessible
157+
# in the second argument of the strategy
158+
social_security_strategy = lambda do |ssn, instance|
159+
sprintf("%09d", instance.id)
160+
end
161+
156162
obfuscate({
157163
'chefs.email' => email_strategy, # This only applies to the "email" column in the "chefs" table
158-
:credit_card => credit_card_strategy # This applies to any column named "credit_card" across every table
164+
:credit_card => credit_card_strategy, # This applies to any column named "credit_card" across every table
165+
:ssn_strategy => social_security_strategy
159166
})
160167
end
161168

@@ -200,4 +207,3 @@ $ bundle exec appraisal rake
200207
## License
201208

202209
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
203-

lib/polo/translator.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ def obfuscate!(instances, fields)
4646

4747
correct_table = table.nil? || instance.class.table_name == table
4848

49-
if correct_table && value = instance.attributes[field]
50-
instance.send("#{field}=", new_field_value(field, strategy, value))
49+
if correct_table && instance.attributes[field]
50+
instance.send("#{field}=", new_field_value(field, strategy, instance))
5151
end
5252
end
5353
end
@@ -65,11 +65,12 @@ def intersection(attrs, fields)
6565
attrs & fields.map { |pair| field_name(pair.first) }
6666
end
6767

68-
def new_field_value(field, strategy, value)
68+
def new_field_value(field, strategy, instance)
69+
value = instance.attributes[field]
6970
if strategy.nil?
7071
value.split("").shuffle.join
7172
else
72-
strategy.call(value)
73+
strategy.arity == 1 ? strategy.call(value) : strategy.call(value, instance)
7374
end
7475
end
7576
end

spec/translator_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@
4747
end
4848
end
4949

50+
context "custom obfuscation strategy using instance context" do
51+
let(:obfuscated_fields) do
52+
{ email: lambda { |field, instance| "#{instance.name}@example.com" } }
53+
end
54+
55+
it "replaces contents of field according to the supplied lambda" do
56+
expect(netto.email.to_s).to eq "Netto@example.com"
57+
end
58+
end
59+
5060
context "no strategy passed in" do
5161
let(:obfuscated_fields) { [:email] }
5262

0 commit comments

Comments
 (0)