Module: ObjectState::Owner

Extended by:
ActiveSupport::Concern
Defined in:
lib/object_state/owner.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#assign_attributes_from_state_hash(attrs) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/object_state/owner.rb', line 37

def assign_attributes_from_state_hash(attrs)
  key = respond_to?(:model_name) ? model_name.singular : self.class.to_s.underscore
  return unless model_attrs = attrs.stringify_keys[key]
  model_id = model_attrs.stringify_keys['id'] || model_attrs.stringify_keys['_id']
  return if respond_to?(:id) && model_id.to_s != id.to_s
  object_state(model_attrs.except(*%i(_id id))).try(:update_model!)
end

#object_state(attrs = {}) ⇒ Object



29
30
31
# File 'lib/object_state/owner.rb', line 29

def object_state(attrs = {})
  self.class.object_state_class.new(self, attrs)
end

#to_object_state_hash(attrs = {}) ⇒ Object



33
34
35
# File 'lib/object_state/owner.rb', line 33

def to_object_state_hash(attrs = {})
  object_state(attrs).try(:to_hash)
end