Skip to content

Commit

Permalink
Fix Rails 5 support
Browse files Browse the repository at this point in the history
It turns out that ActionDispatch::Request no longer inherits from
Rack::Request in Rails 5, so we had to add in direct support for
ActionDispatch::Request.
  • Loading branch information
adamcrown committed Nov 5, 2016
1 parent 5fa126c commit 113c299
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
3 changes: 2 additions & 1 deletion lib/voight_kampff.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'json'

require 'voight_kampff/test'
require 'voight_kampff/rack_request' if defined?(Rack)
require 'voight_kampff/methods'
require 'voight_kampff/rack_request' if defined?(Rack::Request)
require 'voight_kampff/engine' if defined?(Rails)

module VoightKampff
Expand Down
6 changes: 6 additions & 0 deletions lib/voight_kampff/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@ class Engine < Rails::Engine
rake_tasks do
load 'tasks/voight_kampff.rake'
end

initializer :add_voight_kampff_methods do |app|
ActionDispatch::Request.class_eval do
include VoightKampff::Methods
end
end
end
end
10 changes: 10 additions & 0 deletions lib/voight_kampff/methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module VoightKampff::Methods
def human?
VoightKampff::Test.new(user_agent).human?
end

def bot?
VoightKampff::Test.new(user_agent).bot?
end
alias :replicant? :bot?
end
11 changes: 2 additions & 9 deletions lib/voight_kampff/rack_request.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
# Reopen the Rack::Request class to add bot detection methods
class Rack::Request
def human?
VoightKampff::Test.new(user_agent).human?
end

def bot?
VoightKampff::Test.new(user_agent).bot?
end
alias :replicant? :bot?
Rack::Request.class_eval do
include VoightKampff::Methods
end
2 changes: 1 addition & 1 deletion spec/controllers/replicants_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe ReplicantsController, type: :controller do
let(:user_agent_string) { '' }
before do
expect(request).to receive(:user_agent).and_return user_agent_string
expect_any_instance_of(ActionController::TestRequest).to receive(:user_agent).and_return user_agent_string
get :index
end

Expand Down
2 changes: 1 addition & 1 deletion voight_kampff.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |s|

s.add_dependency 'rack', ['>= 1.4', '< 3.0']

s.add_development_dependency 'rails', '~> 4.2'
s.add_development_dependency 'rails', '~> 5.0'
s.add_development_dependency 'rspec-rails', '~> 3.3'
s.add_development_dependency 'combustion', '~> 0.5'
end

0 comments on commit 113c299

Please sign in to comment.