Class: Esse::ActiveRecord::Callbacks::UpdateLazyAttribute

Inherits:
Esse::ActiveRecord::Callback show all
Defined in:
lib/esse/active_record/callbacks/update_lazy_attribute.rb

Instance Attribute Summary collapse

Attributes inherited from Esse::ActiveRecord::Callback

#block_result, #options, #repo

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name:, **kwargs, &block) ⇒ UpdateLazyAttribute

Returns a new instance of UpdateLazyAttribute.



8
9
10
11
# File 'lib/esse/active_record/callbacks/update_lazy_attribute.rb', line 8

def initialize(attribute_name:, **kwargs, &block)
  @attribute_name = attribute_name
  super(**kwargs, &block)
end

Instance Attribute Details

#attribute_nameObject (readonly)

Returns the value of attribute attribute_name.



6
7
8
# File 'lib/esse/active_record/callbacks/update_lazy_attribute.rb', line 6

def attribute_name
  @attribute_name
end

Instance Method Details

#call(model) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/esse/active_record/callbacks/update_lazy_attribute.rb', line 13

def call(model)
  related_ids = Esse::ArrayUtils.wrap(block_result || model.id)
  return true if related_ids.empty?

  repo.update_documents_attribute(attribute_name, related_ids, options)

  true
end