Skip to content

Commit 4b523de

Browse files
author
Ben Lavender
committed
make everything much worse due to rails 5.1 deprecations
1 parent ffb159f commit 4b523de

3 files changed

Lines changed: 31 additions & 15 deletions

File tree

lib/chatops/controller.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,24 @@ def process(*args)
2424

2525
scrubbed_params.each { |k, v| params[k] = v }
2626

27-
super
27+
if params[:chatop].present?
28+
params[:action] = params[:chatop]
29+
args[0] = params[:action]
30+
unless self.respond_to?(params[:chatop].to_sym)
31+
raise AbstractController::ActionNotFound
32+
end
33+
end
34+
35+
super *args
2836
rescue AbstractController::ActionNotFound
2937
return jsonrpc_method_not_found
3038
end
3139

40+
def execute_chatop
41+
# This needs to exist for route declarations, but we'll be overriding
42+
# things in #process to make a method the action.
43+
end
44+
3245
protected
3346

3447
def jsonrpc_params

lib/chatops/controller/test_case_helpers.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ def chatop(method, params = {})
1212
room_id = args.delete :room_id
1313

1414
params = {
15-
:method => method,
1615
:params => args,
1716
:room_id => room_id,
18-
:user => user,
17+
:user => user
1918
}
2019

2120
if Rails.version.slice(0).to_i >= 5
22-
params = { params: params }
21+
post :execute_chatop, params: params.merge(chatop: method)
22+
else
23+
post :execute_chatop, params.merge(chatop: method)
2324
end
24-
post method, params
2525
end
2626

2727
def chat(message, user, room_id = "123")

spec/lib/chatops/controller_spec.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def ensure_app_given
4040
before :each do
4141
routes.draw do
4242
get "/_chatops" => "anonymous#list"
43-
post "/_chatops/:action", controller: "anonymous"
43+
post "/_chatops/:chatop", controller: "anonymous", action: :execute_chatop
4444
get "/other" => "anonymous#non_chatop_method"
4545
get "/other_will_fail" => "anonymous#unexcluded_chatop_method"
4646
end
@@ -121,7 +121,7 @@ def rails_flexible_post(path, outer_params, jsonrpc_params = nil)
121121
end
122122

123123
it "requires a user be sent to chatops" do
124-
post :foobar
124+
rails_flexible_post :execute_chatop, chatop: :foobar
125125
expect(response.status).to eq 400
126126
expect(json_response).to eq({
127127
"jsonrpc" => "2.0",
@@ -134,7 +134,7 @@ def rails_flexible_post(path, outer_params, jsonrpc_params = nil)
134134
end
135135

136136
it "returns method not found for a not found method" do
137-
rails_flexible_post :barfoo, :user => "foo"
137+
rails_flexible_post :execute_chatop, chatop: :barfoo, user: "foo"
138138
expect(json_response).to eq({
139139
"jsonrpc" => "2.0",
140140
"id" => nil,
@@ -160,7 +160,7 @@ def rails_flexible_post(path, outer_params, jsonrpc_params = nil)
160160
end
161161

162162
it "runs a known method" do
163-
rails_flexible_post :foobar, :user => "foo"
163+
rails_flexible_post :execute_chatop, chatop: :foobar, user: "foo"
164164
expect(json_response).to eq({
165165
"jsonrpc" => "2.0",
166166
"id" => nil,
@@ -170,7 +170,7 @@ def rails_flexible_post(path, outer_params, jsonrpc_params = nil)
170170
end
171171

172172
it "passes parameters to methods" do
173-
rails_flexible_post :wcid, { :user => "foo" }, { "app" => "foo" }
173+
rails_flexible_post :execute_chatop, { :chatop => "wcid", :user => "foo" }, { "app" => "foo" }
174174
expect(json_response).to eq({
175175
"jsonrpc" => "2.0",
176176
"id" => nil,
@@ -180,7 +180,7 @@ def rails_flexible_post(path, outer_params, jsonrpc_params = nil)
180180
end
181181

182182
it "uses typical controller fun like before_action" do
183-
rails_flexible_post :wcid, :user => "foo"
183+
rails_flexible_post :execute_chatop, :chatop => "wcid", :user => "foo"
184184
expect(json_response).to eq({
185185
"jsonrpc" => "2.0",
186186
"id" => nil,
@@ -193,7 +193,7 @@ def rails_flexible_post(path, outer_params, jsonrpc_params = nil)
193193
end
194194

195195
it "allows methods to return invalid params with a message" do
196-
rails_flexible_post :wcid, { :user => "foo" }, { "app" => "nope" }
196+
rails_flexible_post :execute_chatop, { :chatop => "wcid", :user => "foo" }, { "app" => "nope" }
197197
expect(response.status).to eq 400
198198
expect(json_response).to eq({
199199
"jsonrpc" => "2.0",
@@ -220,15 +220,17 @@ def rails_flexible_post(path, outer_params, jsonrpc_params = nil)
220220
context "regex-based test helpers" do
221221
it "routes based on regexes from test helpers" do
222222
chat "where can i deploy foobar", "bhuga"
223-
expect(request.params["action"]).to eq "wcid"
223+
expect(request.params["action"]).to eq "execute_chatop"
224+
expect(request.params["chatop"]).to eq "wcid"
224225
expect(request.params["user"]).to eq "bhuga"
225226
expect(request.params["params"]["app"]).to eq "foobar"
226227
expect(chatop_response).to eq "You can deploy foobar just fine."
227228
end
228229

229230
it "works with generic arguments" do
230231
chat "where can i deploy foobar --fruit apple --vegetable green celery", "bhuga"
231-
expect(request.params["action"]).to eq "wcid"
232+
expect(request.params["action"]).to eq "execute_chatop"
233+
expect(request.params["chatop"]).to eq "wcid"
232234
expect(request.params["user"]).to eq "bhuga"
233235
expect(request.params["params"]["app"]).to eq "foobar"
234236
expect(request.params["params"]["fruit"]).to eq "apple"
@@ -238,7 +240,8 @@ def rails_flexible_post(path, outer_params, jsonrpc_params = nil)
238240

239241
it "works with boolean arguments" do
240242
chat "where can i deploy foobar --this-is-sparta", "bhuga"
241-
expect(request.params["action"]).to eq "wcid"
243+
expect(request.params["action"]).to eq "execute_chatop"
244+
expect(request.params["chatop"]).to eq "wcid"
242245
expect(request.params["user"]).to eq "bhuga"
243246
expect(request.params["params"]["this-is-sparta"]).to eq "true"
244247
end

0 commit comments

Comments
 (0)