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
This commit is contained in:
parent
0f7c3b06ce
commit
752b986bb9
|
@ -2,4 +2,3 @@
|
|||
.bundle
|
||||
Gemfile.lock
|
||||
pkg/*
|
||||
.idea
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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']
|
||||
|
|
Loading…
Reference in New Issue