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

Add support to serialized fields with AR 5 #43

Merged
merged 1 commit into from
Nov 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ end
appraise "rails42" do
gem "activerecord", "4.2.3"
end

appraise "rails50" do
gem "activerecord", "5.0.0"
end
7 changes: 7 additions & 0 deletions gemfiles/rails50.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "activerecord", "5.0.0"

gemspec :path => "../"
24 changes: 22 additions & 2 deletions lib/polo/sql_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def insert_values(record)
# wouldn't be properly serialized automatically. That's why explict
# 'type_cast' call are necessary.
#
module ActiveRecordFourOrGreater
module ActiveRecordFour

def insert_values(record)
connection = ActiveRecord::Base.connection
values = record.send(:arel_attributes_with_values_for_create, record.attribute_names)
Expand All @@ -80,10 +81,29 @@ def insert_values(record)
end
end

# Internal: Returns an object's attribute definitions along with
# their set values (for Rails >= 5.x).
#
# Serializers have changed again in rails 5.
# 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)
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)
end
values
end
end

if ActiveRecord::VERSION::MAJOR < 4
include ActiveRecordLessThanFour
elsif ActiveRecord::VERSION::MAJOR == 4
include ActiveRecordFour
else
include ActiveRecordFourOrGreater
include ActiveRecordFive
end
end
end
2 changes: 1 addition & 1 deletion spec/polo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
end

it 'generates insert queries for dependencies' do
if ActiveRecord::VERSION::STRING.start_with?('4.2')
if ActiveRecord::VERSION::STRING >= "4.2"
serialized_nil = "NULL"
else
serialized_nil = "'null'"
Expand Down