Class: AttributesBuilder

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/activerecord-ejection_seat/attributes_builder.rb

Overview

Creates initialization payload for targeted ActiveRecord model

Instance Method Summary collapse

Constructor Details

#initialize(struct:, target_model:) ⇒ AttributesBuilder

Returns a new instance of AttributesBuilder.



9
10
11
12
# File 'lib/activerecord-ejection_seat/attributes_builder.rb', line 9

def initialize(struct:, target_model:)
  @struct = struct
  @target_model = target_model
end

Instance Method Details

#buildObject



15
16
17
18
19
20
# File 'lib/activerecord-ejection_seat/attributes_builder.rb', line 15

def build
  attrs = struct.serialize
  props = attrs.keys & target_model.column_names.map { |name| name.delete_suffix("_id") }

  attrs.slice(*props).to_h { |key, value| build_attribute(name: key, value: value) }
end