Skip to content

Commit

Permalink
Fix query and filter parsing value twice. Fixes #2755, Refs. #2971
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Jun 7, 2019
1 parent 3105be3 commit ff995de
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
1 change: 0 additions & 1 deletion lib/rails_admin/adapters/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ def make_field_conditions(field, value, operator)
conditions_per_collection = {}
field.searchable_columns.each do |column_infos|
collection_name, column_name = parse_collection_name(column_infos[:column])
value = parse_field_value(field, value)
statement = build_statement(column_name, column_infos[:type], value, operator)
next unless statement
conditions_per_collection[collection_name] ||= []
Expand Down
36 changes: 36 additions & 0 deletions spec/rails_admin/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ class PlayerWithDefaultScope < Player
expect { abstract_model.all(query: 'foo') }.not_to raise_error
end
end

context 'when parsing is not idempotent' do
before do
RailsAdmin.config do |c|
c.model Team do
field :name do
def parse_value(value)
"#{value}s"
end
end
end
end
end

it 'parses value only once' do
expect(abstract_model.all(query: 'foo')).to match_array @teams[1]
end
end
end

describe '#filter_scope' do
Expand Down Expand Up @@ -181,6 +199,24 @@ class PlayerWithDefaultScope < Player
it 'makes correct query' do
expect(abstract_model.all(filters: {'name' => {'0000' => {o: 'like', v: 'foo'}}, 'division' => {'0001' => {o: 'like', v: 'bar'}}}, include: :division)).to eq([@teams[2]])
end

context 'when parsing is not idempotent' do
before do
RailsAdmin.config do |c|
c.model Team do
field :name do
def parse_value(value)
"some#{value}"
end
end
end
end
end

it 'parses value only once' do
expect(abstract_model.all(filters: {'name' => {'0000' => {o: 'like', v: 'where'}}})).to match_array @teams[2]
end
end
end

describe '#build_statement' do
Expand Down
36 changes: 36 additions & 0 deletions spec/rails_admin/adapters/mongoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,24 @@
it 'makes correct query' do
expect(@abstract_model.all(query: 'foo').to_a).to match_array @players[1..2]
end

context 'when parsing is not idempotent' do
before do
RailsAdmin.config do |c|
c.model Player do
field :name do
def parse_value(value)
"#{value}s"
end
end
end
end
end

it 'parses value only once' do
expect(@abstract_model.all(query: 'foo')).to match_array @players[1..1]
end
end
end

describe '#filter_scope' do
Expand All @@ -211,6 +229,24 @@
it 'makes correct query' do
expect(@abstract_model.all(filters: {'name' => {'0000' => {o: 'like', v: 'foo'}}, 'team' => {'0001' => {o: 'like', v: 'bar'}}})).to eq([@players[2]])
end

context 'when parsing is not idempotent' do
before do
RailsAdmin.config do |c|
c.model Player do
field :name do
def parse_value(value)
"#{value}s"
end
end
end
end
end

it 'parses value only once' do
expect(@abstract_model.all(filters: {'name' => {'0000' => {o: 'like', v: 'foo'}}})).to match_array @players[2]
end
end
end

describe '#build_statement' do
Expand Down

0 comments on commit ff995de

Please sign in to comment.