Skip to content

Commit

Permalink
Rails 5: use ActionController::Parameters instead of hash
Browse files Browse the repository at this point in the history
  • Loading branch information
mlt committed Oct 4, 2017
1 parent dc9fb63 commit 2c6ba0c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/tabulatr/data/filtering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/tabulatr/data/data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
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

it 'uses default order' do
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
Expand All @@ -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
Expand Down
18 changes: 9 additions & 9 deletions spec/lib/tabulatr/data/filtering_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -56,23 +56,23 @@ 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

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

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,
Expand All @@ -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
Expand Down

0 comments on commit 2c6ba0c

Please sign in to comment.