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.



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

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

Instance Method Details

#add(field, value, operator) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/rails_admin/adapters/active_record.rb', line 112

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, @scope.connection.adapter_name).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



123
124
125
126
127
# File 'lib/rails_admin/adapters/active_record.rb', line 123

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