From 2c6ba0c45f6d664db3fd2441f122bfdf7e444980 Mon Sep 17 00:00:00 2001 From: Mikhail Titov Date: Wed, 27 Sep 2017 22:41:23 -0500 Subject: [PATCH] Rails 5: use ActionController::Parameters instead of hash http://blog.bigbinary.com/2016/07/25/parameters-no-longer-inherit-from-hash-with-indifferent-access-in-rails-5.html --- lib/tabulatr/data/filtering.rb | 2 +- spec/lib/tabulatr/data/data_spec.rb | 6 +++--- spec/lib/tabulatr/data/filtering_spec.rb | 18 +++++++++--------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/tabulatr/data/filtering.rb b/lib/tabulatr/data/filtering.rb index c3f1878..9abc163 100644 --- a/lib/tabulatr/data/filtering.rb +++ b/lib/tabulatr/data/filtering.rb @@ -43,7 +43,7 @@ def apply_search(query) def apply_filters(filter_params) return unless filter_params - filter_params.permit!.to_hash.with_indifferent_access.each do |param| + filter_params.each do |param| name, value = param next unless value.present? diff --git a/spec/lib/tabulatr/data/data_spec.rb b/spec/lib/tabulatr/data/data_spec.rb index 50a971f..a029068 100644 --- a/spec/lib/tabulatr/data/data_spec.rb +++ b/spec/lib/tabulatr/data/data_spec.rb @@ -17,7 +17,7 @@ it 'prefilters the result' do td = Tabulatr::Data.new(Product.where(price: 10)) - td.data_for_table(example_params) + td.data_for_table(ActionController::Parameters.new(example_params)) expect(td.instance_variable_get('@relation').to_sql).to match(/.+WHERE \"products\".\"price\" = 10.+/) end @@ -25,7 +25,7 @@ Product.create([{title: 'foo', price: 5}, {title: 'bar', price: 10}, {title: 'fuzz', price: 7}]) td = Tabulatr::Data.new(Product) - records = td.data_for_table(HashWithIndifferentAccess.new(example_params.merge(product_sort: 'title DESC'))) + records = td.data_for_table(ActionController::Parameters.new(example_params.merge(product_sort: 'title DESC'))) expect(records.count).to eql 3 titles = ['fuzz', 'foo', 'bar'] # raise records.inspect @@ -43,7 +43,7 @@ Product.where(id: ids).destroy_all end }) - td.data_for_table(HashWithIndifferentAccess.new(example_params.merge(product_batch: 'delete', 'tabulatr_checked' => {'checked_ids' => ''}))) + td.data_for_table(ActionController::Parameters.new(example_params.merge(product_batch: 'delete', 'tabulatr_checked' => {'checked_ids' => ''}))) expect(Product.where(active: true).count).to be 0 expect(Product.count).to be 1 end diff --git a/spec/lib/tabulatr/data/filtering_spec.rb b/spec/lib/tabulatr/data/filtering_spec.rb index 4d6e081..5f5d7d9 100644 --- a/spec/lib/tabulatr/data/filtering_spec.rb +++ b/spec/lib/tabulatr/data/filtering_spec.rb @@ -29,7 +29,7 @@ def table_name_for_association(assoc); nil; end it "filters for 'today'" do fake_obj = double() allow(fake_obj).to receive_message_chain('col_options.filter_sql') { 'publish_at'} - @dummy.apply_date_condition(fake_obj, {simple: 'today'}) + @dummy.apply_date_condition(fake_obj, {'simple'=>'today'}) result = @dummy.instance_variable_get('@relation') expect(result.count).to be 1 expect(result[0].id).to be @today.id @@ -38,7 +38,7 @@ def table_name_for_association(assoc); nil; end it "filters for 'yesterday'" do fake_obj = double() allow(fake_obj).to receive_message_chain('col_options.filter_sql') { 'publish_at'} - @dummy.apply_date_condition(fake_obj, {simple: 'yesterday'}) + @dummy.apply_date_condition(fake_obj, {'simple'=>'yesterday'}) result = @dummy.instance_variable_get('@relation') expect(result.count).to be 1 expect(result[0].id).to be @yesterday.id @@ -47,7 +47,7 @@ def table_name_for_association(assoc); nil; end it "filters for 'this week'" do fake_obj = double() allow(fake_obj).to receive_message_chain('col_options.filter_sql') { 'publish_at'} - @dummy.apply_date_condition(fake_obj, {simple: 'this_week'}) + @dummy.apply_date_condition(fake_obj, {'simple'=>'this_week'}) result = @dummy.instance_variable_get('@relation') expect(result.count).to be 4 expect(result.map(&:id).sort).to eq [@yesterday.id, @today.id, @week_one.id, @week_two.id].sort @@ -56,7 +56,7 @@ def table_name_for_association(assoc); nil; end it "filters for 'last 7 days'" do fake_obj = double() allow(fake_obj).to receive_message_chain('col_options.filter_sql') { 'publish_at' } - @dummy.apply_date_condition(fake_obj, {simple: 'last_7_days'}) + @dummy.apply_date_condition(fake_obj, {'simple'=>'last_7_days'}) result = @dummy.instance_variable_get('@relation') expect(result.map(&:id).sort).to eq ([@last_seven_days.id, @yesterday.id, @today.id, @week_one.id].sort) end @@ -64,7 +64,7 @@ def table_name_for_association(assoc); nil; end it "filters for 'this month'" do fake_obj = double() allow(fake_obj).to receive_message_chain('col_options.filter_sql') { 'publish_at'} - @dummy.apply_date_condition(fake_obj, {simple: 'this_month'}) + @dummy.apply_date_condition(fake_obj, {'simple'=>'this_month'}) result = @dummy.instance_variable_get('@relation') expect(result.map(&:id).sort).to eq ([@today.id, @week_two.id, @this_month.id]) end @@ -72,7 +72,7 @@ def table_name_for_association(assoc); nil; end it "filters for 'last 30 days'" do fake_obj = double() allow(fake_obj).to receive_message_chain('col_options.filter_sql') { 'publish_at'} - @dummy.apply_date_condition(fake_obj, {simple: 'last_30_days'}) + @dummy.apply_date_condition(fake_obj, {'simple'=>'last_30_days'}) result = @dummy.instance_variable_get('@relation') expect(result.map(&:id).sort).to eq ([ @last_thirty_days.id, @yesterday.id, @last_seven_days.id, @today.id, @@ -83,15 +83,15 @@ def table_name_for_association(assoc); nil; end fake_obj = double() allow(fake_obj).to receive_message_chain('col_options.filter_sql') { 'publish_at'} @dummy.apply_date_condition(fake_obj, { - simple: 'from_to', from: '31.12.2013 15:00', - to: '15.01.2014 00:00'}) + 'simple'=>'from_to', 'from'=>'31.12.2013 15:00', + 'to'=>'15.01.2014 00:00'}) result = @dummy.instance_variable_get('@relation') expect(result.map(&:id)).to eq ([@yesterday.id, @today.id, @week_two.id].sort) end it "exits early if condition is 'none'" do relation_before = @dummy.instance_variable_get('@relation') - @dummy.apply_date_condition(nil, {simple: 'none'}) + @dummy.apply_date_condition(nil, {'simple'=>'none'}) relation_after = @dummy.instance_variable_get('@relation') expect(relation_after).to eq relation_before end