Class: MetaWhere::Visitors::Attribute

Inherits:
Visitor
  • Object
show all
Defined in:
lib/meta_where/visitors/attribute.rb

Instance Attribute Summary

Attributes inherited from Visitor

#join_dependency, #tables

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Visitor

#accept, #can_accept?, #get_table, #initialize

Constructor Details

This class inherits a constructor from MetaWhere::Visitors::Visitor

Class Method Details

.visitablesObject



7
8
9
# File 'lib/meta_where/visitors/attribute.rb', line 7

def self.visitables
  [Hash, Symbol, MetaWhere::Column]
end

Instance Method Details

#visit_Hash(o, parent) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/meta_where/visitors/attribute.rb', line 11

def visit_Hash(o, parent)
  parent = parent.name if parent.is_a? MetaWhere::JoinType
  table = tables[parent]
  built_attributes = o.map do |column, value|
    if value.is_a?(Hash)
      association = association_from_parent_and_column(parent, column)
      accept(value, association || column)
    elsif value.is_a?(Array) && value.all? {|v| can_accept?(v)}
      association = association_from_parent_and_column(parent, column)
      value.map {|val| self.accept(val, association || column)}
    else
      association = association_from_parent_and_column(parent, column)
      can_accept?(value) ? self.accept(value, association || column) : value
    end
  end

  built_attributes.flatten
end

#visit_MetaWhere_Column(o, parent) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/meta_where/visitors/attribute.rb', line 40

def visit_MetaWhere_Column(o, parent)
  column_name = o.column.to_s
  if column_name.include?('.')
    table_name, column_name = column_name.split('.', 2)
    table = Arel::Table.new(table_name, :engine => parent.arel_engine)
  else
    table = tables[parent]
  end

  unless attribute = table[column_name]
    raise ::ActiveRecord::StatementInvalid, "No attribute named `#{column_name}` exists for table `#{table.name}`"
  end

  attribute.send(o.method)
end

#visit_Symbol(o, parent) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/meta_where/visitors/attribute.rb', line 30

def visit_Symbol(o, parent)
  table = tables[parent]

  unless attribute = table[o]
    raise ::ActiveRecord::StatementInvalid, "No attribute named `#{o}` exists for table `#{table.name}`"
  end

  attribute
end