Skip to content

Commit f890598

Browse files
committed
AREL values passed to #to_sql not handled correctly on AR-3.0 (fixes #365)
1 parent eefd707 commit f890598

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

lib/arjdbc/jdbc/adapter.rb

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -383,30 +383,41 @@ def primary_keys(table)
383383
@connection.primary_keys(table)
384384
end
385385

386-
if ActiveRecord::VERSION::MAJOR >= 3
387-
388-
# Converts an arel AST to SQL
389-
def to_sql(arel, binds = [])
390-
if arel.respond_to?(:ast)
391-
visitor.accept(arel.ast) do
392-
quote(*binds.shift.reverse)
393-
end
394-
else # for backwards compatibility :
386+
if ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 0
387+
388+
#attr_reader :visitor unless method_defined?(:visitor) # not in 3.0
389+
390+
# Converts an AREL AST to SQL.
391+
def to_sql(arel, binds = [])
392+
# NOTE: can not handle `visitor.accept(arel.ast)` right thus
393+
# convert AREL to a SQL string and simply substitute binds :
395394
sql = arel.respond_to?(:to_sql) ? arel.send(:to_sql) : arel
396395
return sql if binds.blank?
397396
sql.gsub('?') { quote(*binds.shift.reverse) }
398397
end
399-
end
398+
399+
elsif ActiveRecord::VERSION::MAJOR >= 3 # AR >= 3.1 or 4.0
400+
401+
# Converts an AREL AST to SQL.
402+
def to_sql(arel, binds = [])
403+
if arel.respond_to?(:ast)
404+
visitor.accept(arel.ast) { quote(*binds.shift.reverse) }
405+
else # for backwards compatibility :
406+
sql = arel.respond_to?(:to_sql) ? arel.send(:to_sql) : arel
407+
return sql if binds.blank?
408+
sql.gsub('?') { quote(*binds.shift.reverse) }
409+
end
410+
end
400411

401412
else # AR-2.3 no #to_sql method
402413

403-
# Substitutes SQL bind (?) parameters
404-
def to_sql(sql, binds = [])
405-
sql = sql.send(:to_sql) if sql.respond_to?(:to_sql)
406-
return sql if binds.blank?
407-
copy = binds.dup
408-
sql.gsub('?') { quote(*copy.shift.reverse) }
409-
end
414+
# Substitutes SQL bind parameters.
415+
def to_sql(sql, binds = [])
416+
sql = sql.send(:to_sql) if sql.respond_to?(:to_sql)
417+
return sql if binds.blank?
418+
copy = binds.dup
419+
sql.gsub('?') { quote(*copy.shift.reverse) }
420+
end
410421

411422
end
412423

test/has_many_through.rb

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ def teardown
1111
end
1212

1313
def test_has_many_through
14-
admin_role = Role.create :name => "Administrator",
15-
:description => "System defined super user - access to right and role management."
16-
admin_role.save!
14+
admin_role = Role.create! :name => "Administrator",
15+
:description => "super user - access to right and role management"
1716

1817
assert_equal(0, admin_role.rights.sum(:hours))
1918

20-
role_rights = Right.create :name => "Administrator - Full Access To Roles",
21-
:actions => "*", :controller_name => "Admin::RolesController", :hours => 0
22-
right_rights = Right.create :name => "Administrator - Full Access To Rights",
23-
:actions => "*", :controller_name => "Admin::RightsController", :hours => 1.5
19+
role_rights = Right.create :name => "Administrator - Full Access To Roles",
20+
:actions => "*", :controller_name => "Admin::RolesController", :hours => 0
21+
right_rights = Right.create :name => "Administrator - Full Access To Rights",
22+
:actions => "*", :controller_name => "Admin::RightsController", :hours => 1.5
2423

2524
admin_role.rights << role_rights
2625
admin_role.rights << right_rights
@@ -40,4 +39,20 @@ def test_has_many_through
4039
assert ! rights_only_role.has_right?(role_rights)
4140
end
4241

42+
def test_has_many_select_rows_with_relation
43+
role = Role.create! :name => "main", :description => "main role"
44+
Role.create! :name => "user", :description => "user role"
45+
46+
Right.create! :name => "r0", :hours => 0
47+
r1 = Right.create! :name => "r1", :hours => 1
48+
r2 = Right.create! :name => "r2", :hours => 2
49+
Right.create! :name => "r3", :hours => 3
50+
51+
role.permission_groups.create! :right => r1.reload
52+
role.permission_groups.create! :right => r2.reload
53+
54+
groups = role.reload.permission_groups.select('right_id')
55+
assert_equal [ [ r1.id ], [ r2.id ] ], role.connection.select_rows(groups)
56+
end if Test::Unit::TestCase.ar_version('3.0')
57+
4358
end

0 commit comments

Comments
 (0)