Sha256: b593f616191f05ecc7a3b89ffe27eb3965efdc509a3631bbd1dc9c9c6336e366

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

module ModelExplorer
  module Associations
    autoload :Base, "model_explorer/associations/base"
    autoload :Many, "model_explorer/associations/many"
    autoload :Singular, "model_explorer/associations/singular"

    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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
model_explorer-0.1.0 lib/model_explorer/associations.rb