Skip to content

Commit 0a41a60

Browse files
hibachrachjkeen
authored andcommitted
fix: keep rake task helpers out of the global namespace
Ported from graphiti-api/graphiti-rails#91, following the review there: the helpers move to their own file rather than being defined inside the rake namespace.
1 parent 9c8c1c2 commit 0a41a60

2 files changed

Lines changed: 49 additions & 32 deletions

File tree

lib/graphiti/rails/rake_helpers.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require "graphiti/rails/test_helpers"
2+
3+
module Graphiti
4+
module Rails
5+
# Rake's `namespace` takes a block, and a block does not open a new definee,
6+
# so helpers defined inside one are defined on Object and turn up in every
7+
# request spec in the host app. They live here to stay off that namespace.
8+
module RakeHelpers
9+
extend TestHelpers
10+
11+
module_function
12+
13+
# ::Rails throughout, because bare Rails resolves to Graphiti::Rails here.
14+
def session
15+
@session ||= ActionDispatch::Integration::Session.new(::Rails.application)
16+
end
17+
18+
def setup_rails!
19+
::Rails.application.eager_load!
20+
::Rails.application.config.cache_classes = true
21+
::Rails.application.config.action_controller.perform_caching = false
22+
end
23+
24+
def make_request(path, debug = false)
25+
if path.split("/").length == 2
26+
path = "#{ApplicationResource.endpoint_namespace}#{path}"
27+
end
28+
path << if path.include?("?")
29+
"&cache=bust"
30+
else
31+
"?cache=bust"
32+
end
33+
path = "#{path}&debug=true" if debug
34+
handle_request_exceptions do
35+
headers = {Authorization: ENV["AUTHORIZATION_HEADER"]}.compact
36+
session.get(path.to_s, headers: headers)
37+
end
38+
JSON.parse(session.response.body)
39+
end
40+
end
41+
end
42+
end

lib/tasks/graphiti.rake

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,27 @@
1-
namespace :graphiti do
2-
include Graphiti::Rails::TestHelpers
3-
4-
def session
5-
@session ||= ActionDispatch::Integration::Session.new(Rails.application)
6-
end
1+
require "graphiti/rails/rake_helpers"
72

8-
def setup_rails!
9-
Rails.application.eager_load!
10-
Rails.application.config.cache_classes = true
11-
Rails.application.config.action_controller.perform_caching = false
12-
end
13-
14-
def make_request(path, debug = false)
15-
if path.split("/").length == 2
16-
path = "#{ApplicationResource.endpoint_namespace}#{path}"
17-
end
18-
path << if path.include?("?")
19-
"&cache=bust"
20-
else
21-
"?cache=bust"
22-
end
23-
path = "#{path}&debug=true" if debug
24-
handle_request_exceptions do
25-
headers = {Authorization: ENV["AUTHORIZATION_HEADER"]}.compact
26-
session.get(path.to_s, headers: headers)
27-
end
28-
JSON.parse(session.response.body)
29-
end
3+
namespace :graphiti do
4+
helpers = Graphiti::Rails::RakeHelpers
305

316
desc "Execute request without web server."
327
task :request, [:path, :debug] => [:environment] do |_, args|
33-
setup_rails!
8+
helpers.setup_rails!
349
Graphiti.logger = Graphiti.stdout_logger
3510
Graphiti::Debugger.preserve = true
3611
require "pp"
3712
path, debug = args[:path], args[:debug]
3813
puts "Graphiti Request: #{path}"
39-
json = make_request(path, debug)
14+
json = helpers.make_request(path, debug)
4015
pp json
4116
Graphiti::Debugger.flush if debug
4217
end
4318

4419
desc "Execute benchmark without web server."
4520
task :benchmark, [:path, :requests] => [:environment] do |_, args|
46-
setup_rails!
21+
helpers.setup_rails!
4722
took = Benchmark.ms {
4823
args[:requests].to_i.times do
49-
make_request(args[:path])
24+
helpers.make_request(args[:path])
5025
end
5126
}
5227
puts "Took: #{(took / args[:requests].to_f).round(2)}ms"

0 commit comments

Comments
 (0)