responder_controller 0.1.1 → 0.1.2
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/VERSION +1 -1
- data/lib/responder_controller.rb +9 -3
- data/responder_controller.gemspec +1 -1
- data/spec/responder_controller_spec.rb +7 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/lib/responder_controller.rb
CHANGED
@@ -3,10 +3,18 @@ require 'active_support/core_ext/string/inflections'
|
|
3
3
|
require 'active_support/core_ext/module/delegation'
|
4
4
|
|
5
5
|
module ResponderController
|
6
|
+
class BadScope < StandardError
|
7
|
+
end
|
8
|
+
|
6
9
|
def self.included(mod)
|
7
10
|
mod.extend ClassMethods
|
8
11
|
mod.send :include, InstanceMethods
|
9
12
|
mod.send :include, Actions
|
13
|
+
|
14
|
+
# Uncaught BadScope exceptions become 422s
|
15
|
+
if defined? ActionDispatch
|
16
|
+
ActionDispatch::ShowExceptions.rescue_responses[BadScope.name] = :unprocessable_entity
|
17
|
+
end
|
10
18
|
end
|
11
19
|
|
12
20
|
# Configure how the controller finds and serves models of what flavor.
|
@@ -122,8 +130,6 @@ module ResponderController
|
|
122
130
|
# examining +params+. If any +params+ key matches a name found in
|
123
131
|
# <tt>ClassMethods#model_class.scopes.keys</tt>, then it is taken to be a scope and is
|
124
132
|
# applied. The values under that +params+ key are passed along as arguments.
|
125
|
-
#
|
126
|
-
# TODO: and if the scope taketh arguments not?
|
127
133
|
def scope(query)
|
128
134
|
query = (scopes || []).inject(query) do |query, scope|
|
129
135
|
if Symbol === scope and model_class.scopes.key? scope
|
@@ -137,7 +143,7 @@ module ResponderController
|
|
137
143
|
|
138
144
|
scopes_from_request = (model_class.scopes.keys & params.keys.collect { |k| k.to_sym })
|
139
145
|
query = scopes_from_request.inject(query) do |query, scope|
|
140
|
-
query.send scope, *params[scope.to_s]
|
146
|
+
query.send scope, *params[scope.to_s] rescue raise BadScope.new
|
141
147
|
end
|
142
148
|
|
143
149
|
query
|
@@ -196,6 +196,13 @@ describe "ResponderController" do
|
|
196
196
|
class_level_query.should_receive(:commented_on_by).with('you').and_return(class_level_query)
|
197
197
|
@controller.scope(@query).should == class_level_query
|
198
198
|
end
|
199
|
+
|
200
|
+
it 'throws BadScope for scopes that raise an exception' do
|
201
|
+
@query.should_receive(:commented_on_by).and_raise(ArgumentError.new)
|
202
|
+
lambda do
|
203
|
+
@controller.scope @query
|
204
|
+
end.should raise_error(ResponderController::BadScope)
|
205
|
+
end
|
199
206
|
end
|
200
207
|
|
201
208
|
describe '#find_models' do
|