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
def can_accept?(object)
method_defined? DISPATCH[object.class]
end
end
def initialize(object)

View File

@ -5,33 +5,36 @@ module Ransack
module ActiveRecord
describe Base do
it 'adds a ransack method to ActiveRecord::Base' do
::ActiveRecord::Base.should respond_to :ransack
end
subject { ::ActiveRecord::Base }
it 'aliases the method to search if available' do
::ActiveRecord::Base.should respond_to :search
end
it { should respond_to :ransack }
it { should respond_to :search }
describe '#search' do
before do
@s = Person.search
end
subject { Person.search }
it 'creates a search with Relation as its object' do
@s.should be_a Search
@s.object.should be_an ::ActiveRecord::Relation
it { should be_a Search }
it 'has a Relation as its object' do
subject.object.should be_an ::ActiveRecord::Relation
end
end
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
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
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.result.to_sql.should match /"children_people"."name" = 'Aric Smith'/
end
@ -42,6 +45,22 @@ module Ransack
end if defined?(Arel::Nodes::InfixOperation)
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

View File

@ -4,19 +4,35 @@ module Ransack
module Adapters
module ActiveRecord
describe Context do
before do
@c = Context.new(Person)
subject { 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
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.name.to_s.should eq 'name'
attribute.relation.table_alias.should eq 'parents_people'
end
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.name.to_s.should eq 'title'
attribute.relation.name.should eq 'articles'

View File

@ -26,3 +26,16 @@ RSpec.configure do |config|
config.include RansackHelper
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