1
0
Fork 0
mirror of https://github.com/activerecord-hackery/ransack.git synced 2022-11-09 13:47:45 -05:00

More specs

This commit is contained in:
Ernie Miller 2011-07-17 19:40:07 -04:00
parent 62b7bd2de8
commit 74ff6c7930
4 changed files with 65 additions and 21 deletions

View file

@ -25,10 +25,6 @@ module Ransack
end end
end end
def can_accept?(object)
method_defined? DISPATCH[object.class]
end
end end
def initialize(object) def initialize(object)

View file

@ -5,33 +5,36 @@ module Ransack
module ActiveRecord module ActiveRecord
describe Base do describe Base do
it 'adds a ransack method to ActiveRecord::Base' do subject { ::ActiveRecord::Base }
::ActiveRecord::Base.should respond_to :ransack
end
it 'aliases the method to search if available' do it { should respond_to :ransack }
::ActiveRecord::Base.should respond_to :search it { should respond_to :search }
end
describe '#search' do describe '#search' do
before do subject { Person.search }
@s = Person.search
end
it 'creates a search with Relation as its object' do it { should be_a Search }
@s.should be_a Search it 'has a Relation as its object' do
@s.object.should be_an ::ActiveRecord::Relation subject.object.should be_an ::ActiveRecord::Relation
end end
end end
describe '#ransacker' do describe '#ransacker' do
# 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 it 'creates ransack attributes' do
s = Person.search(:reversed_name_eq => 'htimS cirA') s = Person.search(:reversed_name_eq => 'htimS cirA')
s.result.should have(1).person s.result.should have(1).person
s.result.first.should eq Person.find_by_name('Aric Smith') s.result.first.should eq Person.find_by_name('Aric Smith')
end end
it 'allows access of attributes through associations' do it 'can be accessed through associations' do
s = Person.search(:children_reversed_name_eq => 'htimS cirA') s = Person.search(:children_reversed_name_eq => 'htimS cirA')
s.result.to_sql.should match /"children_people"."name" = 'Aric Smith'/ s.result.to_sql.should match /"children_people"."name" = 'Aric Smith'/
end end
@ -42,6 +45,22 @@ module Ransack
end if defined?(Arel::Nodes::InfixOperation) end if defined?(Arel::Nodes::InfixOperation)
end end
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
end end
end end
end end

View file

@ -4,19 +4,35 @@ module Ransack
module Adapters module Adapters
module ActiveRecord module ActiveRecord
describe Context do describe Context do
before do subject { Context.new(Person) }
@c = Context.new(Person)
describe '#evaluate' do
it 'evaluates search obects' do
search = Search.new(Person, :name_eq => 'Joe Blow')
result = subject.evaluate(search)
result.should be_an ::ActiveRecord::Relation
result.to_sql.should match /"name" = 'Joe Blow'/
end
it 'SELECTs DISTINCT when :distinct => true' do
search = Search.new(Person, :name_eq => 'Joe Blow')
result = subject.evaluate(search, :distinct => true)
result.should be_an ::ActiveRecord::Relation
result.to_sql.should match /SELECT DISTINCT/
end
end end
it 'contextualizes strings to attributes' do it 'contextualizes strings to attributes' do
attribute = @c.contextualize 'children_children_parent_name' attribute = subject.contextualize 'children_children_parent_name'
attribute.should be_a Arel::Attributes::Attribute attribute.should be_a Arel::Attributes::Attribute
attribute.name.to_s.should eq 'name' attribute.name.to_s.should eq 'name'
attribute.relation.table_alias.should eq 'parents_people' attribute.relation.table_alias.should eq 'parents_people'
end end
it 'builds new associations if not yet built' do it 'builds new associations if not yet built' do
attribute = @c.contextualize 'children_articles_title' attribute = subject.contextualize 'children_articles_title'
attribute.should be_a Arel::Attributes::Attribute attribute.should be_a Arel::Attributes::Attribute
attribute.name.to_s.should eq 'title' attribute.name.to_s.should eq 'title'
attribute.relation.name.should eq 'articles' attribute.relation.name.should eq 'articles'

View file

@ -26,3 +26,16 @@ RSpec.configure do |config|
config.include RansackHelper config.include RansackHelper
end end
RSpec::Matchers.define :be_like do |expected|
match do |actual|
actual.gsub(/^\s+|\s+$/, '').gsub(/\s+/, ' ').strip ==
expected.gsub(/^\s+|\s+$/, '').gsub(/\s+/, ' ').strip
end
end
RSpec::Matchers.define :have_attribute_method do |expected|
match do |actual|
actual.attribute_method?(expected)
end
end