Skip to content

Commit

Permalink
Merge pull request #223 from /issues/222-activerecord-5.0.0
Browse files Browse the repository at this point in the history
Work with Activerecord 5.0.0
  • Loading branch information
zdennis committed Jan 23, 2016
2 parents d1f6967 + 290b6c9 commit c0a4393
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: ruby
cache: bundler
rvm:
- 2.0.0
- 2.2.3

gemfile:
- Gemfile
Expand Down
5 changes: 5 additions & 0 deletions gemfiles/5.0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
platforms :ruby do
gem 'mysql', '~> 2.9'
gem 'activerecord', '~> 5.0.0.beta1'
gem 'em-synchrony', '1.0.4'
end
24 changes: 16 additions & 8 deletions lib/activerecord-import/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ def import_with_validations( column_names, array_of_attributes, options={} )
instance = new do |model|
hsh.each_pair{ |k,v| model.send("#{k}=", v) }
end

if not instance.valid?(options[:validate_with_context])
array_of_attributes[ i ] = nil
failed_instances << instance
Expand Down Expand Up @@ -428,9 +429,14 @@ def import_without_validations_or_callbacks( column_names, array_of_attributes,
def set_ids_and_mark_clean(models, import_result)
unless models.nil?
import_result.ids.each_with_index do |id, index|
models[index].id = id.to_i
models[index].instance_variable_get(:@changed_attributes).clear # mark the model as saved
models[index].instance_variable_set(:@new_record, false)
model = models[index]
model.id = id.to_i
if model.respond_to?(:clear_changes_information) # Rails 4.0 and higher
model.clear_changes_information
else # Rails 3.1
model.instance_variable_get(:@changed_attributes).clear
end
model.instance_variable_set(:@new_record, false)
end
end
end
Expand Down Expand Up @@ -485,10 +491,12 @@ def values_sql_for_columns_and_attributes(columns, array_of_attributes) # :nod
if val.nil? && column.name == primary_key && !sequence_name.blank?
connection_memo.next_value_for_sequence(sequence_name)
elsif column
if column.respond_to?(:type_cast_from_user) # Rails 4.2 and higher
if respond_to?(:type_caster) && type_caster.respond_to?(:type_cast_for_database) # Rails 5.0 and higher
connection_memo.quote(type_caster.type_cast_for_database(column.name, val))
elsif column.respond_to?(:type_cast_from_user) # Rails 4.2 and higher
connection_memo.quote(column.type_cast_from_user(val), column)
else
connection_memo.quote(column.type_cast(val), column) # Rails 3.1, 3.2, and 4.1
else # Rails 3.1, 3.2, and 4.1
connection_memo.quote(column.type_cast(val), column)
end
end
end
Expand All @@ -499,7 +507,7 @@ def values_sql_for_columns_and_attributes(columns, array_of_attributes) # :nod
def add_special_rails_stamps( column_names, array_of_attributes, options )
AREXT_RAILS_COLUMNS[:create].each_pair do |key, blk|
if self.column_names.include?(key)
value = connection.quote blk.call
value = blk.call
if index=column_names.index(key) || index=column_names.index(key.to_sym)
# replace every instance of the array of attributes with our value
array_of_attributes.each{ |arr| arr[index] = value if arr[index].nil? }
Expand All @@ -512,7 +520,7 @@ def add_special_rails_stamps( column_names, array_of_attributes, options )

AREXT_RAILS_COLUMNS[:update].each_pair do |key, blk|
if self.column_names.include?(key)
value = connection.quote blk.call
value = blk.call
if index=column_names.index(key) || index=column_names.index(key.to_sym)
# replace every instance of the array of attributes with our value
array_of_attributes.each{ |arr| arr[index] = value }
Expand Down
4 changes: 2 additions & 2 deletions lib/activerecord-import/synchronize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def self.synchronize(instances, keys=[self.primary_key])
end

if matched_instance
instance.clear_aggregation_cache
instance.clear_association_cache
instance.send :clear_aggregation_cache
instance.send :clear_association_cache
instance.instance_variable_set :@attributes, matched_instance.instance_variable_get(:@attributes)

if instance.respond_to?(:clear_changes_information)
Expand Down
20 changes: 16 additions & 4 deletions test/import_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,14 @@
]
Book.import books
assert_equal 2, Book.count
assert_equal 0, Book.first.read_attribute('status')
assert_equal 1, Book.last.read_attribute('status')

if ENV['AR_VERSION'].to_i >= 5.0
assert_equal 'draft', Book.first.read_attribute('status')
assert_equal 'published', Book.last.read_attribute('status')
else
assert_equal 0, Book.first.read_attribute('status')
assert_equal 1, Book.last.read_attribute('status')
end
end

if ENV['AR_VERSION'].to_i > 4.1
Expand All @@ -407,8 +413,14 @@
]
Book.import books
assert_equal 2, Book.count
assert_equal 0, Book.first.read_attribute('status')
assert_equal 1, Book.last.read_attribute('status')

if ENV['AR_VERSION'].to_i >= 5.0
assert_equal 'draft', Book.first.read_attribute('status')
assert_equal 'published', Book.last.read_attribute('status')
else
assert_equal 0, Book.first.read_attribute('status')
assert_equal 1, Book.last.read_attribute('status')
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/travis/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function run {
$@
}

for activerecord_version in "3.1" "3.2" "4.0" "4.1" "4.2" ; do
for activerecord_version in "3.1" "3.2" "4.0" "4.1" "4.2" "5.0" ; do
export AR_VERSION=$activerecord_version

bundle update activerecord
Expand Down

0 comments on commit c0a4393

Please sign in to comment.