Class: AzaharaSchema::AggregationAttribute

Inherits:
Attribute
  • Object
show all
Defined in:
lib/azahara_schema/aggregation_attribute.rb

Instance Attribute Summary collapse

Attributes inherited from Attribute

#format, #model, #name, #table_alias

Instance Method Summary collapse

Methods inherited from Attribute

#add_join, #add_preload, #add_sort, #add_statement, #aggregable?, #arel_join, #arel_sort_field, #arel_statement, #arel_table, #association_hash, #attribute_name, #available_operators, #available_values, #build_json_options!, #column?, #filter?, #filter_name, #path, #primary_key_name, #type

Constructor Details

#initialize(model, attribute) ⇒ AggregationAttribute

Returns a new instance of AggregationAttribute.



10
11
12
13
# File 'lib/azahara_schema/aggregation_attribute.rb', line 10

def initialize(model, attribute)
  @attribute = attribute
  super(model, 'sum:'+attribute.name, attribute.type)
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



8
9
10
# File 'lib/azahara_schema/aggregation_attribute.rb', line 8

def attribute
  @attribute
end

Instance Method Details

#arel_fieldObject



19
20
21
# File 'lib/azahara_schema/aggregation_attribute.rb', line 19

def arel_field
  attribute.arel_field.sum
end

#searchable?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/azahara_schema/aggregation_attribute.rb', line 15

def searchable?
  false
end

#value(parent) ⇒ Object



23
24
25
26
27
# File 'lib/azahara_schema/aggregation_attribute.rb', line 23

def value(parent)
  val = attribute.attribute.add_join( parent.send(attribute.association.name) ).sum(attribute.arel_field)
  val = BigDecimal(val.to_s.presence || 0) unless val.is_a?(BigDecimal)
  val
end