scope_chain 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +56 -0
- data/Rakefile +1 -0
- data/lib/scope_chain/version.rb +3 -0
- data/lib/scope_chain.rb +37 -0
- data/scope_chain.gemspec +23 -0
- data/spec/scope_chain_spec.rb +66 -0
- metadata +84 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jon Moses
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# ScopeChain
|
2
|
+
|
3
|
+
ScopeChain is a tiny help class useful for testing ActiveRecord model scope usage
|
4
|
+
and scope chaining
|
5
|
+
|
6
|
+
# Usage
|
7
|
+
|
8
|
+
For example, say you have:
|
9
|
+
|
10
|
+
class User < ActiveRecord::Base
|
11
|
+
end
|
12
|
+
|
13
|
+
And you use that model:
|
14
|
+
|
15
|
+
def some_method
|
16
|
+
User.where(active: 1).order("created_at desc")
|
17
|
+
end
|
18
|
+
|
19
|
+
And you want to test that method, but without setting creating data in your database,
|
20
|
+
or actually using the data base.
|
21
|
+
|
22
|
+
Using ScopeChain, you do:
|
23
|
+
|
24
|
+
context "my method" do
|
25
|
+
it "gets the right users" do
|
26
|
+
ScopeChain.for(User).where(active: 1).order("created_at desc")
|
27
|
+
|
28
|
+
some_method
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
What this will do is setup some expectations that make sure those scope methods are called.
|
33
|
+
|
34
|
+
Not in order, but called, which is something, right?
|
35
|
+
|
36
|
+
## Installation
|
37
|
+
|
38
|
+
Add this line to your application's Gemfile:
|
39
|
+
|
40
|
+
gem 'scope-chain'
|
41
|
+
|
42
|
+
And then execute:
|
43
|
+
|
44
|
+
$ bundle
|
45
|
+
|
46
|
+
Or install it yourself as:
|
47
|
+
|
48
|
+
$ gem install scope-chain
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/scope_chain.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require "scope_chain/version"
|
2
|
+
|
3
|
+
module ScopeChain
|
4
|
+
def self.for(klass, &block)
|
5
|
+
Chain.new klass, &block
|
6
|
+
end
|
7
|
+
|
8
|
+
class Chain
|
9
|
+
LINKS = [:select, :where, :includes, :order]
|
10
|
+
|
11
|
+
attr_accessor :klass
|
12
|
+
def initialize(klass, &block)
|
13
|
+
self.klass = klass
|
14
|
+
|
15
|
+
yield self if block_given?
|
16
|
+
end
|
17
|
+
|
18
|
+
LINKS.each do |link|
|
19
|
+
define_method(link) do |arguments = nil, returned = nil|
|
20
|
+
add_link link, arguments, returned || klass
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def all(returned)
|
25
|
+
add_link :all, nil, returned
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def add_link(name, arguments = nil, returned = klass)
|
30
|
+
expectation = klass.expects(name)
|
31
|
+
expectation.with(arguments) if arguments
|
32
|
+
expectation.returns(returned)
|
33
|
+
|
34
|
+
self
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/scope_chain.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'scope_chain/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "scope_chain"
|
8
|
+
gem.version = ScopeChain::VERSION
|
9
|
+
gem.authors = ["Jon Moses"]
|
10
|
+
gem.email = ["jon@burningbush.us"]
|
11
|
+
gem.description = %q{Easy testing of scope usage}
|
12
|
+
gem.summary = %q{Easy testing of scope usage for models}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency 'mocha'
|
21
|
+
|
22
|
+
gem.add_development_dependency 'rspec'
|
23
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require 'mocha/api'
|
5
|
+
require 'scope_chain'
|
6
|
+
|
7
|
+
describe ScopeChain do
|
8
|
+
let(:klass) { Class.new }
|
9
|
+
|
10
|
+
it "yields" do
|
11
|
+
expect {|b| ScopeChain.for(klass, &b) }.to yield_control
|
12
|
+
end
|
13
|
+
|
14
|
+
it "uses the right klass" do
|
15
|
+
ScopeChain.for(klass).klass.should eq(klass)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe ScopeChain::Chain do
|
20
|
+
let(:klass) { Class.new }
|
21
|
+
subject { described_class.new(klass) }
|
22
|
+
|
23
|
+
ScopeChain::Chain::LINKS.each do |link|
|
24
|
+
it "has a #{link} link" do
|
25
|
+
subject.should respond_to(link)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "adds a link for #{link}" do
|
29
|
+
subject.expects(:add_link).with(link, :arguments, klass)
|
30
|
+
|
31
|
+
subject.send(link, :arguments)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it "has an all link" do
|
36
|
+
subject.should respond_to(:all)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "adds a link for all" do
|
40
|
+
subject.expects(:add_link).with(:all, nil, :boom)
|
41
|
+
|
42
|
+
subject.all(:boom)
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#add_link" do
|
46
|
+
it "adds a basic expectation" do
|
47
|
+
expectation = mock
|
48
|
+
expectation.expects(:returns).with(:returned)
|
49
|
+
|
50
|
+
klass.expects(:expects).with(:name).returns(expectation)
|
51
|
+
|
52
|
+
subject.send :add_link, :name, nil, :returned
|
53
|
+
end
|
54
|
+
|
55
|
+
it "adds an expectation with arguments" do
|
56
|
+
expectation = mock
|
57
|
+
expectation.expects(:with).with(:arguments)
|
58
|
+
expectation.expects(:returns).with(:returned)
|
59
|
+
|
60
|
+
klass.expects(:expects).with(:name).returns(expectation)
|
61
|
+
|
62
|
+
subject.send :add_link, :name, :arguments, :returned
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scope_chain
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jon Moses
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2013-03-15 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mocha
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rspec
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
description: Easy testing of scope usage
|
38
|
+
email:
|
39
|
+
- jon@burningbush.us
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- LICENSE.txt
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- lib/scope_chain.rb
|
53
|
+
- lib/scope_chain/version.rb
|
54
|
+
- scope_chain.gemspec
|
55
|
+
- spec/scope_chain_spec.rb
|
56
|
+
homepage: ""
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: "0"
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.8.24
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: Easy testing of scope usage for models
|
83
|
+
test_files:
|
84
|
+
- spec/scope_chain_spec.rb
|