Module: ModelExplorer::Associations

Defined in:
lib/model_explorer/associations.rb,
lib/model_explorer/associations/base.rb,
lib/model_explorer/associations/many.rb,
lib/model_explorer/associations/singular.rb

Defined Under Namespace

Classes: Base, Many, Singular

Class Method Summary collapse

Class Method Details

.build(record, reflection, association) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/model_explorer/associations.rb', line 7

def self.build(record, reflection, association)
  case reflection&.macro
  when :has_many
    ModelExplorer::Associations::Many.new(record, reflection, association)
  when :has_one, :belongs_to
    ModelExplorer::Associations::Singular.new(record, reflection, association)
  else
    raise "Unknown association #{association[:name]}"
  end
end

.build_from_params(associations_params) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/model_explorer/associations.rb', line 18

def self.build_from_params(associations_params)
  associations = associations_params.dig("association_attributes", "associations") || {}

  associations.map do |_index, association_params|
    attributes = association_params["association_attributes"]

    {
      name: attributes["name"],
      scopes: attributes["scopes"],
      columns: attributes["columns"],
      associations: build_from_params(association_params)
    }
  end
end