From ff995de217ed8e00ef3041ddd3d41d441bbfa41a Mon Sep 17 00:00:00 2001 From: "M.Shibuya" Date: Fri, 7 Jun 2019 19:41:45 +0900 Subject: [PATCH] Fix query and filter parsing value twice. Fixes #2755, Refs. #2971 --- lib/rails_admin/adapters/mongoid.rb | 1 - .../adapters/active_record_spec.rb | 36 +++++++++++++++++++ spec/rails_admin/adapters/mongoid_spec.rb | 36 +++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/lib/rails_admin/adapters/mongoid.rb b/lib/rails_admin/adapters/mongoid.rb index 8c7cc520f6..b42a6e0f43 100644 --- a/lib/rails_admin/adapters/mongoid.rb +++ b/lib/rails_admin/adapters/mongoid.rb @@ -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] ||= [] diff --git a/spec/rails_admin/adapters/active_record_spec.rb b/spec/rails_admin/adapters/active_record_spec.rb index 33e4514799..ac04283328 100644 --- a/spec/rails_admin/adapters/active_record_spec.rb +++ b/spec/rails_admin/adapters/active_record_spec.rb @@ -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 @@ -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 diff --git a/spec/rails_admin/adapters/mongoid_spec.rb b/spec/rails_admin/adapters/mongoid_spec.rb index a74afe74c1..b1927a652a 100644 --- a/spec/rails_admin/adapters/mongoid_spec.rb +++ b/spec/rails_admin/adapters/mongoid_spec.rb @@ -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 @@ -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