Module: DefaultWhere

Includes:
Group, Order, Params, Scope
Defined in:
lib/default_where.rb,
lib/default_where/group.rb,
lib/default_where/order.rb,
lib/default_where/scope.rb,
lib/default_where/params.rb,
lib/default_where/version.rb

Defined Under Namespace

Modules: Group, Order, Params, Scope

Constant Summary collapse

REJECT =
['', nil].freeze
STRIP =
true
VERSION =
'2.3.0'

Constants included from Order

Order::PATTERN

Constants included from Scope

Scope::PATTERN

Instance Method Summary collapse

Methods included from Group

#default_group

Methods included from Params

#default_where_params

Methods included from Order

#default_where_order, #default_where_order_filter

Methods included from Scope

#default_where_scope

Instance Method Details

#default_where(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/default_where.rb', line 17

def default_where(params = {})
  return all if params.blank?

  params = params.to_h
  options = {}
  [:strip, :allow, :reject].each do |key|
    options[key] = params.delete(key) if params[key].respond_to?(:to_hash)
  end
  or_params = {}
  or_params = params.delete(:or) if params[:or].respond_to?(:to_hash)

  and_params, and_refs, and_tables = default_where_params(params, options)
  order_params = default_where_order_filter(and_params)
  and_params.except!(*order_params.keys)

  or_params, or_refs, or_tables = default_where_params(or_params, options)
  refs = and_refs + or_refs
  tables = and_tables + or_tables

  includes(refs).default_where_and(and_params).default_where_or(or_params).default_where_order(order_params).references(tables)
end

#default_where_and(params = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/default_where.rb', line 39

def default_where_and(params = {})
  return current_scope if params.blank?

  equal_params = {}
  params.each do |key, _|
    equal_params[key] = params.delete(key) unless key.match? /[-\/]/
  end
  where_string, where_hash = default_where_scope(params)
  where_string = where_string.join ' AND '

  where(equal_params).where(where_string, where_hash)
end

#default_where_or(params = {}) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/default_where.rb', line 52

def default_where_or(params = {})
  return current_scope if params.blank?

  where_string, where_hash = default_where_scope(params)
  where_string = where_string.join ' OR '

  where(where_string, where_hash)
end

#loggerObject



61
62
63
# File 'lib/default_where.rb', line 61

def logger
  ::ActiveRecord::Base.logger
end