Skip to content

Commit

Permalink
Fixes SQL error from rails 5.2 upgrade CanCanCommunity#478
Browse files Browse the repository at this point in the history
  • Loading branch information
lizzyaustad committed Jan 29, 2018
1 parent e2bfddf commit 048c516
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
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

0 comments on commit 048c516

Please sign in to comment.