Skip to content

Commit

Permalink
chore: update_lazy_attribute_callback by attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosgz committed Jul 21, 2024
1 parent 84f2617 commit 067b7e3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
7 changes: 5 additions & 2 deletions lib/esse/active_record/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def esse_callbacks
@esse_callbacks ||= {}.freeze
end

def esse_callback(index_repo_name, operation_name, on: %i[create update destroy], **options, &block)
def esse_callback(index_repo_name, operation_name, on: %i[create update destroy], identifier_suffix: nil, **options, &block)
@esse_callbacks = esse_callbacks.dup
cb_if = options.delete(:if)
cb_unless = options.delete(:unless)
Expand All @@ -25,6 +25,9 @@ def esse_callback(index_repo_name, operation_name, on: %i[create update destroy]

Array(on).each do |event|
identifier, klass = Esse::ActiveRecord::Callbacks.fetch!(operation_name, event)
if identifier_suffix
identifier = :"#{identifier}_#{identifier_suffix}"
end

if @esse_callbacks.dig(index_repo_name, identifier)
raise ArgumentError, format('index repository %<name>p already registered %<op>s operation', name: index_repo_name, op: operation_name)
Expand Down Expand Up @@ -71,7 +74,7 @@ def index_callback(index_repo_name, on: %i[create update destroy], with: nil, **

def update_lazy_attribute_callback(index_repo_name, attribute_name, on: %i[create update destroy], **options, &block)
options[:attribute_name] = attribute_name
esse_callback(index_repo_name, :update_lazy_attribute, on: on, **options, &block)
esse_callback(index_repo_name, :update_lazy_attribute, identifier_suffix: attribute_name.to_sym, on: on, **options, &block)
end

# Disable indexing for the block execution on model level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,24 @@
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(
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)),
create_update_lazy_attribute_field: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
update_update_lazy_attribute_field: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
destroy_update_lazy_attribute_field: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
)
)
end

it 'registers multiple callbacks to different attributes' do
model_class.update_lazy_attribute_callback('states:state', :field1, custom: 'value1') { :ok }
model_class.update_lazy_attribute_callback('states:state', :field2, custom: 'value2') { :ok }
expect(model_class.esse_callbacks).to a_hash_including(
'states:state' => a_hash_including(
create_update_lazy_attribute_field1: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field1, custom: 'value1'}, an_instance_of(Proc)),
update_update_lazy_attribute_field1: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field1, custom: 'value1'}, an_instance_of(Proc)),
destroy_update_lazy_attribute_field1: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field1, custom: 'value1'}, an_instance_of(Proc)),
create_update_lazy_attribute_field2: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field2, custom: 'value2'}, an_instance_of(Proc)),
update_update_lazy_attribute_field2: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field2, custom: 'value2'}, an_instance_of(Proc)),
destroy_update_lazy_attribute_field2: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field2, custom: 'value2'}, an_instance_of(Proc)),
)
)
end
Expand All @@ -51,7 +66,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(
create_update_lazy_attribute: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
create_update_lazy_attribute_field: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
)
)
end
Expand All @@ -67,7 +82,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_update_lazy_attribute: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
update_update_lazy_attribute_field: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
)
)
end
Expand All @@ -83,7 +98,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(
destroy_update_lazy_attribute: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
destroy_update_lazy_attribute_field: contain_exactly(Esse::ActiveRecord::Callbacks::UpdateLazyAttribute, { attribute_name: :field, custom: 'value'}, an_instance_of(Proc)),
)
)
end
Expand Down

0 comments on commit 067b7e3

Please sign in to comment.