Class: BaseQueryBuilder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
app/queries/base_query_builder.rb

Overview

Query object: base query builder.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, options = {}) ⇒ BaseQueryBuilder

Returns a new instance of BaseQueryBuilder.

Parameters:

  • query (ActiveRecord::Relation, Class)

    base query

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :table_prefix (String)

    table name prefix (defaults
    to the query's table name)

  • :join_table_key (String)

    foreign key column used in
    joins (defaults to "<table_prefix.singularize>_id")



16
17
18
19
20
21
# File 'app/queries/base_query_builder.rb', line 16

def initialize(query, options = {})
  @query = query
  @table_prefix = options[:table_prefix] || @query.table_name
  @join_table_key = options[:join_table_key] || "#{@table_prefix.singularize}_id"
  @options = options
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



6
7
8
# File 'app/queries/base_query_builder.rb', line 6

def query
  @query
end

Instance Method Details

#array_check(array_column, value, exclusion_column = nil) ⇒ Object (protected)



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
# File 'app/queries/base_query_builder.rb', line 25

def array_check(array_column, value, exclusion_column = nil)
  exclusion_column ||= "#{@table_prefix}.exclude_#{array_column}".pluralize
  array_splat = [value].compact.map(&:presence).flatten.compact
  if array_splat.present?
    array_value_expression = ActiveRecord::Base.escape_sql("ARRAY[?]::character varying[]", array_splat)
    reflect(query.where("(
                           (#{exclusion_column} is null or #{exclusion_column} = false)
                           AND
                           (
                             #{array_value_expression} && #{@table_prefix}.#{array_column}
                             OR #{@table_prefix}.#{array_column} = '{}'
                             OR #{@table_prefix}.#{array_column} IS NULL
                           )
                         )
                         OR
                         (
                           #{exclusion_column} = true
                           AND
                           (
                             NOT ( #{array_value_expression} && #{@table_prefix}.#{array_column} )
                             OR #{@table_prefix}.#{array_column} = '{}'
                             OR #{@table_prefix}.#{array_column} IS NULL
                           )
                         )"))
  else
    reflect(query.where(array_column.to_sym => []))
  end
end

#habtm_check(value, join_table, foreign_key, exclusion_column = nil) ⇒ Object (protected)

When we check for conditions we need to check if the
association has the value in it, if the association doesn't
have the value in it if its set to exclude
or if the association as no values at all which is
implicit to accept all the values



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 'app/queries/base_query_builder.rb', line 59

def habtm_check(value, join_table, foreign_key, exclusion_column = nil)
  exclusion_column ||= "#{@table_prefix}.exclude_#{foreign_key}".pluralize
  sql_filter = "
    (
      /* non exclusion detection, has to have the value in question */
      (#{exclusion_column} is null or #{exclusion_column} = false)
      and
      exists(select 1 from #{join_table}
             where #{join_table}.#{foreign_key} = :value
             and #{join_table}.#{@join_table_key} = #{@table_prefix}.id )

    )
    or /* exclusion detection, can't use filters matching this value */
    (
      #{exclusion_column} = true
      and
      not exists(select 1 from #{join_table}
          where #{join_table}.#{foreign_key} = :value
          and #{join_table}.#{@join_table_key} = #{@table_prefix}.id )

    )
    or /* no criteria specified so it doesn't matter */
    not exists(select 1 from #{join_table}
         where #{join_table}.#{@join_table_key} = #{@table_prefix}.id )
  "
  reflect(query.where(sql_filter, value: value))
end

#reflect(query) ⇒ Object (protected)



87
88
89
# File 'app/queries/base_query_builder.rb', line 87

def reflect(query)
  self.class.new(query)
end