Skip to content

Commit

Permalink
Rework what arguments are passed to maskers
Browse files Browse the repository at this point in the history
- For clarity and predictability, encapsulate all attr_masker options
  which are further passed to masker in a single hash.
- Pass the attribute name as well.
  • Loading branch information
skalee committed Feb 3, 2020
1 parent e55be9c commit da015a9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/attr_masker/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def should_mask?(model_instance)
def mask(model_instance)
value = unmarshal_data(model_instance.send(name))
masker = options[:masker]
masker_value = masker.call(value: value, model: model_instance, **options)
masker_value = masker.call(value: value, model: model_instance,
attribute_name: name, masking_options: options)
model_instance.send("#{name}=", marshal_data(masker_value))
end

Expand Down
12 changes: 12 additions & 0 deletions spec/unit/attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ def retval_for_opts(opts)
subject.bind(receiver).call(model_instance)
end

it "passes the attribute name to the masker" do
expect(masker).to receive(:call).
with(hash_including(attribute_name: :some_attr))
subject.bind(receiver).call(model_instance)
end

it "passes the masking options to the masker" do
expect(masker).to receive(:call).
with(hash_including(masking_options: options))
subject.bind(receiver).call(model_instance)
end

it "marshals the masked value, and assigns it to the attribute" do
expect(receiver).to receive(:marshal_data).
with("masked_value").and_return("marshalled_masked_value")
Expand Down

0 comments on commit da015a9

Please sign in to comment.