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

52 lines
1.7 KiB
Ruby
Raw Normal View History

2011-03-31 00:31:39 +00:00
require 'spec_helper'
module Ransack
module Adapters
module ActiveRecord
describe Context do
2011-07-17 23:40:07 +00:00
subject { Context.new(Person) }
describe '#relation_for' do
it 'returns relation for given object' do
subject.object.should be_an ::ActiveRecord::Relation
end
end
2011-07-17 23:40:07 +00:00
describe '#evaluate' do
2013-08-12 18:40:39 +00:00
it 'evaluates search objects' do
search = Search.new(Person, :name_eq => 'Joe Blow')
2011-07-17 23:40:07 +00:00
result = subject.evaluate(search)
result.should be_an ::ActiveRecord::Relation
2013-12-07 02:01:22 +00:00
result.to_sql.should match /#{quote_column_name("name")} = 'Joe Blow'/
2011-07-17 23:40:07 +00:00
end
2013-08-04 13:13:41 +00:00
it 'SELECTs DISTINCT when distinct: true' do
search = Search.new(Person, :name_eq => 'Joe Blow')
result = subject.evaluate(search, :distinct => true)
2011-07-17 23:40:07 +00:00
result.should be_an ::ActiveRecord::Relation
result.to_sql.should match /SELECT DISTINCT/
end
2011-03-31 00:31:39 +00:00
end
it 'contextualizes strings to attributes' do
2011-07-17 23:40:07 +00:00
attribute = subject.contextualize 'children_children_parent_name'
2011-03-31 00:31:39 +00:00
attribute.should be_a Arel::Attributes::Attribute
2011-04-18 15:28:19 +00:00
attribute.name.to_s.should eq 'name'
2011-03-31 00:31:39 +00:00
attribute.relation.table_alias.should eq 'parents_people'
end
it 'builds new associations if not yet built' do
2011-07-17 23:40:07 +00:00
attribute = subject.contextualize 'children_articles_title'
2011-03-31 00:31:39 +00:00
attribute.should be_a Arel::Attributes::Attribute
2011-04-18 15:28:19 +00:00
attribute.name.to_s.should eq 'title'
2011-03-31 00:31:39 +00:00
attribute.relation.name.should eq 'articles'
attribute.relation.table_alias.should be_nil
end
end
end
end
end