Module: YiffSpace::Search::QueryHelper

Defined in:
lib/yiffspace/search/query_helper.rb

Class Method Summary collapse

Class Method Details

.get_column(attribute, table = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/yiffspace/search/query_helper.rb', line 31

def get_column(attribute, table = nil)
  attribute = attribute.to_s
  if attribute.include?(".")
    table, column = attribute.split(".", 2)
  else
    column = attribute
  end
  raise(ArgumentError, "Missing table") if table.nil?

  c = ActiveRecord::Base.connection.columns(table).find { |c| c.name == column }
  raise(StandardError, "Column #{column} does not exist in table #{table}") unless c

  c
rescue ActiveRecord::StatementInvalid
  raise(StandardError, "Table #{table} does not exist")
end

.parse_conditions(conditions, model, table_name = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/yiffspace/search/query_helper.rb', line 8

def parse_conditions(conditions, model, table_name = nil)
  pairs = []

  conditions.each do |key, value|
    case key
    when String, Symbol
      key = key.to_s
      if value.is_a?(Hash)
        pairs.concat(parse_conditions(value, model, key))
      elsif key.include?(".")
        table, col = key.split(".", 2)
        pairs << [[table, col], value]
      else
        pairs << [[table_name || model.table_name, key], value]
      end
    else
      raise(ArgumentError, "Unsupported key type: #{key.class}")
    end
  end

  pairs
end