has_scope 0.4 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +6 -13
- data/Rakefile +1 -1
- data/lib/has_scope.rb +10 -11
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -22,7 +22,7 @@ Now, if you want to apply them to an specific resource, you just need to call <t
|
|
22
22
|
has_scope :by_degree
|
23
23
|
|
24
24
|
def index
|
25
|
-
@graduations = apply_scopes(
|
25
|
+
@graduations = apply_scopes(Graduation).all
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -54,26 +54,19 @@ If you want it as plugin, just do:
|
|
54
54
|
|
55
55
|
HasScope supports several options:
|
56
56
|
|
57
|
-
* <tt>:type</tt> - Checks the type of the parameter sent. If set to :boolean
|
58
|
-
it just calls the named scope, without any argument. By default,
|
59
|
-
it does not allow hashes or arrays to be given, except if type
|
60
|
-
:hash or :array are set.
|
57
|
+
* <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.
|
61
58
|
|
62
59
|
* <tt>:only</tt> - In which actions the scope is applied.
|
63
60
|
|
64
61
|
* <tt>:except</tt> - In which actions the scope is not applied.
|
65
62
|
|
66
|
-
* <tt>:as</tt> - The key in the params hash expected to find the scope.
|
67
|
-
Defaults to the scope name.
|
63
|
+
* <tt>:as</tt> - The key in the params hash expected to find the scope. Defaults to the scope name.
|
68
64
|
|
69
|
-
* <tt>:if</tt> - Specifies a method, proc or string to call to determine
|
70
|
-
if the scope should apply
|
65
|
+
* <tt>:if</tt> - Specifies a method, proc or string to call to determine if the scope should apply.
|
71
66
|
|
72
|
-
* <tt>:unless</tt> - Specifies a method, proc or string to call to determine
|
73
|
-
if the scope should NOT apply.
|
67
|
+
* <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the scope should NOT apply.
|
74
68
|
|
75
|
-
* <tt>:default</tt> - Default value for the scope. Whenever supplied the scope
|
76
|
-
is always called.
|
69
|
+
* <tt>:default</tt> - Default value for the scope. Whenever supplied the scope is always called.
|
77
70
|
|
78
71
|
* <tt>:allow_blank</tt> - Blank values are not sent to scopes by default. Set to true to overwrite.
|
79
72
|
|
data/Rakefile
CHANGED
@@ -27,7 +27,7 @@ begin
|
|
27
27
|
require 'jeweler'
|
28
28
|
Jeweler::Tasks.new do |s|
|
29
29
|
s.name = "has_scope"
|
30
|
-
s.version = "0.4"
|
30
|
+
s.version = "0.4.1"
|
31
31
|
s.summary = "Maps controller filters to your resource scopes"
|
32
32
|
s.email = "contact@plataformatec.com.br"
|
33
33
|
s.homepage = "http://github.com/plataformatec/has_scope"
|
data/lib/has_scope.rb
CHANGED
@@ -12,9 +12,7 @@ module HasScope
|
|
12
12
|
base.class_eval do
|
13
13
|
extend ClassMethods
|
14
14
|
helper_method :current_scopes
|
15
|
-
|
16
15
|
class_inheritable_hash :scopes_configuration, :instance_writer => false
|
17
|
-
self.scopes_configuration ||= {}
|
18
16
|
end
|
19
17
|
end
|
20
18
|
|
@@ -63,16 +61,13 @@ module HasScope
|
|
63
61
|
def has_scope(*scopes, &block)
|
64
62
|
options = scopes.extract_options!
|
65
63
|
options.symbolize_keys!
|
66
|
-
|
67
|
-
if options.delete(:boolean)
|
68
|
-
options[:type] ||= :boolean
|
69
|
-
ActiveSupport::Deprecation.warn(":boolean => true is deprecated, use :type => :boolean instead", caller)
|
70
|
-
end
|
71
64
|
options.assert_valid_keys(:type, :only, :except, :if, :unless, :default, :as, :allow_blank)
|
72
65
|
|
73
66
|
options[:only] = Array(options[:only])
|
74
67
|
options[:except] = Array(options[:except])
|
75
68
|
|
69
|
+
self.scopes_configuration ||= {}
|
70
|
+
|
76
71
|
scopes.each do |scope|
|
77
72
|
self.scopes_configuration[scope] ||= { :as => scope, :type => :default, :block => block }
|
78
73
|
self.scopes_configuration[scope].merge!(options)
|
@@ -94,6 +89,8 @@ module HasScope
|
|
94
89
|
# end
|
95
90
|
#
|
96
91
|
def apply_scopes(target)
|
92
|
+
return target unless scopes_configuration
|
93
|
+
|
97
94
|
self.scopes_configuration.each do |scope, options|
|
98
95
|
next unless apply_scope_to_action?(options)
|
99
96
|
key = options[:as]
|
@@ -119,7 +116,7 @@ module HasScope
|
|
119
116
|
if type == :boolean
|
120
117
|
current_scopes[key] = TRUE_VALUES.include?(value)
|
121
118
|
elsif ALLOWED_TYPES[type].none?{ |klass| value.is_a?(klass) }
|
122
|
-
raise "Expected type :#{type} in params[:#{key}], got
|
119
|
+
raise "Expected type :#{type} in params[:#{key}], got #{value.class}"
|
123
120
|
else
|
124
121
|
current_scopes[key] = value
|
125
122
|
end
|
@@ -127,10 +124,12 @@ module HasScope
|
|
127
124
|
|
128
125
|
# Apply the scope taking into account its type.
|
129
126
|
def apply_scope_by_type(type, scope, target, value, block) #:nodoc:
|
130
|
-
return target if type == :boolean && value == false
|
131
|
-
|
132
127
|
if type == :boolean
|
133
|
-
|
128
|
+
if value
|
129
|
+
block ? block.call(self, target) : target.send(scope)
|
130
|
+
else
|
131
|
+
target
|
132
|
+
end
|
134
133
|
else
|
135
134
|
block ? block.call(self, target, value) : target.send(scope, value)
|
136
135
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_scope
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Jos\xC3\xA9 Valim"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-01-29 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|