Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes SQL exception from rails 5.2 upgrade #479

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ gemfile:
- gemfiles/activerecord_4.2.gemfile
- gemfiles/activerecord_5.0.2.gemfile
- gemfiles/activerecord_5.1.0.gemfile
- gemfiles/activerecord_5.2.0.beta2.gemfile
services:
- mongodb
matrix:
Expand All @@ -33,6 +34,10 @@ matrix:
gemfile: gemfiles/activerecord_5.1.0.gemfile
- rvm: jruby-9.1.9.0
gemfile: gemfiles/activerecord_5.1.0.gemfile
- rvm: jruby-9.0.5.0
gemfile: gemfiles/activerecord_5.2.0.beta2.gemfile
- rvm: jruby-9.1.9.0
gemfile: gemfiles/activerecord_5.2.0.beta2.gemfile
notifications:
email:
recipients:
Expand Down
16 changes: 16 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,19 @@ appraise 'activerecord_5.1.0' do
gem 'pg', '~> 0.21'
end
end

appraise 'activerecord_5.2.0.beta2' do
gem 'activerecord', '~> 5.2.0.beta2', require: 'active_record'
gem 'activesupport', '~> 5.2.0.beta2', require: 'active_support/all'
gem 'actionpack', '~> 5.2.0.beta2', require: 'action_pack'

gemfile.platforms :jruby do
gem 'activerecord-jdbcsqlite3-adapter'
gem 'jdbc-sqlite3'
end

gemfile.platforms :ruby, :mswin, :mingw do
gem 'sqlite3'
gem 'pg', '~> 0.21'
end
end
19 changes: 19 additions & 0 deletions gemfiles/activerecord_5.2.0.beta2.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "activerecord", "~> 5.2.0.beta2", require: "active_record"
gem "activesupport", "~> 5.2.0.beta2", require: "active_support/all"
gem "actionpack", "~> 5.2.0.beta2", require: "action_pack"

platforms :jruby do
gem "activerecord-jdbcsqlite3-adapter"
gem "jdbc-sqlite3"
end

platforms :ruby, :mswin, :mingw do
gem "sqlite3"
gem "pg", "~> 0.21"
end

gemspec path: "../"
13 changes: 12 additions & 1 deletion lib/cancan/model_adapters/active_record_4_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,20 @@ def sanitize_sql_activerecord5(conditions)
conditions.stringify_keys!

predicate_builder.build_from_hash(conditions).map do |b|
@model_class.send(:connection).visitor.compile b
visit_nodes(b)
end.join(' AND ')
end

def visit_nodes(b)
# Rails 5.2 adds a BindParam node that prevents the visitor method from properly compiling the SQL query
if ActiveRecord::VERSION::MINOR >= 2
connection = @model_class.send(:connection)
collector = Arel::Collectors::SubstituteBinds.new(connection, Arel::Collectors::SQLString.new)
connection.visitor.accept(b, collector).value
else
@model_class.send(:connection).visitor.compile(b)
end
end
end
end
end