Skip to content

Commit

Permalink
Fix deprecation notices (#44)
Browse files Browse the repository at this point in the history
Using new Rails5 codepath to generate SQL
  • Loading branch information
dv authored and nettofarah committed Nov 18, 2016
1 parent 433ab9e commit 8fba306
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/polo/sql_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def insert_values(record)
# 'type_cast' call are necessary.
#
module ActiveRecordFour

def insert_values(record)
connection = ActiveRecord::Base.connection
values = record.send(:arel_attributes_with_values_for_create, record.attribute_names)
Expand All @@ -88,13 +87,18 @@ def insert_values(record)
# We now use the type_caster from the arel_table.
#
module ActiveRecordFive
def insert_values(record)
type_caster = record.class.arel_table.send(:type_caster)
# Based on the codepath used in Rails 5
def raw_sql(record)
values = record.send(:arel_attributes_with_values_for_create, record.attribute_names)
values.each do |attribute, value|
values[attribute] = type_caster.type_cast_for_database(attribute.name, value)
model = record.class
substitutes, binds = model.unscoped.substitute_values(values)

insert_manager = model.arel_table.create_insert
insert_manager.insert substitutes

model.connection.unprepared_statement do
model.connection.to_sql(insert_manager, binds)
end
values
end
end

Expand All @@ -103,7 +107,7 @@ def insert_values(record)
elsif ActiveRecord::VERSION::MAJOR == 4
include ActiveRecordFour
else
include ActiveRecordFive
prepend ActiveRecordFive
end
end
end

0 comments on commit 8fba306

Please sign in to comment.