class AzaharaSchema::AssociationAttribute

The class is attribute for associated record, it is used for working with related records.


TODO: better way of joining the association - mandatory as joins others as left outer, but not includes.


The class holds schema for related entity.

Attributes

attribute[R]
schema[R]

Public Class Methods

new(model, association_schema, attribute) click to toggle source
Calls superclass method
# File lib/azahara_schema/association_attribute.rb, line 19
def initialize(model, association_schema, attribute)
  @schema = association_schema
  @attribute = attribute
  super(model, association.name.to_s+'-'+attribute.name, attribute.type)
end

Public Instance Methods

add_join(scope) click to toggle source
# File lib/azahara_schema/association_attribute.rb, line 85
def add_join(scope)
  # scope.left_outer_joins(association_hash)
  scope.joins(arel_join.join_sources)
end
add_preload(scope) click to toggle source
# File lib/azahara_schema/association_attribute.rb, line 90
def add_preload(scope)
  scope.preload(association_hash)
end
add_sort(scope, order) click to toggle source
Calls superclass method
# File lib/azahara_schema/association_attribute.rb, line 99
def add_sort(scope, order)
  super(add_join(scope), order)
end
add_statement(scope, operator, values) click to toggle source

TODO: heuristic for when add left outer join and when add inner join

Calls superclass method
# File lib/azahara_schema/association_attribute.rb, line 95
def add_statement(scope, operator, values)
  super(add_join(scope), operator, values)
end
arel_field() click to toggle source
# File lib/azahara_schema/association_attribute.rb, line 34
def arel_field
  attribute.arel_field
end
arel_join(parent=nil, join_type=::Arel::Nodes::OuterJoin, a_tbl=self.arel_table) click to toggle source
# File lib/azahara_schema/association_attribute.rb, line 67
def arel_join(parent=nil, join_type=::Arel::Nodes::OuterJoin, a_tbl=self.arel_table)
  parent ||= self.arel_table(nil)
  joined = parent
  case association.macro
  when :has_many, :has_one
    joined = parent.join(attribute.arel_table, join_type).on( attribute.arel_table[association.foreign_key].eq( a_tbl[model.primary_key] ) )
  when :belongs_to
    joined = parent.join(attribute.arel_table, join_type).on( attribute.arel_table[attribute.model.primary_key].eq( a_tbl[association.foreign_key] ) )
  else
    raise 'Unknown macro ' + association.macro.to_s
  end
  attribute.arel_join( joined, join_type )
end
arel_statement(operator, values) click to toggle source
# File lib/azahara_schema/association_attribute.rb, line 81
def arel_statement(operator, values)
  attribute.arel_statement(operator, values)
end
association_hash() click to toggle source
# File lib/azahara_schema/association_attribute.rb, line 63
def association_hash
  { association.name => attribute.association_hash }
end
available_values() click to toggle source
# File lib/azahara_schema/association_attribute.rb, line 30
def available_values
  attribute.available_values
end
base_schema() click to toggle source

Goes to the last level, for attribute base schema

# File lib/azahara_schema/association_attribute.rb, line 26
def base_schema
  attribute.try(:schema) || schema
end
build_json_options!(options) click to toggle source
# File lib/azahara_schema/association_attribute.rb, line 103
def build_json_options!(options)
  options[:include] ||= {}
  options[:include][association.name.to_sym] ||= {}
  attribute.build_json_options!(options[:include][association.name.to_sym])
  options
end
column?() click to toggle source
# File lib/azahara_schema/association_attribute.rb, line 46
def column?
  association.macro == :belongs_to && attribute.column?
end
path() click to toggle source
# File lib/azahara_schema/association_attribute.rb, line 42
def path
  association.name.to_s+'.'+attribute.path
end
primary_key_name() click to toggle source
# File lib/azahara_schema/association_attribute.rb, line 38
def primary_key_name
  association.name.to_s+'-'+attribute.primary_key_name
end
searchable?() click to toggle source
# File lib/azahara_schema/association_attribute.rb, line 50
def searchable?
  false
end
value(parent) click to toggle source
# File lib/azahara_schema/association_attribute.rb, line 54
def value(parent)
  if association.macro == :has_many
    parent.public_send(association.name).collect{|record| attribute.value( record )}.flatten
  else
    entity = parent.public_send(association.name)
    attribute.value( entity ) if entity
  end
end