has_scope 0.5.1 → 0.6.0.rc
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -1
- data/.travis.yml +14 -0
- data/Gemfile +5 -10
- data/Gemfile.lock +48 -0
- data/MIT-LICENSE +1 -1
- data/README.md +115 -0
- data/Rakefile +5 -3
- data/gemfiles/Gemfile-rails.3.2.x +8 -0
- data/has_scope.gemspec +6 -2
- data/lib/has_scope.rb +17 -4
- data/lib/has_scope/version.rb +2 -2
- data/test/has_scope_test.rb +57 -8
- data/test/test_helper.rb +2 -3
- metadata +72 -50
- data/README.rdoc +0 -101
- data/init.rb +0 -1
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: eb5c20d9ff1cb1bbd626b15574b9159fd50dfa47
|
4
|
+
data.tar.gz: 783dd0bc45a0bf8d97a16c77bdee0f24a52c926a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 56bc3e1fc2043a227dd8bf18e34896e6b5c6d5693a61be1ef45b49f13cf035157a572efffdde6b3e95ef1f230cd498a8d044a004689635eb0367d7cdbf607efc
|
7
|
+
data.tar.gz: 9cd682408a65660d72646f32a9447ceca58d498af77fdc502ea7ad0f9c39ff2aaa3f5a3fcb0f907c75ec99f6c62e7af3837c762b70e5dd1836c0b902b3a5b643
|
data/.gitignore
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
.bundle
|
2
|
-
|
2
|
+
gemfiles/*.lock
|
data/.travis.yml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.9.3
|
4
|
+
- 2.0.0
|
5
|
+
gemfile:
|
6
|
+
- Gemfile
|
7
|
+
- gemfiles/Gemfile-rails.3.2.x
|
8
|
+
notifications:
|
9
|
+
email: false
|
10
|
+
campfire:
|
11
|
+
on_success: change
|
12
|
+
on_failure: always
|
13
|
+
rooms:
|
14
|
+
- secure: "kysymH2KAtGgNLcWPaNLVENtghORHJC6yUxGzJ5y+JrzUwkr4gTOv0nMyw5k\nsHCN1F1mJtbiXxFOZpxDaWMSGVhP7ThRy0esovkfzkAHihbyPClX241eJcyD\nI3f7/BZN4gfiR+Mbml2frLKHOtEtrm2h0gIEsXJ/YL+Ysf0nxHw="
|
data/Gemfile
CHANGED
@@ -1,13 +1,8 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
gemspec
|
4
4
|
|
5
|
-
gem
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
else
|
10
|
-
gem "test-unit"
|
11
|
-
end
|
12
|
-
|
13
|
-
gem "mocha"
|
5
|
+
gem 'actionpack', '~> 4.0.0.rc1'
|
6
|
+
gem 'activesupport', '~> 4.0.0.rc1'
|
7
|
+
gem 'mocha', '~> 0.13.2', require: false
|
8
|
+
gem 'rake'
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
has_scope (0.6.0.rc)
|
5
|
+
actionpack (>= 3.2, < 5)
|
6
|
+
activesupport (>= 3.2, < 5)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionpack (4.0.0.rc1)
|
12
|
+
activesupport (= 4.0.0.rc1)
|
13
|
+
builder (~> 3.1.0)
|
14
|
+
erubis (~> 2.7.0)
|
15
|
+
rack (~> 1.5.2)
|
16
|
+
rack-test (~> 0.6.2)
|
17
|
+
activesupport (4.0.0.rc1)
|
18
|
+
i18n (~> 0.6, >= 0.6.4)
|
19
|
+
minitest (~> 4.2)
|
20
|
+
multi_json (~> 1.3)
|
21
|
+
thread_safe (~> 0.1)
|
22
|
+
tzinfo (~> 0.3.37)
|
23
|
+
atomic (1.1.9)
|
24
|
+
builder (3.1.4)
|
25
|
+
erubis (2.7.0)
|
26
|
+
i18n (0.6.4)
|
27
|
+
metaclass (0.0.1)
|
28
|
+
minitest (4.7.4)
|
29
|
+
mocha (0.13.3)
|
30
|
+
metaclass (~> 0.0.1)
|
31
|
+
multi_json (1.7.3)
|
32
|
+
rack (1.5.2)
|
33
|
+
rack-test (0.6.2)
|
34
|
+
rack (>= 1.0)
|
35
|
+
rake (10.0.4)
|
36
|
+
thread_safe (0.1.0)
|
37
|
+
atomic
|
38
|
+
tzinfo (0.3.37)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
ruby
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
actionpack (~> 4.0.0.rc1)
|
45
|
+
activesupport (~> 4.0.0.rc1)
|
46
|
+
has_scope!
|
47
|
+
mocha (~> 0.13.2)
|
48
|
+
rake
|
data/MIT-LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright 2009 Plataforma Tecnologia. http://blog.plataformatec.com.br
|
1
|
+
Copyright 2009-2013 Plataforma Tecnologia. http://blog.plataformatec.com.br
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
a copy of this software and associated documentation files (the
|
data/README.md
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
## HasScope
|
2
|
+
|
3
|
+
[![Gem Version](https://fury-badge.herokuapp.com/rb/has_scope.png)](http://badge.fury.io/rb/has_scope)
|
4
|
+
[![Build Status](https://api.travis-ci.org/plataformatec/has_scope.png?branch=master)](http://travis-ci.org/plataformatec/has_scope)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/plataformatec/has_scope.png)](https://codeclimate.com/github/plataformatec/has_scope)
|
6
|
+
|
7
|
+
Has scope allows you to easily create controller filters based on your resources named scopes.
|
8
|
+
Imagine the following model called graduations:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
class Graduation < ActiveRecord::Base
|
12
|
+
scope :featured, -> { where(:featured => true) }
|
13
|
+
scope :by_degree, -> degree { where(:degree => degree) }
|
14
|
+
end
|
15
|
+
```
|
16
|
+
|
17
|
+
You can use those named scopes as filters by declaring them on your controller:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class GraduationsController < ApplicationController
|
21
|
+
has_scope :featured, :type => :boolean
|
22
|
+
has_scope :by_degree
|
23
|
+
end
|
24
|
+
```
|
25
|
+
|
26
|
+
Now, if you want to apply them to an specific resource, you just need to call `apply_scopes`:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class GraduationsController < ApplicationController
|
30
|
+
has_scope :featured, :type => :boolean
|
31
|
+
has_scope :by_degree
|
32
|
+
has_scope :by_period, :using => [:started_at, :ended_at]
|
33
|
+
|
34
|
+
def index
|
35
|
+
@graduations = apply_scopes(Graduation).all
|
36
|
+
end
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
Then for each request:
|
41
|
+
|
42
|
+
```
|
43
|
+
/graduations
|
44
|
+
#=> acts like a normal request
|
45
|
+
|
46
|
+
/graduations?featured=true
|
47
|
+
#=> calls the named scope and bring featured graduations
|
48
|
+
|
49
|
+
/graduations?params[by_period][started_at]=20100701¶ms[by_period][ended_at]=20101013
|
50
|
+
#=> brings graduations in the given period
|
51
|
+
|
52
|
+
/graduations?featured=true&by_degree=phd
|
53
|
+
#=> brings featured graduations with phd degree
|
54
|
+
```
|
55
|
+
|
56
|
+
You can retrieve all the scopes applied in one action with `current_scopes` method.
|
57
|
+
In the last case, it would return: { :featured => true, :by_degree => "phd" }.
|
58
|
+
|
59
|
+
## Installation
|
60
|
+
|
61
|
+
Add `has_scope` to your Gemfile or install it from Rubygems.
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
gem 'has_scope'
|
65
|
+
```
|
66
|
+
|
67
|
+
## Options
|
68
|
+
|
69
|
+
HasScope supports several options:
|
70
|
+
|
71
|
+
* `:type` - Checks the type of the parameter sent. If set to :boolean it just calls the named scope, without any argument. By default, it does not allow hashes or arrays to be given, except if type :hash or :array are set.
|
72
|
+
|
73
|
+
* `:only` - In which actions the scope is applied.
|
74
|
+
|
75
|
+
* `:except` - In which actions the scope is not applied.
|
76
|
+
|
77
|
+
* `:as` - The key in the params hash expected to find the scope. Defaults to the scope name.
|
78
|
+
|
79
|
+
* `:using` - The subkeys to be used as args when type is a hash.
|
80
|
+
|
81
|
+
* `:if` - Specifies a method, proc or string to call to determine if the scope should apply.
|
82
|
+
|
83
|
+
* `:unless` - Specifies a method, proc or string to call to determine if the scope should NOT apply.
|
84
|
+
|
85
|
+
* `:default` - Default value for the scope. Whenever supplied the scope is always called.
|
86
|
+
|
87
|
+
* `:allow_blank` - Blank values are not sent to scopes by default. Set to true to overwrite.
|
88
|
+
|
89
|
+
## Block usage
|
90
|
+
|
91
|
+
`has_scope` also accepts a block. The controller, current scope and value are yielded
|
92
|
+
to the block so the user can apply the scope on its own. This is useful in case we
|
93
|
+
need to manipulate the given value:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
has_scope :category do |controller, scope, value|
|
97
|
+
value != "all" ? scope.by_category(value) : scope
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
When used with booleans, it just receives two arguments and is just invoked if true is given:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
has_scope :not_voted_by_me, :type => :boolean do |controller, scope|
|
105
|
+
scope.not_voted_by(controller.current_user.id)
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
## Bugs and Feedback
|
110
|
+
|
111
|
+
If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
|
112
|
+
|
113
|
+
http://github.com/plataformatec/has_scope/issues
|
114
|
+
|
115
|
+
MIT License. Copyright 2009-2013 Plataformatec. http://blog.plataformatec.com.br
|
data/Rakefile
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
-
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
|
3
5
|
require 'rake/testtask'
|
4
6
|
require 'rdoc/task'
|
5
7
|
|
@@ -19,6 +21,6 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
19
21
|
rdoc.rdoc_dir = 'rdoc'
|
20
22
|
rdoc.title = 'HasScope'
|
21
23
|
rdoc.options << '--line-numbers' << '--inline-source'
|
22
|
-
rdoc.rdoc_files.include('README.
|
24
|
+
rdoc.rdoc_files.include('README.md')
|
23
25
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
-
end
|
26
|
+
end
|
data/has_scope.gemspec
CHANGED
@@ -11,6 +11,7 @@ Gem::Specification.new do |s|
|
|
11
11
|
s.homepage = "http://github.com/plataformatec/has_scope"
|
12
12
|
s.description = "Maps controller filters to your resource scopes"
|
13
13
|
s.authors = ['José Valim']
|
14
|
+
s.license = 'MIT'
|
14
15
|
|
15
16
|
s.rubyforge_project = "has_scope"
|
16
17
|
|
@@ -21,6 +22,9 @@ Gem::Specification.new do |s|
|
|
21
22
|
|
22
23
|
s.rdoc_options = ["--charset=UTF-8"]
|
23
24
|
s.extra_rdoc_files = [
|
24
|
-
"README.
|
25
|
+
"README.md"
|
25
26
|
]
|
26
|
-
|
27
|
+
|
28
|
+
s.add_runtime_dependency "actionpack", ">= 3.2", "< 5"
|
29
|
+
s.add_runtime_dependency "activesupport", ">= 3.2", "< 5"
|
30
|
+
end
|
data/lib/has_scope.rb
CHANGED
@@ -11,7 +11,6 @@ module HasScope
|
|
11
11
|
def self.included(base)
|
12
12
|
base.class_eval do
|
13
13
|
extend ClassMethods
|
14
|
-
helper_method :current_scopes
|
15
14
|
class_attribute :scopes_configuration, :instance_writer => false
|
16
15
|
end
|
17
16
|
end
|
@@ -116,6 +115,7 @@ module HasScope
|
|
116
115
|
end
|
117
116
|
|
118
117
|
value = parse_value(options[:type], key, value)
|
118
|
+
value = normalize_blanks(value)
|
119
119
|
|
120
120
|
if call_scope && (value.present? || options[:allow_blank])
|
121
121
|
current_scopes[key] = value
|
@@ -130,8 +130,18 @@ module HasScope
|
|
130
130
|
def parse_value(type, key, value) #:nodoc:
|
131
131
|
if type == :boolean
|
132
132
|
TRUE_VALUES.include?(value)
|
133
|
-
elsif value && ALLOWED_TYPES[type].
|
134
|
-
|
133
|
+
elsif value && ALLOWED_TYPES[type].any?{ |klass| value.is_a?(klass) }
|
134
|
+
value
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
# Screens pseudo-blank params.
|
139
|
+
def normalize_blanks(value) #:nodoc:
|
140
|
+
return value if value.nil?
|
141
|
+
if value.is_a?(Array)
|
142
|
+
value.select { |v| v.present? }
|
143
|
+
elsif value.is_a?(Hash)
|
144
|
+
value.select { |k, v| normalize_blanks(v).present? }.with_indifferent_access
|
135
145
|
else
|
136
146
|
value
|
137
147
|
end
|
@@ -184,4 +194,7 @@ module HasScope
|
|
184
194
|
end
|
185
195
|
end
|
186
196
|
|
187
|
-
|
197
|
+
ActiveSupport.on_load :action_controller do
|
198
|
+
include HasScope
|
199
|
+
helper_method :current_scopes
|
200
|
+
end
|
data/lib/has_scope/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module HasScope
|
2
|
-
VERSION = "0.
|
3
|
-
end
|
2
|
+
VERSION = "0.6.0.rc"
|
3
|
+
end
|
data/test/has_scope_test.rb
CHANGED
@@ -161,6 +161,33 @@ class HasScopeTest < ActionController::TestCase
|
|
161
161
|
assert_equal({ :args_paginate => hash }, current_scopes)
|
162
162
|
end
|
163
163
|
|
164
|
+
def test_hash_with_blank_values_is_ignored
|
165
|
+
hash = { "page" => "", "per_page" => "" }
|
166
|
+
Tree.expects(:paginate).never
|
167
|
+
Tree.expects(:all).returns([mock_tree])
|
168
|
+
get :index, :paginate => hash
|
169
|
+
assert_equal([mock_tree], assigns(:trees))
|
170
|
+
assert_equal({ }, current_scopes)
|
171
|
+
end
|
172
|
+
|
173
|
+
def test_nested_hash_with_blank_values_is_ignored
|
174
|
+
hash = { "parent" => {"children" => ""} }
|
175
|
+
Tree.expects(:paginate).never
|
176
|
+
Tree.expects(:all).returns([mock_tree])
|
177
|
+
get :index, :paginate => hash
|
178
|
+
assert_equal([mock_tree], assigns(:trees))
|
179
|
+
assert_equal({ }, current_scopes)
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_nested_blank_array_param_is_ignored
|
183
|
+
hash = { "parent" => [""] }
|
184
|
+
Tree.expects(:paginate).never
|
185
|
+
Tree.expects(:all).returns([mock_tree])
|
186
|
+
get :index, :paginate => hash
|
187
|
+
assert_equal([mock_tree], assigns(:trees))
|
188
|
+
assert_equal({ }, current_scopes)
|
189
|
+
end
|
190
|
+
|
164
191
|
def test_scope_of_type_array
|
165
192
|
array = %w(book kitchen sport)
|
166
193
|
Tree.expects(:categories).with(array).returns(Tree)
|
@@ -170,16 +197,19 @@ class HasScopeTest < ActionController::TestCase
|
|
170
197
|
assert_equal({ :categories => array }, current_scopes)
|
171
198
|
end
|
172
199
|
|
173
|
-
def
|
174
|
-
|
175
|
-
|
176
|
-
|
200
|
+
def test_array_of_blank_values_is_ignored
|
201
|
+
Tree.expects(:categories).never
|
202
|
+
Tree.expects(:all).returns([mock_tree])
|
203
|
+
get :index, :categories => [""]
|
204
|
+
assert_equal([mock_tree], assigns(:trees))
|
205
|
+
assert_equal({ }, current_scopes)
|
177
206
|
end
|
178
207
|
|
179
|
-
def
|
180
|
-
|
181
|
-
|
182
|
-
|
208
|
+
def test_scope_of_invalid_type_silently_fails
|
209
|
+
Tree.expects(:all).returns([mock_tree])
|
210
|
+
get :index, :paginate => "1"
|
211
|
+
assert_equal([mock_tree], assigns(:trees))
|
212
|
+
assert_equal({}, current_scopes)
|
183
213
|
end
|
184
214
|
|
185
215
|
def test_scope_is_called_with_default_value
|
@@ -247,3 +277,22 @@ class HasScopeTest < ActionController::TestCase
|
|
247
277
|
end
|
248
278
|
end
|
249
279
|
|
280
|
+
class TreeHugger
|
281
|
+
include HasScope
|
282
|
+
|
283
|
+
has_scope :color
|
284
|
+
|
285
|
+
def by_color
|
286
|
+
apply_scopes(Tree, :color => 'blue')
|
287
|
+
end
|
288
|
+
|
289
|
+
end
|
290
|
+
|
291
|
+
class HasScopeOutsideControllerTest < ActiveSupport::TestCase
|
292
|
+
|
293
|
+
def test_has_scope_usable_outside_controller
|
294
|
+
Tree.expects(:color).with('blue')
|
295
|
+
TreeHugger.new.by_color
|
296
|
+
end
|
297
|
+
|
298
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
|
-
require 'rubygems'
|
2
1
|
require 'bundler'
|
3
2
|
|
4
3
|
Bundler.setup
|
5
4
|
require 'test/unit'
|
6
|
-
require 'mocha'
|
5
|
+
require 'mocha/setup'
|
7
6
|
|
8
7
|
# Configure Rails
|
9
8
|
ENV["RAILS_ENV"] = "test"
|
@@ -17,7 +16,7 @@ require 'has_scope'
|
|
17
16
|
|
18
17
|
HasScope::Routes = ActionDispatch::Routing::RouteSet.new
|
19
18
|
HasScope::Routes.draw do
|
20
|
-
|
19
|
+
get '/:controller(/:action(/:id))'
|
21
20
|
end
|
22
21
|
|
23
22
|
class ApplicationController < ActionController::Base
|
metadata
CHANGED
@@ -1,78 +1,100 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_scope
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 5
|
9
|
-
- 1
|
10
|
-
version: 0.5.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.6.0.rc
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
13
|
-
-
|
6
|
+
authors:
|
7
|
+
- José Valim
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
11
|
+
date: 2013-05-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionpack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
- - <
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.2'
|
30
|
+
- - <
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: activesupport
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '3.2'
|
40
|
+
- - <
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '5'
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '3.2'
|
50
|
+
- - <
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '5'
|
22
53
|
description: Maps controller filters to your resource scopes
|
23
54
|
email: developers@plataformatec.com.br
|
24
55
|
executables: []
|
25
|
-
|
26
56
|
extensions: []
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
files:
|
57
|
+
extra_rdoc_files:
|
58
|
+
- README.md
|
59
|
+
files:
|
31
60
|
- .gitignore
|
61
|
+
- .travis.yml
|
32
62
|
- Gemfile
|
63
|
+
- Gemfile.lock
|
33
64
|
- MIT-LICENSE
|
34
|
-
- README.
|
65
|
+
- README.md
|
35
66
|
- Rakefile
|
67
|
+
- gemfiles/Gemfile-rails.3.2.x
|
36
68
|
- has_scope.gemspec
|
37
|
-
- init.rb
|
38
69
|
- lib/has_scope.rb
|
39
70
|
- lib/has_scope/version.rb
|
40
71
|
- test/has_scope_test.rb
|
41
72
|
- test/test_helper.rb
|
42
|
-
has_rdoc: true
|
43
73
|
homepage: http://github.com/plataformatec/has_scope
|
44
|
-
licenses:
|
45
|
-
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
46
77
|
post_install_message:
|
47
|
-
rdoc_options:
|
78
|
+
rdoc_options:
|
48
79
|
- --charset=UTF-8
|
49
|
-
require_paths:
|
80
|
+
require_paths:
|
50
81
|
- lib
|
51
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
none: false
|
62
|
-
requirements:
|
63
|
-
- - ">="
|
64
|
-
- !ruby/object:Gem::Version
|
65
|
-
hash: 3
|
66
|
-
segments:
|
67
|
-
- 0
|
68
|
-
version: "0"
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - '>'
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: 1.3.1
|
69
92
|
requirements: []
|
70
|
-
|
71
93
|
rubyforge_project: has_scope
|
72
|
-
rubygems_version:
|
94
|
+
rubygems_version: 2.0.3
|
73
95
|
signing_key:
|
74
|
-
specification_version:
|
96
|
+
specification_version: 4
|
75
97
|
summary: Maps controller filters to your resource scopes.
|
76
|
-
test_files:
|
98
|
+
test_files:
|
77
99
|
- test/has_scope_test.rb
|
78
100
|
- test/test_helper.rb
|
data/README.rdoc
DELETED
@@ -1,101 +0,0 @@
|
|
1
|
-
== HasScope
|
2
|
-
|
3
|
-
Has scope allows you to easily create controller filters based on your resources named scopes.
|
4
|
-
Imagine the following model called graduations:
|
5
|
-
|
6
|
-
class Graduation < ActiveRecord::Base
|
7
|
-
named_scope :featured, :conditions => { :featured => true }
|
8
|
-
named_scope :by_degree, proc {|degree| { :conditions => { :degree => degree } } }
|
9
|
-
end
|
10
|
-
|
11
|
-
You can use those named scopes as filters by declaring them on your controller:
|
12
|
-
|
13
|
-
class GraduationsController < ApplicationController
|
14
|
-
has_scope :featured, :type => :boolean
|
15
|
-
has_scope :by_degree
|
16
|
-
end
|
17
|
-
|
18
|
-
Now, if you want to apply them to an specific resource, you just need to call <tt>apply_scopes</tt>:
|
19
|
-
|
20
|
-
class GraduationsController < ApplicationController
|
21
|
-
has_scope :featured, :type => :boolean
|
22
|
-
has_scope :by_degree
|
23
|
-
has_scope :by_period, :using => [:started_at, :ended_at]
|
24
|
-
|
25
|
-
def index
|
26
|
-
@graduations = apply_scopes(Graduation).all
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
Then for each request:
|
31
|
-
|
32
|
-
/graduations
|
33
|
-
#=> acts like a normal request
|
34
|
-
|
35
|
-
/graduations?featured=true
|
36
|
-
#=> calls the named scope and bring featured graduations
|
37
|
-
|
38
|
-
/graduations?params[by_period][started_at]=20100701¶ms[by_period][ended_at]=20101013
|
39
|
-
#=> brings graduations in the given period
|
40
|
-
|
41
|
-
/graduations?featured=true&by_degree=phd
|
42
|
-
#=> brings featured graduations with phd degree
|
43
|
-
|
44
|
-
You can retrieve all the scopes applied in one action with <tt>current_scopes</tt> method.
|
45
|
-
In the last case, it would return: { :featured => true, :by_degree => "phd" }.
|
46
|
-
|
47
|
-
== Installation
|
48
|
-
|
49
|
-
HasScope is available as gem on Gemcutter, so just run the following:
|
50
|
-
|
51
|
-
sudo gem install has_scope
|
52
|
-
|
53
|
-
If you want it as plugin, just do:
|
54
|
-
|
55
|
-
script/plugin install git://github.com/plataformatec/has_scope.git
|
56
|
-
|
57
|
-
== Options
|
58
|
-
|
59
|
-
HasScope supports several options:
|
60
|
-
|
61
|
-
* <tt>:type</tt> - Checks the type of the parameter sent. If set to :boolean it just calls the named scope, without any argument. By default, it does not allow hashes or arrays to be given, except if type :hash or :array are set.
|
62
|
-
|
63
|
-
* <tt>:only</tt> - In which actions the scope is applied.
|
64
|
-
|
65
|
-
* <tt>:except</tt> - In which actions the scope is not applied.
|
66
|
-
|
67
|
-
* <tt>:as</tt> - The key in the params hash expected to find the scope. Defaults to the scope name.
|
68
|
-
|
69
|
-
* <tt>:using</tt> - The subkeys to be used as args when type is a hash.
|
70
|
-
|
71
|
-
* <tt>:if</tt> - Specifies a method, proc or string to call to determine if the scope should apply.
|
72
|
-
|
73
|
-
* <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the scope should NOT apply.
|
74
|
-
|
75
|
-
* <tt>:default</tt> - Default value for the scope. Whenever supplied the scope is always called.
|
76
|
-
|
77
|
-
* <tt>:allow_blank</tt> - Blank values are not sent to scopes by default. Set to true to overwrite.
|
78
|
-
|
79
|
-
== Block usage
|
80
|
-
|
81
|
-
has_scope also accepts a block. The controller, current scope and value are yielded
|
82
|
-
to the block so the user can apply the scope on its own. This is useful in case we
|
83
|
-
need to manipulate the given value:
|
84
|
-
|
85
|
-
has_scope :category do |controller, scope, value|
|
86
|
-
value != "all" ? scope.by_category(value) : scope
|
87
|
-
end
|
88
|
-
|
89
|
-
When used with booleans, it just receives two arguments and is just invoked if true is given:
|
90
|
-
|
91
|
-
has_scope :not_voted_by_me, :type => :boolean do |controller, scope|
|
92
|
-
scope.not_voted_by(controller.current_user.id)
|
93
|
-
end
|
94
|
-
|
95
|
-
== Bugs and Feedback
|
96
|
-
|
97
|
-
If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
|
98
|
-
|
99
|
-
http://github.com/plataformatec/has_scope/issues
|
100
|
-
|
101
|
-
MIT License. Copyright 2009 Plataforma Tecnologia. http://blog.plataformatec.com.br
|
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'has_scope'
|