class Dbee::Dsl::AssociationBuilder

This is really syntactic sugar built on top of Association. It can handle two main use-cases when declaring associations:

Attributes

inflector[R]

Public Class Methods

new(inflector) click to toggle source
# File lib/dbee/dsl/association_builder.rb, line 19
def initialize(inflector)
  raise ArgumentError, 'inflector is required' unless inflector

  @inflector = inflector

  freeze
end

Public Instance Methods

child_association(on_class_name, name, opts = {}) click to toggle source
# File lib/dbee/dsl/association_builder.rb, line 36
def child_association(on_class_name, name, opts = {})
  reference_constraint = {
    name: opts[:foreign_key] || inflector.foreign_key(on_class_name),
    parent: opts[:primary_key] || :id
  }

  association(on_class_name, name, opts, reference_constraint)
end
parent_association(on_class_name, name, opts = {}) click to toggle source
# File lib/dbee/dsl/association_builder.rb, line 27
def parent_association(on_class_name, name, opts = {})
  reference_constraint = {
    name: opts[:foreign_key] || :id,
    parent: opts[:primary_key] || inflector.foreign_key(name)
  }

  association(on_class_name, name, opts, reference_constraint)
end

Private Instance Methods

array(value) click to toggle source
# File lib/dbee/dsl/association_builder.rb, line 60
def array(value)
  value.is_a?(Hash) ? [value] : Array(value)
end
association(on_class_name, name, opts, reference_constraint) click to toggle source
# File lib/dbee/dsl/association_builder.rb, line 47
def association(on_class_name, name, opts, reference_constraint)
  association_opts = {
    model: opts[:model],
    constraints: [reference_constraint] + make_constraints(opts)
  }

  Association.new(on_class_name, inflector, name, association_opts)
end
make_constraints(opts = {}) click to toggle source
# File lib/dbee/dsl/association_builder.rb, line 56
def make_constraints(opts = {})
  array(opts[:constraints]) + array(opts[:static]).map { |c| c.merge(type: :static) }
end