activerecord-hackery--ransack/spec/ransack/adapters/active_record/base_spec.rb

72 lines
2.3 KiB
Ruby
Raw Normal View History

2011-03-31 00:31:39 +00:00
require 'spec_helper'
module Ransack
module Adapters
module ActiveRecord
describe Base do
2011-07-17 23:40:07 +00:00
subject { ::ActiveRecord::Base }
2011-03-31 00:31:39 +00:00
2011-07-17 23:40:07 +00:00
it { should respond_to :ransack }
it { should respond_to :search }
2011-03-31 00:31:39 +00:00
describe '#search' do
2011-07-17 23:40:07 +00:00
subject { Person.search }
2011-03-31 00:31:39 +00:00
2011-07-17 23:40:07 +00:00
it { should be_a Search }
it 'has a Relation as its object' do
subject.object.should be_an ::ActiveRecord::Relation
2011-03-31 00:31:39 +00:00
end
end
describe '#ransacker' do
2011-07-17 23:40:07 +00:00
# in schema.rb, class Person:
# ransacker :reversed_name, :formatter => proc {|v| v.reverse} do |parent|
# parent.table[:name]
# end
#
# ransacker :doubled_name do |parent|
# Arel::Nodes::InfixOperation.new('||', parent.table[:name], parent.table[:name])
# end
it 'creates ransack attributes' do
2011-04-11 16:04:31 +00:00
s = Person.search(:reversed_name_eq => 'htimS cirA')
s.result.should have(1).person
s.result.first.should eq Person.find_by(name: 'Aric Smith')
end
2011-04-11 16:59:26 +00:00
2011-07-17 23:40:07 +00:00
it 'can be accessed through associations' do
2011-04-11 16:59:26 +00:00
s = Person.search(:children_reversed_name_eq => 'htimS cirA')
s.result.to_sql.should match /"children_people"."name" = 'Aric Smith'/
end
it 'allows an "attribute" to be an InfixOperation' do
s = Person.search(:doubled_name_eq => 'Aric SmithAric Smith')
s.result.first.should eq Person.find_by(name: 'Aric Smith')
2011-04-18 15:28:19 +00:00
end if defined?(Arel::Nodes::InfixOperation)
2012-08-22 23:17:45 +00:00
it "doesn't break #count if using InfixOperations" do
s = Person.search(:doubled_name_eq => 'Aric SmithAric Smith')
s.result.count.should eq 1
end if defined?(Arel::Nodes::InfixOperation)
end
2011-07-17 23:40:07 +00:00
describe '#ransackable_attributes' do
subject { Person.ransackable_attributes }
it { should include 'name' }
it { should include 'reversed_name' }
it { should include 'doubled_name' }
end
describe '#ransackable_associations' do
subject { Person.ransackable_associations }
it { should include 'parent' }
it { should include 'children' }
it { should include 'articles' }
end
2011-03-31 00:31:39 +00:00
end
end
end
end