Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run callbacks at the right level #37

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions lib/acts_as_scrubbable/scrub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@ module Scrub
def scrubbed_values
return unless self.class.scrubbable?

run_callbacks(:scrub) do
_updates = {}
_updates = {}

scrubbable_fields.each do |_field, value|
unless self.respond_to?(_field)
raise ArgumentError, "#{self.class} do not respond to #{_field}"
end
next if self.send(_field).blank?

if ActsAsScrubbable.scrub_map.keys.include?(value)
_updates[_field] = ActsAsScrubbable.scrub_map[value].call
else
puts "Undefined scrub: #{value} for #{self.class}.#{_field}"
end
scrubbable_fields.each do |_field, value|
unless self.respond_to?(_field)
raise ArgumentError, "#{self.class} do not respond to #{_field}"
end
next if self.send(_field).blank?

_updates
if ActsAsScrubbable.scrub_map.keys.include?(value)
_updates[_field] = ActsAsScrubbable.scrub_map[value].call
else
puts "Undefined scrub: #{value} for #{self.class}.#{_field}"
end
end

_updates
end
end
end
6 changes: 4 additions & 2 deletions lib/acts_as_scrubbable/update_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ class UpdateProcessor
def handle_batch(batch)
scrubbed_count = 0
batch.each do |obj|
_updates = obj.scrubbed_values
obj.update_columns(_updates) unless _updates.empty?
obj.run_callbacks(:scrub) do
_updates = obj.scrubbed_values
obj.update_columns(_updates) unless _updates.empty?
end
scrubbed_count += 1
end
scrubbed_count
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_scrubbable/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ActsAsScrubbable
VERSION = '2.1.4'
VERSION = '2.1.5'
end
16 changes: 5 additions & 11 deletions spec/lib/acts_as_scrubbable/scrub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,25 @@
expect(_updates[:address1]).to be_nil
end

it 'runs scrub callbacks' do
subject.scrubbed_values
expect(subject.scrubbing_begun).to be(true)
expect(subject.scrubbing_finished).to be(true)
end

it 'output no information when all scrubbers found' do
expect(STDOUT).to_not receive(:puts)

_updates = subject.scrubbed_values
end

context "scrubbable" do
subject { MissingScrubbableModel.new }

it 'outputs warning message' do
subject.first_name = "Johnny"
subject.last_name = "Frank"

allow(Faker::Name).to receive(:first_name).and_return("Larry")
allow(Faker::Name).to receive(:last_name).and_return("Baker")

expect(STDOUT).to receive(:puts).with('Undefined scrub: fake_first_name for MissingScrubbableModel.first_name')
expect(Faker::Name).to_not receive(:first_name)

_updates = subject.scrubbed_values
expect(_updates[:last_name]).to eq('Baker')
expect(_updates[:first_name]).to be_nil
Expand Down
8 changes: 7 additions & 1 deletion spec/lib/acts_as_scrubbable/update_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@

expect(subject.send(:handle_batch, [model])).to eq 1
end

it "runs scrub callbacks" do
subject.send(:handle_batch, [model])
expect(model.scrubbing_begun).to be(true)
expect(model.scrubbing_finished).to be(true)
end
end
end
end