Class: AzaharaSchema::DerivedAttribute
Overview
The class is attribute for derivateve attribute, it is used for working with combination of attributes as one attribute.
Instance Attribute Summary collapse
Attributes inherited from Attribute
#format, #model, #name, #table_alias
Instance Method Summary
collapse
Methods inherited from Attribute
#add_sort, #aggregable?, #arel_sort_field, #arel_table, #association_hash, #attribute_name, #available_operators, #available_values, #build_json_options!, #column?, #filter?, #filter_name, #path, #primary_key_name, #searchable?
Constructor Details
#initialize(model, name, derivation_method, *attribute_names, **options) ⇒ DerivedAttribute
Returns a new instance of DerivedAttribute.
13
14
15
16
17
18
|
# File 'lib/azahara_schema/derived_attribute.rb', line 13
def initialize(model, name, derivation_method, *attribute_names, **options)
self.attribute_names = attribute_names
self.derivation_method = derivation_method.to_sym
@options = options
super(model, name, type)
end
|
Instance Attribute Details
#attribute_names ⇒ Object
Returns the value of attribute attribute_names.
11
12
13
|
# File 'lib/azahara_schema/derived_attribute.rb', line 11
def attribute_names
@attribute_names
end
|
#derivation_method ⇒ Object
Returns the value of attribute derivation_method.
10
11
12
|
# File 'lib/azahara_schema/derived_attribute.rb', line 10
def derivation_method
@derivation_method
end
|
Instance Method Details
#add_join(scope) ⇒ Object
89
90
91
92
|
# File 'lib/azahara_schema/derived_attribute.rb', line 89
def add_join(scope)
attributes.each{|a| scope = a.add_join(scope) }
scope
end
|
#add_preload(scope) ⇒ Object
94
95
96
97
|
# File 'lib/azahara_schema/derived_attribute.rb', line 94
def add_preload(scope)
attributes.each{|a| scope = a.add_preload(scope) }
scope
end
|
#add_statement(scope, operator, values) ⇒ Object
78
79
80
|
# File 'lib/azahara_schema/derived_attribute.rb', line 78
def add_statement(scope, operator, values)
super(add_join(scope), operator, values)
end
|
#arel_field(t_alias = nil) ⇒ Object
do not alias tables - let attributes derived from handle that
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/azahara_schema/derived_attribute.rb', line 52
def arel_field(t_alias=nil)
case derivation_method
when :concat
arel_fields = attributes.collect{|a| a.arel_field}
(1..arel_fields.length-1).to_a.reverse.each{|i| arel_fields.insert(i, Arel::Nodes::SqlLiteral.new("'#{concat_divider}'")) }
Arel::Nodes::NamedFunction.new 'CONCAT', arel_fields
else
raise "DerivedAttribute(#{name}) - derivation_method '#{derivation_method}' is not supported"
end
end
|
#arel_join(parent = nil, join_type = ::Arel::Nodes::OuterJoin, a_tbl = self.arel_table(self.table_alias)) ⇒ Object
82
83
84
85
86
87
|
# File 'lib/azahara_schema/derived_attribute.rb', line 82
def arel_join(parent=nil, join_type=::Arel::Nodes::OuterJoin, a_tbl=self.arel_table(self.table_alias))
parent ||= self.arel_table(nil)
joined = parent
attributes.each{|a| joined = a.arel_join(joined, join_type) }
joined
end
|
#arel_statement(operator, values) ⇒ Object
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/azahara_schema/derived_attribute.rb', line 67
def arel_statement(operator, values)
case operator
when '~'
arl = attributes[0].arel_statement(operator, values)
attributes[1..-1].each{|att| arl = arl.or( att.arel_statement(operator, values) ) }
arl
else
super
end
end
|
#attributes ⇒ Object
28
29
30
|
# File 'lib/azahara_schema/derived_attribute.rb', line 28
def attributes
@attributes ||= options[:attributes] || options[:schema].available_attributes_hash.slice(*attribute_names).values
end
|
#concat_divider ⇒ Object
32
33
34
|
# File 'lib/azahara_schema/derived_attribute.rb', line 32
def concat_divider
options[:divider] || ' '
end
|
#options ⇒ Object
36
37
38
|
# File 'lib/azahara_schema/derived_attribute.rb', line 36
def options
@options || {}
end
|
#type ⇒ Object
——————-| OVERRIDES |———————
42
43
44
45
46
47
48
49
|
# File 'lib/azahara_schema/derived_attribute.rb', line 42
def type
case derivation_method
when :concat
'string'
else
raise "DerivedAttribute(#{name}) - derivation_method '#{derivation_method}' is not supported"
end
end
|
#value(record) ⇒ Object
63
64
65
|
# File 'lib/azahara_schema/derived_attribute.rb', line 63
def value(record)
record.try(name) || attributes.collect{|a| a.value(record) }.join(concat_divider)
end
|