Skip to content

Commit edb07f0

Browse files
committed
Add new option. Review.
1 parent 11c6191 commit edb07f0

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
lines changed

lib/table_sync/receiving/config.rb

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,15 @@ def add_option(name, value_setter_wrapper:, value_as_proc_setter_wrapper:, defau
5858
def add_hook_option(name, hook_class:)
5959
ivar = :"@#{name}"
6060

61-
default_conditions = { columns: %i[] }
62-
default_handler = proc { |**_| }
63-
6461
@default_values_for_options ||= {}
65-
@default_values_for_options[ivar] = proc do
66-
hook_class.new(
67-
conditions: default_conditions,
68-
handler: default_handler,
69-
)
70-
end
62+
@default_values_for_options[ivar] = proc { [] }
7163

7264
define_method(name) do |conditions, &handler|
73-
hook = hook_class.new(conditions:, handler:)
74-
instance_variable_set(ivar, hook)
65+
hooks = instance_variable_get(ivar)
66+
hooks ||= []
67+
68+
hooks << hook_class.new(conditions:, handler:)
69+
instance_variable_set(ivar, hooks)
7570
end
7671
end
7772
end

lib/table_sync/receiving/handler.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@ def perform(config, params) # rubocop:disable Metrics/MethodLength
161161
model.after_commit do
162162
config.option(:after_commit_on_update, **params, results:)
163163

164-
hook = config.option(:on_first_sync)
165-
hook.perform(config:, targets: results) if hook.enabled?
164+
Array(config.option(:on_first_sync)).each do |hook|
165+
hook.perform(config:, targets: results) if hook.enabled?
166+
end
166167
end
167168
else
168169
model.after_commit { config.option(:after_commit_on_destroy, **params, results:) }

spec/receiving/config_spec.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,23 +351,20 @@
351351

352352
describe "#on_first_sync" do
353353
it "returns correct default value" do
354-
hook = config.option(:on_first_sync)
355-
expect(hook).not_to be_nil
356-
expect(hook).not_to be_enabled
357-
expect(hook.conditions[:columns]).to be_empty
358-
expect(hook.lookup_code).to eq("")
354+
hooks = config.option(:on_first_sync)
355+
expect(hooks).to be_empty
359356
end
360357

361358
it "processes a value" do
362359
config.on_first_sync(columns: %i[test], test: "value") do |**_|
363360
# Some hook work here
364361
end
365362

366-
hook = config.option(:on_first_sync)
367-
expect(hook).not_to be_nil
368-
expect(hook).to be_enabled
369-
expect(hook.conditions[:columns]).not_to be_empty
370-
expect(hook.lookup_code).to eq("test-value")
363+
hooks = config.option(:on_first_sync)
364+
expect(hooks).not_to be_nil
365+
expect(hooks.first).to be_enabled
366+
expect(hooks.first.conditions[:columns]).not_to be_empty
367+
expect(hooks.first.lookup_code).to eq("test-value")
371368
end
372369
end
373370

0 commit comments

Comments
 (0)