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, allow_table_name = true) ⇒ Object



53
54
55
56
57
58
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
85
# File 'lib/sakuramochi/predicate_builder.rb', line 53

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

    if allow_table_name && value.is_a?(Hash)
      table = Arel::Table.new(column, engine)

      if value.empty?
        '1 = 2'
      else
        build_from_hash_with_predicate(engine, value, table, false)
      end
    else
      column = column.to_s

      if allow_table_name && 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

  predicates.flatten
end