class AzaharaSchema::Attribute

Attributes

format[RW]
model[RW]
name[RW]
table_alias[RW]

Public Class Methods

new(model, name, type) click to toggle source
# File lib/azahara_schema/attribute.rb, line 7
def initialize(model, name, type)
  @name, @model = name, model
  @format = AzaharaSchema::FieldFormat.find(type)
end

Public Instance Methods

add_join(scope) click to toggle source
# File lib/azahara_schema/attribute.rb, line 103
def add_join(scope)
  scope
end
add_preload(scope) click to toggle source
# File lib/azahara_schema/attribute.rb, line 107
def add_preload(scope)
  scope
end
add_sort(scope, order) click to toggle source
# File lib/azahara_schema/attribute.rb, line 116
def add_sort(scope, order)
  scope.order( arel_sort_field.public_send(order) )
end
add_statement(scope, operator, values) click to toggle source
# File lib/azahara_schema/attribute.rb, line 111
def add_statement(scope, operator, values)
  values = [values] unless values.is_a?(Array)
  scope.where(arel_statement(operator, values))
end
aggregable?() click to toggle source
# File lib/azahara_schema/attribute.rb, line 63
def aggregable?
  format.aggregable?
end
arel_field(t_alias=self.table_alias) click to toggle source
# File lib/azahara_schema/attribute.rb, line 33
def arel_field(t_alias=self.table_alias)
  arel_table(t_alias)[filter_name]
end
arel_join(parent=nil, join_type=::Arel::Nodes::OuterJoin, a_tbl=self.arel_table(self.table_alias)) click to toggle source
# File lib/azahara_schema/attribute.rb, line 132
def arel_join(parent=nil, join_type=::Arel::Nodes::OuterJoin, a_tbl=self.arel_table(self.table_alias))
  parent
end
arel_sort_field() click to toggle source
# File lib/azahara_schema/attribute.rb, line 37
def arel_sort_field
  arel_field
end
arel_statement(operator, values) click to toggle source

values has to be array!

# File lib/azahara_schema/attribute.rb, line 76
def arel_statement(operator, values)
  values = values.collect{|v| format.sanitize_value(v) }
  case operator
  when '='
    condition = arel_field.in(values.compact) unless values.compact.empty?
    if values.include?(nil)
      c_nil = arel_field.eq(nil)
      condition = condition ? condition.or(c_nil) : c_nil
    end
    condition
  when '~'
    vals = values.collect{|v| v.split }.flatten
    arl = arel_field.matches("%#{vals[0]}%")
    vals[1..-1].each{|v| arl = arl.or( arel_field.matches("%#{v}%") ) }
    arl
  when '>='
    arel_field.gteq(values.map(&:to_f).min)
  when '<='
    arel_field.lteq(values.map(&:to_f).max)
  when '@>'
    require 'arel/azahara_postgres_exts'
    arel_field.contains(values)
  else
    throw 'Unknown operator ' + operator.to_s
  end
end
arel_table(t_alias=self.table_alias) click to toggle source
# File lib/azahara_schema/attribute.rb, line 29
def arel_table(t_alias=self.table_alias)
  t_alias ? model.arel_table.alias(t_alias) : model.arel_table
end
association_hash() click to toggle source
# File lib/azahara_schema/attribute.rb, line 128
def association_hash
  {}
end
attribute_name() click to toggle source
# File lib/azahara_schema/attribute.rb, line 124
def attribute_name
  AzaharaSchema::AttributeName.new(self)
end
available_operators() click to toggle source
# File lib/azahara_schema/attribute.rb, line 12
def available_operators
  format.available_operators
end
available_values() click to toggle source
# File lib/azahara_schema/attribute.rb, line 16
def available_values
  case type
  when 'list'
    @model.try(name.to_s.pluralize)
  else
    nil
  end
end
build_json_options!(options) click to toggle source
# File lib/azahara_schema/attribute.rb, line 120
def build_json_options!(options)
  options
end
column?() click to toggle source
# File lib/azahara_schema/attribute.rb, line 55
def column?
  true
end
filter?() click to toggle source
# File lib/azahara_schema/attribute.rb, line 59
def filter?
  true
end
filter_name() click to toggle source
# File lib/azahara_schema/attribute.rb, line 41
def filter_name
  name
end
path() click to toggle source

Path in json structure

# File lib/azahara_schema/attribute.rb, line 46
def path
  name
end
primary_key_name() click to toggle source

Name of the primary key attribute (same as column obviously)

# File lib/azahara_schema/attribute.rb, line 51
def primary_key_name
  model.primary_key
end
searchable?() click to toggle source
# File lib/azahara_schema/attribute.rb, line 67
def searchable?
  format.searchable?
end
type() click to toggle source
# File lib/azahara_schema/attribute.rb, line 25
def type
  format.format_name
end
value(record) click to toggle source
# File lib/azahara_schema/attribute.rb, line 71
def value(record)
  record.public_send(name)
end