Skip to content

Commit

Permalink
Add support to serialized fields with AR 5 (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
nettofarah committed Nov 18, 2016
1 parent adb672a commit 433ab9e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
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

0 comments on commit 433ab9e

Please sign in to comment.