Class: YiffSpace::Search::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/yiffspace/search/query_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dsl, klass, relation, user) ⇒ QueryBuilder

param, db_field, type


9
10
11
12
13
14
15
16
# File 'lib/yiffspace/search/query_builder.rb', line 9

def initialize(dsl, klass, relation, user)
  dsl ||= []
  @dsl = dsl
  @klass = klass
  @relation = relation
  @user = user
  @q = relation
end

Instance Attribute Details

#dslObject (readonly)

Returns the value of attribute dsl.



6
7
8
# File 'lib/yiffspace/search/query_builder.rb', line 6

def dsl
  @dsl
end

#klassObject (readonly)

Returns the value of attribute klass.



6
7
8
# File 'lib/yiffspace/search/query_builder.rb', line 6

def klass
  @klass
end

#qObject (readonly)

Returns the value of attribute q.



6
7
8
# File 'lib/yiffspace/search/query_builder.rb', line 6

def q
  @q
end

#relationObject (readonly)

Returns the value of attribute relation.



6
7
8
# File 'lib/yiffspace/search/query_builder.rb', line 6

def relation
  @relation
end

#userObject (readonly)

Returns the value of attribute user.



6
7
8
# File 'lib/yiffspace/search/query_builder.rb', line 6

def user
  @user
end

Instance Method Details

#process_dsl(dsl, params) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/yiffspace/search/query_builder.rb', line 18

def process_dsl(dsl, params)
  dsl[:fields].each do |param, field, type = nil, block = nil, options = {}|
    value = params[param]
    multi = options.fetch(:multi, nil) || false
    like = options.fetch(:like, nil) || false
    ilike = options.fetch(:ilike, nil) || false
    normalize = options.fetch(:normalize, nil) || ->(value) { value }
    next if value.nil?

    value = normalize.call(value)
    @q = block.call(q) if block
    if type.is_a?(Proc)
      args = [q, value, user, params]
      @q = type.call(*args.first(type.arity))
      next
    end
    field ||= param
    if like
      @q = q.where.like(field => value)
      next
    elsif ilike
      @q = q.where.ilike(field => value)
      next
    end
    type ||= QueryHelper.get_column(field, klass.table_name)..type
    case type
    when :boolean
      @q = q.boolean_attribute_matches(field, value)
    when :integer
      # multiple comma separated values are implicitly supported
      # trimming them seems unneeded
      # value = value[0.. value.index(",") - 1] unless multi
      @q = q.numeric_attribute_matches(field, value)
    when :datetime
      @q = q.datetime_attribute_matches(field, value)
    when :text, :string
      value = value.split(",").first(YiffSpace.config.max_multi_count.call) if multi # explicitly supports arrays
      @q = q.text_attribute_matches(field, value)
    when :inet
      @q = q.ip_attribute_matches(field, value)
    when :present
      @q = q.attribute_present(field, value)
    else
      raise(ArgumentError, "Unknown type: #{type} for field: #{field}")
    end
  end

  dsl[:associations].each do |attribute, klass, nested_dsl, join|
    next if params[attribute].nil?

    @q = q.joins(join)
    nested_relation = klass.all
    nested_builder = QueryBuilder.new(nested_dsl, klass, nested_relation, user)
    nested_relation = nested_builder.search(params[attribute])
    @q = q.merge(nested_relation)
  end
end

#search(params = {}) ⇒ Object



76
77
78
79
80
# File 'lib/yiffspace/search/query_builder.rb', line 76

def search(params = {})
  params.transform_keys!(&:to_sym)
  process_dsl(dsl, params)
  q
end