scope_chain 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +32 -2
- data/lib/scope_chain/version.rb +1 -1
- data/lib/scope_chain.rb +20 -8
- data/scope_chain.gemspec +1 -0
- data/spec/scope_chain_spec.rb +44 -6
- metadata +12 -1
data/README.md
CHANGED
@@ -31,13 +31,43 @@ Using ScopeChain, you do:
|
|
31
31
|
|
32
32
|
What this will do is setup some expectations that make sure those scope methods are called.
|
33
33
|
|
34
|
+
You can do "manual" scopes:
|
35
|
+
|
36
|
+
def manual_scopes
|
37
|
+
User.scoped.where("somethign = else")
|
38
|
+
end
|
39
|
+
|
40
|
+
it "does the right thing" do
|
41
|
+
ScopeChain.for(User).where("something = else")
|
42
|
+
|
43
|
+
manual_scopes
|
44
|
+
end
|
45
|
+
|
46
|
+
You can test return values:
|
47
|
+
|
48
|
+
def return_values
|
49
|
+
User.where("thing = 1")
|
50
|
+
end
|
51
|
+
|
52
|
+
it "returns properly" do
|
53
|
+
ScopeChain.for(User).where("thing = 1").returns(5)
|
54
|
+
|
55
|
+
return_values.should eq(5)
|
56
|
+
end
|
57
|
+
|
58
|
+
|
34
59
|
Not in order, but called, which is something, right?
|
35
60
|
|
61
|
+
## Known Issues
|
62
|
+
|
63
|
+
Uh, actual, user defined scopes don't work. Or if they do, I'd be surprised. And I'm sure I'm
|
64
|
+
missing a bunch of the ActiveRecord methods that are used.
|
65
|
+
|
36
66
|
## Installation
|
37
67
|
|
38
68
|
Add this line to your application's Gemfile:
|
39
69
|
|
40
|
-
gem '
|
70
|
+
gem 'scope_chain'
|
41
71
|
|
42
72
|
And then execute:
|
43
73
|
|
@@ -45,7 +75,7 @@ And then execute:
|
|
45
75
|
|
46
76
|
Or install it yourself as:
|
47
77
|
|
48
|
-
$ gem install
|
78
|
+
$ gem install scope_chain
|
49
79
|
|
50
80
|
## Contributing
|
51
81
|
|
data/lib/scope_chain/version.rb
CHANGED
data/lib/scope_chain.rb
CHANGED
@@ -8,28 +8,40 @@ module ScopeChain
|
|
8
8
|
class Chain
|
9
9
|
LINKS = [:select, :where, :includes, :order]
|
10
10
|
|
11
|
-
attr_accessor :klass
|
11
|
+
attr_accessor :klass, :expectations
|
12
12
|
def initialize(klass, &block)
|
13
13
|
self.klass = klass
|
14
|
+
self.expectations = []
|
15
|
+
self.klass.stubs(scoped: klass) # Handle manual scope building
|
14
16
|
|
15
17
|
yield self if block_given?
|
16
18
|
end
|
17
19
|
|
18
20
|
LINKS.each do |link|
|
19
|
-
define_method(link) do
|
20
|
-
add_link link, arguments
|
21
|
+
define_method(link) do |*arguments|
|
22
|
+
add_link link, *arguments
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
24
26
|
def all(returned)
|
25
|
-
add_link :all
|
27
|
+
add_link :all
|
28
|
+
returns returned
|
26
29
|
end
|
27
|
-
|
30
|
+
|
31
|
+
def returns(object)
|
32
|
+
# DON'T LOOK
|
33
|
+
expectations.last.instance_variable_set(:@return_values, Mocha::ReturnValues.build(object))
|
34
|
+
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
28
38
|
private
|
29
|
-
def add_link(name, arguments
|
39
|
+
def add_link(name, *arguments)
|
30
40
|
expectation = klass.expects(name)
|
31
|
-
expectation.with(arguments) if arguments
|
32
|
-
expectation.returns(
|
41
|
+
expectation.with(*arguments) if arguments.size > 0
|
42
|
+
expectation.returns(klass)
|
43
|
+
|
44
|
+
expectations << expectation
|
33
45
|
|
34
46
|
self
|
35
47
|
end
|
data/scope_chain.gemspec
CHANGED
data/spec/scope_chain_spec.rb
CHANGED
@@ -1,9 +1,14 @@
|
|
1
1
|
lib = File.expand_path('../lib', __FILE__)
|
2
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
3
|
|
4
|
+
require 'active_record'
|
4
5
|
require 'mocha/api'
|
5
6
|
require 'scope_chain'
|
6
7
|
|
8
|
+
class Model < ActiveRecord::Base
|
9
|
+
scope :active, where("column = value")
|
10
|
+
end
|
11
|
+
|
7
12
|
describe ScopeChain do
|
8
13
|
let(:klass) { Class.new }
|
9
14
|
|
@@ -26,7 +31,7 @@ describe ScopeChain::Chain do
|
|
26
31
|
end
|
27
32
|
|
28
33
|
it "adds a link for #{link}" do
|
29
|
-
subject.expects(:add_link).with(link, :arguments
|
34
|
+
subject.expects(:add_link).with(link, :arguments)
|
30
35
|
|
31
36
|
subject.send(link, :arguments)
|
32
37
|
end
|
@@ -37,29 +42,62 @@ describe ScopeChain::Chain do
|
|
37
42
|
end
|
38
43
|
|
39
44
|
it "adds a link for all" do
|
40
|
-
subject.expects(:add_link).with(:all
|
45
|
+
subject.expects(:add_link).with(:all)
|
46
|
+
subject.expects(:returns).with(:boom)
|
41
47
|
|
42
48
|
subject.all(:boom)
|
43
49
|
end
|
44
50
|
|
51
|
+
it "supports manual scopes" do
|
52
|
+
subject.select("id")
|
53
|
+
|
54
|
+
klass.scoped.select("id")
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "#returns" do
|
58
|
+
it "modifies the last expectation" do
|
59
|
+
subject.select("id").where("5").returns("9")
|
60
|
+
|
61
|
+
klass.select("id").where("5").should eq("9")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
45
65
|
describe "#add_link" do
|
46
66
|
it "adds a basic expectation" do
|
47
67
|
expectation = mock
|
48
|
-
expectation.expects(:returns).with(
|
68
|
+
expectation.expects(:returns).with(klass)
|
49
69
|
|
50
70
|
klass.expects(:expects).with(:name).returns(expectation)
|
51
71
|
|
52
|
-
subject.send :add_link, :name
|
72
|
+
subject.send :add_link, :name
|
53
73
|
end
|
54
74
|
|
55
75
|
it "adds an expectation with arguments" do
|
56
76
|
expectation = mock
|
57
77
|
expectation.expects(:with).with(:arguments)
|
58
|
-
expectation.expects(:returns).with(
|
78
|
+
expectation.expects(:returns).with(klass)
|
59
79
|
|
60
80
|
klass.expects(:expects).with(:name).returns(expectation)
|
61
81
|
|
62
|
-
subject.send :add_link, :name, :arguments
|
82
|
+
subject.send :add_link, :name, :arguments
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "with a custom scopes" do
|
87
|
+
it "fails properly" do
|
88
|
+
pending
|
89
|
+
|
90
|
+
ScopeChain.for(Model).where("column != value")
|
91
|
+
|
92
|
+
expect { Model.active.to_a }.to raise_error(Mocha::StubbingError)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "passes properly" do
|
96
|
+
pending
|
97
|
+
|
98
|
+
ScopeChain.for(Model).where("column = value")
|
99
|
+
|
100
|
+
Model.active.to_a
|
63
101
|
end
|
64
102
|
end
|
65
103
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: scope_chain
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jon Moses
|
@@ -34,6 +34,17 @@ dependencies:
|
|
34
34
|
version: "0"
|
35
35
|
type: :development
|
36
36
|
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: activerecord
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.1.11
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id003
|
37
48
|
description: Easy testing of scope usage
|
38
49
|
email:
|
39
50
|
- jon@burningbush.us
|