From 752b986bb9042642a5887de8242fceb39d80552e Mon Sep 17 00:00:00 2001 From: jonatack Date: Sat, 31 Jan 2015 00:33:28 +0100 Subject: [PATCH] Follow-up to #505 - simplify the spec ransacker in schema.rb - clarify the spec and increase the coverage - move the spec into a shared context with similar specs - revert the .gitignore change in the previous commit --- .gitignore | 1 - .../adapters/active_record/base_spec.rb | 38 +++++++++---------- spec/support/schema.rb | 15 ++------ 3 files changed, 23 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 4613d48..4040c6c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,3 @@ .bundle Gemfile.lock pkg/* -.idea diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index e27fd18..1a4f8ca 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -3,7 +3,6 @@ require 'spec_helper' module Ransack module Adapters module ActiveRecord - describe Base do subject { ::ActiveRecord::Base } @@ -12,7 +11,6 @@ module Ransack it { should respond_to :search } describe '#search' do - subject { Person.ransack } it { should be_a Search } @@ -20,14 +18,6 @@ module Ransack expect(subject.object).to be_an ::ActiveRecord::Relation end - context 'using ransacker with a SqlLiteral callable' do - it 'should not raise an error' do - # The `with_banana` ransacker has a SqlLiteral callable - search = Person.search("with_banana_in" => "peel") - expect { search.result }.to_not raise_error - end - end - context 'with scopes' do before do Person.stub :ransackable_scopes => @@ -115,14 +105,6 @@ module Ransack end end - context "search on an `in` predicate with an array" do - it "should function correctly when passing an array of ids" do - array = Person.all.map(&:id) - s = Person.ransack(id_in: array) - expect(s.result.count).to eq array.size - end - end - describe '#ransacker' do # For infix tests def self.sane_adapter? @@ -216,7 +198,7 @@ module Ransack expect(s.result.to_a).to eq [p] end - context "search on an `in` predicate with an array to a ransacker" do + context "searching on an `in` predicate with a ransacker" do it "should function correctly when passing an array of ids" do s = Person.ransack(array_users_in: true) expect(s.result.count).to be > 0 @@ -227,6 +209,24 @@ module Ransack s = Person.ransack(array_names_in: true) expect(s.result.count).to be > 0 end + + it 'should function correctly with an Arel SqlLiteral' do + s = Person.search(sql_literal_id_in: 1) + expect(s.result.count).to be 1 + s = Person.search(sql_literal_id_in: ['2', 4, '5', 8]) + expect(s.result.count).to be 4 + end + end + + context "search on an `in` predicate with an array" do + it "should function correctly when passing an array of ids" do + array = Person.all.map(&:id) + s = Person.ransack(id_in: array) + expect(s.result.count).to eq array.size + end + end + + context 'search on an `in` predicate with an Arel.sql ransacker' do end it "should function correctly when an attribute name ends with '_start'" do diff --git a/spec/support/schema.rb b/spec/support/schema.rb index b364faa..d0b6d55 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -50,17 +50,6 @@ class Person < ActiveRecord::Base parent.table[:name] end - # The `with_banana` ransacker is just to reproduce a - # particular issue (#504) and probably should be removed - # eventually, because it doesn't represent a useful - # ransacker one might want in an actual application. - ransacker(:with_banana, - callable: proc { |parent| - Arel::Nodes::SqlLiteral.new("#{Article.table_name}.banana") - }, - formatter: proc { |v| v } - ) - ransacker :array_users, formatter: proc { |v| Person.first(2).map(&:id) } do |parent| parent.table[:id] @@ -77,6 +66,10 @@ class Person < ActiveRecord::Base ) end + ransacker :sql_literal_id do + Arel.sql('people.id') + end + def self.ransackable_attributes(auth_object = nil) if auth_object == :admin column_names + _ransackers.keys - ['only_sort']