Class: RailsAdmin::Adapters::ActiveRecord::WhereBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_admin/adapters/active_record.rb

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ WhereBuilder

Returns a new instance of WhereBuilder


88
89
90
91
92
93
# File 'lib/rails_admin/adapters/active_record.rb', line 88

def initialize(scope)
  @statements = []
  @values = []
  @tables = []
  @scope = scope
end

Instance Method Details

#add(field, value, operator) ⇒ Object


95
96
97
98
99
100
101
102
103
104
# File 'lib/rails_admin/adapters/active_record.rb', line 95

def add(field, value, operator)
  field.searchable_columns.flatten.each do |column_infos|
    statement, value1, value2 = StatementBuilder.new(column_infos[:column], column_infos[:type], value, operator).to_statement
    @statements << statement if statement.present?
    @values << value1 unless value1.nil?
    @values << value2 unless value2.nil?
    table, column = column_infos[:column].split('.')
    @tables.push(table) if column
  end
end

#buildObject


106
107
108
109
110
# File 'lib/rails_admin/adapters/active_record.rb', line 106

def build
  scope = @scope.where(@statements.join(' OR '), *@values)
  scope = scope.references(*(@tables.uniq)) if @tables.any?
  scope
end