Module: Sakuramochi::PredicateBuilder::ClassMethods

Defined in:
lib/sakuramochi/predicate_builder.rb

Instance Method Summary collapse

Instance Method Details

#build_from_hash_with_predicate(engine, attributes, default_table) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sakuramochi/predicate_builder.rb', line 59

def build_from_hash_with_predicate(engine, attributes, default_table)
  attributes.map do |column, value|
    table = default_table

    if value.is_a?(Hash)
      table = Arel::Table.new(column, engine)
      build_from_hash_with_predicate(engine, value, table)
    else
      column = column.to_s

      if column.include?('.')
        table_name, column = column.split('.', 2)
        table = Arel::Table.new(table_name, engine)
      end 

      column_name, predicate = Sakuramochi::Predicate.detect(column.to_s)
      attribute = table[column_name.to_sym]

      if predicate
        build_with_predicate(attribute, value, predicate)
      else
        build(attribute, value)
      end
    end
  end.flatten
end