Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Test the ActiveRecord adapter with real queried results.

  • Loading branch information...
commit f3c434e6f990ae8d1e622ebf24bed1cd96a2b5fc 1 parent fa7bb35
Steve Richert authored
2  lib/periscope/adapters/active_record.rb
@@ -7,7 +7,7 @@ module ActiveRecord
7 7 include Abstract
8 8
9 9 module ClassMethods
10   - def periscope(params)
  10 + def periscope(params = {})
11 11 periscope_authorizer.sanitize(params).inject(scoped) do |chain, (key, value)|
12 12 chain.send(key, value)
13 13 end
139 spec/periscope/adapters/active_record_spec.rb
@@ -5,65 +5,124 @@ module Adapters
5 5 describe ActiveRecord do
6 6 it_should_behave_like 'an adapter'
7 7
8   - subject do
9   - Class.new(User).tap do |klass|
10   - klass.class_eval do
11   - scope :gender, lambda{|g| where(:gender => g) }
12   - scope :makes, lambda{|s| where('users.salary >= ?', s) }
13   - scope :rich, where('users.salary >= 1000000')
  8 + describe :periscope do
  9 + subject do
  10 + Class.new(User).tap do |klass|
  11 + klass.class_eval do
  12 + scope :gender, lambda{|g| where(:gender => g) }
  13 + scope :makes, lambda{|s| where('users.salary >= ?', s) }
  14 + scope :rich, where('users.salary >= 1000000')
  15 + end
14 16 end
15 17 end
16   - end
17 18
18   - it 'ignores all scopes by default' do
19   - subject.should_receive(:gender).never
20   - subject.should_receive(:makes).never
  19 + context 'ignores' do
  20 + specify 'all scopes by default' do
  21 + subject.should_receive(:gender).never
  22 + subject.should_receive(:makes).never
21 23
22   - subject.periscope(:gender => 'male', :makes => 100000)
23   - end
  24 + subject.periscope(:gender => 'male', :makes => 100000)
  25 + end
24 26
25   - it 'ignores all scopes when none are accessible' do
26   - subject.scope_accessible
  27 + specify 'all scopes when none are accessible' do
  28 + subject.scope_accessible
27 29
28   - subject.should_receive(:gender).never
29   - subject.should_receive(:makes).never
  30 + subject.should_receive(:gender).never
  31 + subject.should_receive(:makes).never
30 32
31   - subject.periscope(:gender => 'male', :makes => 100000)
32   - end
  33 + subject.periscope(:gender => 'male', :makes => 100000)
  34 + end
33 35
34   - it 'uses all scopes when none are protected' do
35   - subject.scope_protected
  36 + specify 'protected scopes' do
  37 + subject.scope_protected :makes
36 38
37   - subject.should_receive(:gender).with('male').once
38   - subject.should_receive(:makes).with(100000).once
  39 + subject.should_receive(:gender).with('male').once
  40 + subject.should_receive(:makes).never
39 41
40   - subject.periscope(:gender => 'male', :makes => 100000)
41   - end
  42 + subject.periscope(:gender => 'male', :makes => 100000)
  43 + end
  44 + end
42 45
43   - it 'uses accessible scopes' do
44   - subject.scope_accessible :gender
  46 + context 'uses' do
  47 + specify 'all scopes when none are protected' do
  48 + subject.scope_protected
45 49
46   - subject.should_receive(:gender).with('male').once
47   - subject.should_receive(:makes).never
  50 + subject.should_receive(:gender).with('male').once
  51 + subject.should_receive(:makes).with(100000).once
48 52
49   - subject.periscope(:gender => 'male', :makes => 100000)
50   - end
  53 + subject.periscope(:gender => 'male', :makes => 100000)
  54 + end
51 55
52   - it 'ignores protected scopes' do
53   - subject.scope_protected :makes
  56 + specify 'accessible scopes' do
  57 + subject.scope_accessible :gender
54 58
55   - subject.should_receive(:gender).with('male').once
56   - subject.should_receive(:makes).never
  59 + subject.should_receive(:gender).with('male').once
  60 + subject.should_receive(:makes).never
57 61
58   - subject.periscope(:gender => 'male', :makes => 100000)
59   - end
  62 + subject.periscope(:gender => 'male', :makes => 100000)
  63 + end
  64 +
  65 + specify 'accessible, zero-arity scopes' do
  66 + subject.scope_accessible :rich
  67 +
  68 + subject.should_receive(:rich).with(true).once
  69 +
  70 + lambda{ subject.periscope(:rich => true) }.should_not raise_error
  71 + end
  72 + end
  73 +
  74 + context 'returns' do
  75 + before do
  76 + subject.delete_all
  77 + subject.create(:name => 'Henry', :gender => 'male', :salary => 50_000)
  78 + subject.create(:name => 'Penny', :gender => 'female', :salary => 1_000_000)
  79 + subject.create(:name => 'Sammy', :gender => 'male', :salary => 100_000)
  80 + end
  81 +
  82 + let(:params){ {:gender => 'male', :rich => true} }
  83 +
  84 + context 'all records' do
  85 + specify 'for no params' do
  86 + subject.periscope.map(&:name).should == %w(Henry Penny Sammy)
  87 + end
  88 +
  89 + specify 'for empty params' do
  90 + subject.periscope({}).map(&:name).should == %w(Henry Penny Sammy)
  91 + end
  92 +
  93 + specify 'for no accessible scopes' do
  94 + subject.scope_accessible
  95 +
  96 + subject.periscope(params).map(&:name).should == %w(Henry Penny Sammy)
  97 + end
60 98
61   - it 'uses accessible, zero-arity scopes' do
62   - subject.scope_accessible :rich
  99 + specify 'for all protected scopes' do
  100 + subject.scope_protected :gender, :rich
63 101
64   - subject.should_receive(:rich).with(true).once
  102 + subject.periscope(params).map(&:name).should == %w(Henry Penny Sammy)
  103 + end
  104 + end
  105 +
  106 + context 'scoped results' do
  107 + specify 'for an accessible scope' do
  108 + subject.scope_accessible :rich
  109 +
  110 + subject.periscope(params).map(&:name).should == %w(Penny)
  111 + end
  112 +
  113 + specify 'for an unprotected scope' do
  114 + subject.scope_protected :gender
65 115
66   - lambda{ subject.periscope(:rich => true) }.should_not raise_error
  116 + subject.periscope(params).map(&:name).should == %w(Penny)
  117 + end
  118 +
  119 + specify 'for multiple accessible scopes' do
  120 + subject.scope_accessible :gender, :rich
  121 +
  122 + subject.periscope(params).should be_empty
  123 + end
  124 + end
  125 + end
67 126 end
68 127 end
69 128 end

0 comments on commit f3c434e

Please sign in to comment.
Something went wrong with that request. Please try again.