diff --git a/lib/ransack/context.rb b/lib/ransack/context.rb index 3507562..436f4a1 100644 --- a/lib/ransack/context.rb +++ b/lib/ransack/context.rb @@ -25,10 +25,6 @@ module Ransack end end - def can_accept?(object) - method_defined? DISPATCH[object.class] - end - end def initialize(object) diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index 72e86f0..aad7b6a 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -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 diff --git a/spec/ransack/adapters/active_record/context_spec.rb b/spec/ransack/adapters/active_record/context_spec.rb index e3fe3f9..a3440b6 100644 --- a/spec/ransack/adapters/active_record/context_spec.rb +++ b/spec/ransack/adapters/active_record/context_spec.rb @@ -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' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0d68720..145d24d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -25,4 +25,17 @@ RSpec.configure do |config| config.before(:each) { Sham.reset(:before_each) } 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 \ No newline at end of file