ryanb-scope-builder 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +0 -0
- data/LICENSE +20 -0
- data/Manifest +14 -0
- data/README +50 -0
- data/TODO +0 -0
- data/lib/scope_builder/builder.rb +25 -0
- data/lib/scope_builder/model_additions.rb +13 -0
- data/lib/scope_builder.rb +3 -0
- data/scope-builder.gemspec +58 -0
- data/spec/models/product.rb +16 -0
- data/spec/scope_builder/builder_spec.rb +50 -0
- data/spec/spec_helper.rb +20 -0
- data/tasks/deployment.rake +2 -0
- data/tasks/spec.rake +9 -0
- metadata +80 -0
data/CHANGELOG
ADDED
File without changes
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Ryan Bates
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
CHANGELOG
|
2
|
+
lib/scope_builder/builder.rb
|
3
|
+
lib/scope_builder/model_additions.rb
|
4
|
+
lib/scope_builder.rb
|
5
|
+
LICENSE
|
6
|
+
Manifest
|
7
|
+
README
|
8
|
+
spec/models/product.rb
|
9
|
+
spec/scope_builder/builder_spec.rb
|
10
|
+
spec/spec_helper.rb
|
11
|
+
spec/test.sqlite3
|
12
|
+
tasks/deployment.rake
|
13
|
+
tasks/spec.rake
|
14
|
+
TODO
|
data/README
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
= Scope Builder
|
2
|
+
|
3
|
+
Build up named scopes conditionally.
|
4
|
+
|
5
|
+
|
6
|
+
== Install
|
7
|
+
|
8
|
+
First install the gem.
|
9
|
+
|
10
|
+
gem install ryanb-scope-builder --source=http://gems.github.com
|
11
|
+
|
12
|
+
Then specify it in your Rails config.
|
13
|
+
|
14
|
+
config.gem 'ryanb-scope-builder', :lib => 'scope_builder', :source => 'http://gems.github.com'
|
15
|
+
|
16
|
+
Rails 2.1 or later required.
|
17
|
+
|
18
|
+
|
19
|
+
== Usage
|
20
|
+
|
21
|
+
This gem adds the scope_builder method to all Active Record models. A
|
22
|
+
builder behaves exactly like any other named scope except that calling
|
23
|
+
other named scopes on it will alter the builder itself rather than
|
24
|
+
returning a new named scope.
|
25
|
+
|
26
|
+
builder = Product.scope_builder
|
27
|
+
builder.released.visible # call a couple named scopes to change builder
|
28
|
+
builder.cheap if only_show_cheap_products? # build scopes conditionally
|
29
|
+
|
30
|
+
The scope_builder method can also take a block which will return the
|
31
|
+
builder. This is useful when you are using the builder in a model
|
32
|
+
search method.
|
33
|
+
|
34
|
+
# in product model
|
35
|
+
def self.search(options)
|
36
|
+
scope_builder do |builder|
|
37
|
+
builder.released.visible
|
38
|
+
builder.cheap if options[:cheap]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
== Development
|
44
|
+
|
45
|
+
This project can be found on github at the following URL.
|
46
|
+
|
47
|
+
http://github.com/ryanb/scope-builder/
|
48
|
+
|
49
|
+
If you would like to contribute to this project, please fork the
|
50
|
+
repository and send me a pull request.
|
data/TODO
ADDED
File without changes
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ScopeBuilder
|
2
|
+
class Builder
|
3
|
+
def initialize(proxy_scope)
|
4
|
+
@proxy_scope = proxy_scope
|
5
|
+
end
|
6
|
+
|
7
|
+
def method_missing(method, *args, &block)
|
8
|
+
result = @proxy_scope.send(method, *args, &block)
|
9
|
+
if result.class == ActiveRecord::NamedScope::Scope
|
10
|
+
@proxy_scope = result
|
11
|
+
self
|
12
|
+
else
|
13
|
+
result
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def respond_to?(method)
|
18
|
+
super || @proxy_scope.respond_to?(method)
|
19
|
+
end
|
20
|
+
|
21
|
+
def inspect
|
22
|
+
sprintf("#<%s:%#0x>", self.class.to_s, self.object_id)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
|
2
|
+
# Gem::Specification for Scope-builder-0.1.0
|
3
|
+
# Originally generated by Echoe
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = %q{scope-builder}
|
7
|
+
s.version = "0.1.0"
|
8
|
+
|
9
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
10
|
+
s.authors = ["Ryan Bates"]
|
11
|
+
s.date = %q{2008-06-26}
|
12
|
+
s.description = %q{Build up named scopes conditionally.}
|
13
|
+
s.email = %q{ryan (at) railscasts (dot) com}
|
14
|
+
s.extra_rdoc_files = ["CHANGELOG", "lib/scope_builder/builder.rb", "lib/scope_builder/model_additions.rb", "lib/scope_builder.rb", "LICENSE", "README", "tasks/deployment.rake", "tasks/spec.rake", "TODO"]
|
15
|
+
s.files = ["CHANGELOG", "lib/scope_builder/builder.rb", "lib/scope_builder/model_additions.rb", "lib/scope_builder.rb", "LICENSE", "Manifest", "README", "spec/models/product.rb", "spec/scope_builder/builder_spec.rb", "spec/spec_helper.rb", "spec/test.sqlite3", "tasks/deployment.rake", "tasks/spec.rake", "TODO", "scope-builder.gemspec"]
|
16
|
+
s.has_rdoc = true
|
17
|
+
s.homepage = %q{http://github.com/ryanb/scope-builder}
|
18
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Scope-builder", "--main", "README"]
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
s.rubyforge_project = %q{scope-builder}
|
21
|
+
s.rubygems_version = %q{1.2.0}
|
22
|
+
s.summary = %q{Build up named scopes conditionally.}
|
23
|
+
|
24
|
+
if s.respond_to? :specification_version then
|
25
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
26
|
+
s.specification_version = 2
|
27
|
+
|
28
|
+
if current_version >= 3 then
|
29
|
+
else
|
30
|
+
end
|
31
|
+
else
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
# # Original Rakefile source (requires the Echoe gem):
|
37
|
+
#
|
38
|
+
# require 'rubygems'
|
39
|
+
# require 'rake'
|
40
|
+
#
|
41
|
+
# begin
|
42
|
+
# require 'echoe'
|
43
|
+
#
|
44
|
+
# Echoe.new('scope-builder', '0.1.0') do |p|
|
45
|
+
# p.summary = "Build up named scopes conditionally."
|
46
|
+
# p.description = "Build up named scopes conditionally."
|
47
|
+
# p.url = "http://github.com/ryanb/scope-builder"
|
48
|
+
# p.author = 'Ryan Bates'
|
49
|
+
# p.email = "ryan (at) railscasts (dot) com"
|
50
|
+
# p.ignore_pattern = ["script/*"]
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# rescue LoadError => boom
|
54
|
+
# puts "You are missing a dependency required for meta-operations on this gem."
|
55
|
+
# puts "#{boom.to_s.capitalize}."
|
56
|
+
# end
|
57
|
+
#
|
58
|
+
# Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class Product < ActiveRecord::Base
|
2
|
+
named_scope :released, :conditions => ['released=?', true]
|
3
|
+
end
|
4
|
+
|
5
|
+
class CreateProducts < ActiveRecord::Migration
|
6
|
+
def self.up
|
7
|
+
create_table :products do |t|
|
8
|
+
t.string :name
|
9
|
+
t.boolean :released
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.down
|
14
|
+
drop_table :products
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe ScopeBuilder::Builder do
|
4
|
+
before(:each) do
|
5
|
+
Product.delete_all
|
6
|
+
Product.create!(:name => 'a', :released => true)
|
7
|
+
Product.create!(:name => 'b', :released => false)
|
8
|
+
Product.create!(:name => 'c', :released => true)
|
9
|
+
|
10
|
+
@builder = Product.scope_builder
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should start with empty proxy options" do
|
14
|
+
@builder.proxy_options.should == {}
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should allow named scopes to be called through it" do
|
18
|
+
@builder.released.proxy_options.should == Product.released.proxy_options
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should remember scope calls" do
|
22
|
+
@builder.released
|
23
|
+
@builder.proxy_options.should == Product.released.proxy_options
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should build up scopes" do
|
27
|
+
@builder.released.scoped(:limit => 1)
|
28
|
+
@builder.scoped(:offset => 1)
|
29
|
+
@builder.all.should == Product.released.scoped(:limit => 1, :offset => 1).all
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should enumerate like an array" do
|
33
|
+
products = Product.find(:all)
|
34
|
+
@builder.each_with_index do |product, index|
|
35
|
+
product.should == products[index]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should not include instance variables on inspect so it doesn't load proxy" do
|
40
|
+
@builder.inspect.should_not include('proxy_scope')
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should respond to enumerable methods like each_with_index" do
|
44
|
+
@builder.should respond_to(:each_with_index)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should be able to build up scope in block" do
|
48
|
+
Product.scope_builder { |b| b.released }.all.should == Product.released.all
|
49
|
+
end
|
50
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_record'
|
5
|
+
require File.dirname(__FILE__) + '/../lib/scope_builder.rb'
|
6
|
+
|
7
|
+
# setup database adapter
|
8
|
+
ActiveRecord::Base.establish_connection({
|
9
|
+
:adapter => "sqlite3",
|
10
|
+
:dbfile => File.dirname(__FILE__) + "/test.sqlite3"
|
11
|
+
})
|
12
|
+
|
13
|
+
# load models
|
14
|
+
# there's probably a better way to handle this
|
15
|
+
require File.dirname(__FILE__) + '/models/product.rb'
|
16
|
+
CreateProducts.migrate(:up) unless Product.table_exists?
|
17
|
+
|
18
|
+
Spec::Runner.configure do |config|
|
19
|
+
config.mock_with :mocha
|
20
|
+
end
|
data/tasks/spec.rake
ADDED
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ryanb-scope-builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Bates
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-26 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Build up named scopes conditionally.
|
17
|
+
email: ryan (at) railscasts (dot) com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- CHANGELOG
|
24
|
+
- lib/scope_builder/builder.rb
|
25
|
+
- lib/scope_builder/model_additions.rb
|
26
|
+
- lib/scope_builder.rb
|
27
|
+
- LICENSE
|
28
|
+
- README
|
29
|
+
- tasks/deployment.rake
|
30
|
+
- tasks/spec.rake
|
31
|
+
- TODO
|
32
|
+
files:
|
33
|
+
- CHANGELOG
|
34
|
+
- lib/scope_builder/builder.rb
|
35
|
+
- lib/scope_builder/model_additions.rb
|
36
|
+
- lib/scope_builder.rb
|
37
|
+
- LICENSE
|
38
|
+
- Manifest
|
39
|
+
- README
|
40
|
+
- spec/models/product.rb
|
41
|
+
- spec/scope_builder/builder_spec.rb
|
42
|
+
- spec/spec_helper.rb
|
43
|
+
- spec/test.sqlite3
|
44
|
+
- tasks/deployment.rake
|
45
|
+
- tasks/spec.rake
|
46
|
+
- TODO
|
47
|
+
- scope-builder.gemspec
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://github.com/ryanb/scope-builder
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options:
|
52
|
+
- --line-numbers
|
53
|
+
- --inline-source
|
54
|
+
- --title
|
55
|
+
- Scope-builder
|
56
|
+
- --main
|
57
|
+
- README
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project: scope-builder
|
75
|
+
rubygems_version: 1.2.0
|
76
|
+
signing_key:
|
77
|
+
specification_version: 2
|
78
|
+
summary: Build up named scopes conditionally.
|
79
|
+
test_files: []
|
80
|
+
|