Class: AzaharaSchema::AssociationAttribute
- Inherits:
-
Attribute
- Object
- Attribute
- AzaharaSchema::AssociationAttribute
show all
- Defined in:
- lib/azahara_schema/association_attribute.rb
Overview
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.
Instance Attribute Summary collapse
Attributes inherited from Attribute
#format, #model, #name, #table_alias
Instance Method Summary
collapse
Methods inherited from Attribute
#aggregable?, #arel_sort_field, #arel_table, #attribute_name, #available_operators, #filter?, #filter_name, #type
Constructor Details
#initialize(model, association_schema, attribute) ⇒ AssociationAttribute
Returns a new instance of AssociationAttribute.
19
20
21
22
23
|
# 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
|
Instance Attribute Details
#attribute ⇒ Object
Returns the value of attribute attribute.
15
16
17
|
# File 'lib/azahara_schema/association_attribute.rb', line 15
def attribute
@attribute
end
|
#schema ⇒ Object
Returns the value of attribute schema.
15
16
17
|
# File 'lib/azahara_schema/association_attribute.rb', line 15
def schema
@schema
end
|
Instance Method Details
#add_join(scope) ⇒ Object
85
86
87
88
|
# File 'lib/azahara_schema/association_attribute.rb', line 85
def add_join(scope)
scope.joins(arel_join.join_sources)
end
|
#add_preload(scope) ⇒ Object
90
91
92
|
# File 'lib/azahara_schema/association_attribute.rb', line 90
def add_preload(scope)
scope.preload(association_hash)
end
|
#add_sort(scope, order) ⇒ Object
99
100
101
|
# 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) ⇒ Object
TODO: heuristic for when add left outer join and when add inner join
95
96
97
|
# File 'lib/azahara_schema/association_attribute.rb', line 95
def add_statement(scope, operator, values)
super(add_join(scope), operator, values)
end
|
#arel_field ⇒ Object
34
35
36
|
# 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) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# 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) ⇒ Object
81
82
83
|
# File 'lib/azahara_schema/association_attribute.rb', line 81
def arel_statement(operator, values)
attribute.arel_statement(operator, values)
end
|
#association_hash ⇒ Object
63
64
65
|
# File 'lib/azahara_schema/association_attribute.rb', line 63
def association_hash
{ association.name => attribute.association_hash }
end
|
#available_values ⇒ Object
30
31
32
|
# File 'lib/azahara_schema/association_attribute.rb', line 30
def available_values
attribute.available_values
end
|
#base_schema ⇒ Object
Goes to the last level, for attribute base schema
26
27
28
|
# File 'lib/azahara_schema/association_attribute.rb', line 26
def base_schema
attribute.try(:schema) || schema
end
|
#build_json_options!(options) ⇒ Object
103
104
105
106
107
108
|
# 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? ⇒ Boolean
46
47
48
|
# File 'lib/azahara_schema/association_attribute.rb', line 46
def column?
association.macro == :belongs_to && attribute.column?
end
|
#path ⇒ Object
42
43
44
|
# File 'lib/azahara_schema/association_attribute.rb', line 42
def path
association.name.to_s+'.'+attribute.path
end
|
#primary_key_name ⇒ Object
38
39
40
|
# File 'lib/azahara_schema/association_attribute.rb', line 38
def primary_key_name
association.name.to_s+'-'+attribute.primary_key_name
end
|
#searchable? ⇒ Boolean
50
51
52
|
# File 'lib/azahara_schema/association_attribute.rb', line 50
def searchable?
false
end
|
#value(parent) ⇒ Object
54
55
56
57
58
59
60
61
|
# 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
|