Class: ExtractAssociatedObject

Inherits:
Object
  • Object
show all
Defined in:
lib/extract_associated_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, associated_object = []) ⇒ ExtractAssociatedObject

Returns a new instance of ExtractAssociatedObject.



8
9
10
11
12
# File 'lib/extract_associated_object.rb', line 8

def initialize(object, associated_object = [])
  @object = object[0]
  @associated_object = associated_object
  @schema_tables = ActiveRecord::Base.connection.tables
end

Instance Attribute Details

#associated_objectObject (readonly)

Returns the value of attribute associated_object.



6
7
8
# File 'lib/extract_associated_object.rb', line 6

def associated_object
  @associated_object
end

#objectObject (readonly)

Returns the value of attribute object.



6
7
8
# File 'lib/extract_associated_object.rb', line 6

def object
  @object
end

#schema_tablesObject (readonly)

Returns the value of attribute schema_tables.



6
7
8
# File 'lib/extract_associated_object.rb', line 6

def schema_tables
  @schema_tables
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/extract_associated_object.rb', line 14

def call
  associated_tables_names.each do |table, column|
    value = object[column]
    next if value.nil?

    result = ActiveRecord::Base.connection.execute("SELECT * FROM #{table} WHERE id='#{value}'").to_a
    result[0].delete('updated_at')
    result[0].delete('created_at')
    associated_object << { table: table, attributes: result }
    ExtractAssociatedObject.new(result, associated_object).call
  end
  CreateFactory.call(associated_object)
end