Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Test the ActiveRecord adapter with real queried results.
Browse files Browse the repository at this point in the history
  • Loading branch information
laserlemon committed Feb 3, 2011
1 parent fa7bb35 commit f3c434e
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 41 deletions.
2 changes: 1 addition & 1 deletion lib/periscope/adapters/active_record.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module ActiveRecord
include Abstract include Abstract


module ClassMethods module ClassMethods
def periscope(params) def periscope(params = {})
periscope_authorizer.sanitize(params).inject(scoped) do |chain, (key, value)| periscope_authorizer.sanitize(params).inject(scoped) do |chain, (key, value)|
chain.send(key, value) chain.send(key, value)
end end
Expand Down
139 changes: 99 additions & 40 deletions spec/periscope/adapters/active_record_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,65 +5,124 @@ module Adapters
describe ActiveRecord do describe ActiveRecord do
it_should_behave_like 'an adapter' it_should_behave_like 'an adapter'


subject do describe :periscope do
Class.new(User).tap do |klass| subject do
klass.class_eval do Class.new(User).tap do |klass|
scope :gender, lambda{|g| where(:gender => g) } klass.class_eval do
scope :makes, lambda{|s| where('users.salary >= ?', s) } scope :gender, lambda{|g| where(:gender => g) }
scope :rich, where('users.salary >= 1000000') scope :makes, lambda{|s| where('users.salary >= ?', s) }
scope :rich, where('users.salary >= 1000000')
end
end end
end end
end


it 'ignores all scopes by default' do context 'ignores' do
subject.should_receive(:gender).never specify 'all scopes by default' do
subject.should_receive(:makes).never subject.should_receive(:gender).never
subject.should_receive(:makes).never


subject.periscope(:gender => 'male', :makes => 100000) subject.periscope(:gender => 'male', :makes => 100000)
end end


it 'ignores all scopes when none are accessible' do specify 'all scopes when none are accessible' do
subject.scope_accessible subject.scope_accessible


subject.should_receive(:gender).never subject.should_receive(:gender).never
subject.should_receive(:makes).never subject.should_receive(:makes).never


subject.periscope(:gender => 'male', :makes => 100000) subject.periscope(:gender => 'male', :makes => 100000)
end end


it 'uses all scopes when none are protected' do specify 'protected scopes' do
subject.scope_protected subject.scope_protected :makes


subject.should_receive(:gender).with('male').once subject.should_receive(:gender).with('male').once
subject.should_receive(:makes).with(100000).once subject.should_receive(:makes).never


subject.periscope(:gender => 'male', :makes => 100000) subject.periscope(:gender => 'male', :makes => 100000)
end end
end


it 'uses accessible scopes' do context 'uses' do
subject.scope_accessible :gender specify 'all scopes when none are protected' do
subject.scope_protected


subject.should_receive(:gender).with('male').once subject.should_receive(:gender).with('male').once
subject.should_receive(:makes).never subject.should_receive(:makes).with(100000).once


subject.periscope(:gender => 'male', :makes => 100000) subject.periscope(:gender => 'male', :makes => 100000)
end end


it 'ignores protected scopes' do specify 'accessible scopes' do
subject.scope_protected :makes subject.scope_accessible :gender


subject.should_receive(:gender).with('male').once subject.should_receive(:gender).with('male').once
subject.should_receive(:makes).never subject.should_receive(:makes).never


subject.periscope(:gender => 'male', :makes => 100000) subject.periscope(:gender => 'male', :makes => 100000)
end end

specify 'accessible, zero-arity scopes' do
subject.scope_accessible :rich

subject.should_receive(:rich).with(true).once

lambda{ subject.periscope(:rich => true) }.should_not raise_error
end
end

context 'returns' do
before do
subject.delete_all
subject.create(:name => 'Henry', :gender => 'male', :salary => 50_000)
subject.create(:name => 'Penny', :gender => 'female', :salary => 1_000_000)
subject.create(:name => 'Sammy', :gender => 'male', :salary => 100_000)
end

let(:params){ {:gender => 'male', :rich => true} }

context 'all records' do
specify 'for no params' do
subject.periscope.map(&:name).should == %w(Henry Penny Sammy)
end

specify 'for empty params' do
subject.periscope({}).map(&:name).should == %w(Henry Penny Sammy)
end

specify 'for no accessible scopes' do
subject.scope_accessible

subject.periscope(params).map(&:name).should == %w(Henry Penny Sammy)
end


it 'uses accessible, zero-arity scopes' do specify 'for all protected scopes' do
subject.scope_accessible :rich subject.scope_protected :gender, :rich


subject.should_receive(:rich).with(true).once subject.periscope(params).map(&:name).should == %w(Henry Penny Sammy)
end
end

context 'scoped results' do
specify 'for an accessible scope' do
subject.scope_accessible :rich

subject.periscope(params).map(&:name).should == %w(Penny)
end

specify 'for an unprotected scope' do
subject.scope_protected :gender


lambda{ subject.periscope(:rich => true) }.should_not raise_error subject.periscope(params).map(&:name).should == %w(Penny)
end

specify 'for multiple accessible scopes' do
subject.scope_accessible :gender, :rich

subject.periscope(params).should be_empty
end
end
end
end end
end end
end end
Expand Down

0 comments on commit f3c434e

Please sign in to comment.