Skip to content

Commit

Permalink
Mask records disregarding default_scope
Browse files Browse the repository at this point in the history
Mask all the records, even if default_scope is defined for given model.
  • Loading branch information
skalee committed Aug 5, 2017
1 parent 53f8b96 commit 4a6fd84
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ attr_masker :email :unless => ->(record) { ! record.tester_user? }
attr_masker :first_name, :if => :tester_user?
----

The ActiveRecord's `::default_scope` method has no effect on masking. All
table records are updated, provided that :if and :unless filters allow that.
For example, if you're using a Paranoia[https://github.com/rubysherpas/paranoia]
gem to soft-delete your data, records marked as deleted will be masked as well.

=== Using custom maskers

By default, data is maksed with `AttrMasker::Maskers::SIMPLE` masker which
Expand Down
6 changes: 3 additions & 3 deletions lib/attr_masker/performer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def mask

def mask_class(klass)
progressbar_for_model(klass) do |bar|
klass.all.each do |model|
klass.all.unscoped.each do |model|
mask_object model
bar.increment
end
Expand All @@ -44,13 +44,13 @@ def mask_object(instance)
acc.merge!(column_name => masker_value)
end

klass.all.update(instance.id, updates)
klass.all.unscoped.update(instance.id, updates)
end

def progressbar_for_model(klass)
bar = ProgressBar.create(
title: klass.name,
total: klass.count,
total: klass.unscoped.count,
throttle_rate: 0.1,
format: %q[%t %c/%C (%j%%) %B %E],
)
Expand Down
16 changes: 16 additions & 0 deletions spec/features_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,22 @@ def dump_json(*args)
end
end

example "It masks records disregarding default scope" do
User.class_eval do
attr_masker :last_name

default_scope ->() { where(last_name: "Solo") }
end

expect { run_rake_task }.not_to(change { User.unscoped.count })

[han, luke].each do |record|
expect { record.reload }.to(
change { record.last_name }.to("(redacted)")
)
end
end

def run_rake_task
Rake::Task["db:mask"].execute
end
Expand Down

0 comments on commit 4a6fd84

Please sign in to comment.