Skip to content

Commit

Permalink
chore: update structure of callback naming
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosgz committed Jul 21, 2024
1 parent 7781ed8 commit 84f2617
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
7 changes: 4 additions & 3 deletions lib/esse/active_record/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def register_callback(identifier, operation, callback_class)
raise ArgumentError, 'callback_class must be a subclass of Esse::ActiveRecord::Callback'
end

key = :"#{identifier}_on_#{operation}"
key = [operation, identifier].join('_').to_sym

@callbacks = @callbacks ? @callbacks.dup : {}
if @callbacks.key?(key)
Expand All @@ -42,11 +42,12 @@ def register_callback(identifier, operation, callback_class)
def registered?(identifier, operation)
return false unless @callbacks

@callbacks.key?(:"#{identifier}_on_#{operation}")
key = [operation, identifier].join('_').to_sym
@callbacks.key?(key)
end

def fetch!(identifier, operation)
key = :"#{identifier}_on_#{operation}"
key = [operation, identifier].join('_').to_sym
if registered?(identifier, operation)
[key, @callbacks[key]]
else
Expand Down
2 changes: 1 addition & 1 deletion spec/esse/active_record/callbacks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
it 'returns the callback class' do
klass = Class.new(Esse::ActiveRecord::Callback)
described_class.register_callback(:external, :create, klass)
expect(described_class.fetch!(:external, :create)).to eq([:external_on_create, klass])
expect(described_class.fetch!(:external, :create)).to eq([:create_external, klass])
end
end
end
10 changes: 5 additions & 5 deletions spec/esse/active_record/model/esse_callback_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class DumpTempCallbackOnDestroy < DumpTempCallback; end
}.not_to raise_error
expect(model_class.esse_callbacks).to a_hash_including(
'states:state' => a_hash_including(
temp_on_create: contain_exactly(DumpTempCallbackOnCreate, {}, an_instance_of(Proc)),
temp_on_update: contain_exactly(DumpTempCallbackOnUpdate, {}, an_instance_of(Proc)),
create_temp: contain_exactly(DumpTempCallbackOnCreate, {}, an_instance_of(Proc)),
update_temp: contain_exactly(DumpTempCallbackOnUpdate, {}, an_instance_of(Proc)),
)
)
expect(model_class.esse_callbacks).to be_frozen
Expand Down Expand Up @@ -165,7 +165,7 @@ class DumpTempCallbackOnDestroy < DumpTempCallback; end
model_class.esse_callback('states:state', :temp, on: %i[create], custom: 'value') { :ok }
expect(model_class.esse_callbacks).to a_hash_including(
'states:state' => a_hash_including(
temp_on_create: contain_exactly(DumpTempCallbackOnCreate, {custom: 'value'}, an_instance_of(Proc)),
create_temp: contain_exactly(DumpTempCallbackOnCreate, {custom: 'value'}, an_instance_of(Proc)),
)
)
end
Expand Down Expand Up @@ -212,7 +212,7 @@ class DumpTempCallbackOnDestroy < DumpTempCallback; end
model_class.esse_callback('states:state', :temp, on: %i[update], custom: 'value') { :ok }
expect(model_class.esse_callbacks).to a_hash_including(
'states:state' => a_hash_including(
temp_on_update: contain_exactly(DumpTempCallbackOnUpdate, {custom: 'value'}, an_instance_of(Proc)),
update_temp: contain_exactly(DumpTempCallbackOnUpdate, {custom: 'value'}, an_instance_of(Proc)),
)
)
end
Expand Down Expand Up @@ -262,7 +262,7 @@ class DumpTempCallbackOnDestroy < DumpTempCallback; end
model_class.esse_callback('states:state', :temp, on: %i[destroy], custom: 'value') { :ok }
expect(model_class.esse_callbacks).to a_hash_including(
'states:state' => a_hash_including(
temp_on_destroy: contain_exactly(DumpTempCallbackOnDestroy, {custom: 'value'}, an_instance_of(Proc)),
destroy_temp: contain_exactly(DumpTempCallbackOnDestroy, {custom: 'value'}, an_instance_of(Proc)),
)
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
model_class.update_lazy_attribute_callback('states:state', :field, custom: 'value') { :ok }
expect(model_class.esse_callbacks).to a_hash_including(
'states:state' => a_hash_including(
update_lazy_attribute_on_create: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
update_lazy_attribute_on_update: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
update_lazy_attribute_on_destroy: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
create_update_lazy_attribute: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
update_update_lazy_attribute: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
destroy_update_lazy_attribute: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
)
)
end
Expand All @@ -51,7 +51,7 @@
model_class.update_lazy_attribute_callback('states:state', :field, on: %i[create], custom: 'value') { :ok }
expect(model_class.esse_callbacks).to a_hash_including(
'states:state' => a_hash_including(
update_lazy_attribute_on_create: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
create_update_lazy_attribute: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
)
)
end
Expand All @@ -67,7 +67,7 @@
model_class.update_lazy_attribute_callback('states:state', :field, on: %i[update], custom: 'value') { :ok }
expect(model_class.esse_callbacks).to a_hash_including(
'states:state' => a_hash_including(
update_lazy_attribute_on_update: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
update_update_lazy_attribute: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
)
)
end
Expand All @@ -83,7 +83,7 @@
model_class.update_lazy_attribute_callback('states:state', :field, on: %i[destroy], custom: 'value') { :ok }
expect(model_class.esse_callbacks).to a_hash_including(
'states:state' => a_hash_including(
update_lazy_attribute_on_destroy: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
destroy_update_lazy_attribute: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
)
)
end
Expand Down

0 comments on commit 84f2617

Please sign in to comment.