Skip to content

Commit c0a4393

Browse files
committed
Merge pull request #223 from /issues/222-activerecord-5.0.0
Work with Activerecord 5.0.0
2 parents d1f6967 + 290b6c9 commit c0a4393

File tree

6 files changed

+41
-16
lines changed

6 files changed

+41
-16
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: ruby
22
cache: bundler
33
rvm:
4-
- 2.0.0
4+
- 2.2.3
55

66
gemfile:
77
- Gemfile

gemfiles/5.0.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
platforms :ruby do
2+
gem 'mysql', '~> 2.9'
3+
gem 'activerecord', '~> 5.0.0.beta1'
4+
gem 'em-synchrony', '1.0.4'
5+
end

lib/activerecord-import/import.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ def import_with_validations( column_names, array_of_attributes, options={} )
355355
instance = new do |model|
356356
hsh.each_pair{ |k,v| model.send("#{k}=", v) }
357357
end
358+
358359
if not instance.valid?(options[:validate_with_context])
359360
array_of_attributes[ i ] = nil
360361
failed_instances << instance
@@ -428,9 +429,14 @@ def import_without_validations_or_callbacks( column_names, array_of_attributes,
428429
def set_ids_and_mark_clean(models, import_result)
429430
unless models.nil?
430431
import_result.ids.each_with_index do |id, index|
431-
models[index].id = id.to_i
432-
models[index].instance_variable_get(:@changed_attributes).clear # mark the model as saved
433-
models[index].instance_variable_set(:@new_record, false)
432+
model = models[index]
433+
model.id = id.to_i
434+
if model.respond_to?(:clear_changes_information) # Rails 4.0 and higher
435+
model.clear_changes_information
436+
else # Rails 3.1
437+
model.instance_variable_get(:@changed_attributes).clear
438+
end
439+
model.instance_variable_set(:@new_record, false)
434440
end
435441
end
436442
end
@@ -485,10 +491,12 @@ def values_sql_for_columns_and_attributes(columns, array_of_attributes) # :nod
485491
if val.nil? && column.name == primary_key && !sequence_name.blank?
486492
connection_memo.next_value_for_sequence(sequence_name)
487493
elsif column
488-
if column.respond_to?(:type_cast_from_user) # Rails 4.2 and higher
494+
if respond_to?(:type_caster) && type_caster.respond_to?(:type_cast_for_database) # Rails 5.0 and higher
495+
connection_memo.quote(type_caster.type_cast_for_database(column.name, val))
496+
elsif column.respond_to?(:type_cast_from_user) # Rails 4.2 and higher
489497
connection_memo.quote(column.type_cast_from_user(val), column)
490-
else
491-
connection_memo.quote(column.type_cast(val), column) # Rails 3.1, 3.2, and 4.1
498+
else # Rails 3.1, 3.2, and 4.1
499+
connection_memo.quote(column.type_cast(val), column)
492500
end
493501
end
494502
end
@@ -499,7 +507,7 @@ def values_sql_for_columns_and_attributes(columns, array_of_attributes) # :nod
499507
def add_special_rails_stamps( column_names, array_of_attributes, options )
500508
AREXT_RAILS_COLUMNS[:create].each_pair do |key, blk|
501509
if self.column_names.include?(key)
502-
value = connection.quote blk.call
510+
value = blk.call
503511
if index=column_names.index(key) || index=column_names.index(key.to_sym)
504512
# replace every instance of the array of attributes with our value
505513
array_of_attributes.each{ |arr| arr[index] = value if arr[index].nil? }
@@ -512,7 +520,7 @@ def add_special_rails_stamps( column_names, array_of_attributes, options )
512520

513521
AREXT_RAILS_COLUMNS[:update].each_pair do |key, blk|
514522
if self.column_names.include?(key)
515-
value = connection.quote blk.call
523+
value = blk.call
516524
if index=column_names.index(key) || index=column_names.index(key.to_sym)
517525
# replace every instance of the array of attributes with our value
518526
array_of_attributes.each{ |arr| arr[index] = value }

lib/activerecord-import/synchronize.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def self.synchronize(instances, keys=[self.primary_key])
4040
end
4141

4242
if matched_instance
43-
instance.clear_aggregation_cache
44-
instance.clear_association_cache
43+
instance.send :clear_aggregation_cache
44+
instance.send :clear_association_cache
4545
instance.instance_variable_set :@attributes, matched_instance.instance_variable_get(:@attributes)
4646

4747
if instance.respond_to?(:clear_changes_information)

test/import_test.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,14 @@
394394
]
395395
Book.import books
396396
assert_equal 2, Book.count
397-
assert_equal 0, Book.first.read_attribute('status')
398-
assert_equal 1, Book.last.read_attribute('status')
397+
398+
if ENV['AR_VERSION'].to_i >= 5.0
399+
assert_equal 'draft', Book.first.read_attribute('status')
400+
assert_equal 'published', Book.last.read_attribute('status')
401+
else
402+
assert_equal 0, Book.first.read_attribute('status')
403+
assert_equal 1, Book.last.read_attribute('status')
404+
end
399405
end
400406

401407
if ENV['AR_VERSION'].to_i > 4.1
@@ -407,8 +413,14 @@
407413
]
408414
Book.import books
409415
assert_equal 2, Book.count
410-
assert_equal 0, Book.first.read_attribute('status')
411-
assert_equal 1, Book.last.read_attribute('status')
416+
417+
if ENV['AR_VERSION'].to_i >= 5.0
418+
assert_equal 'draft', Book.first.read_attribute('status')
419+
assert_equal 'published', Book.last.read_attribute('status')
420+
else
421+
assert_equal 0, Book.first.read_attribute('status')
422+
assert_equal 1, Book.last.read_attribute('status')
423+
end
412424
end
413425
end
414426
end

test/travis/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function run {
77
$@
88
}
99

10-
for activerecord_version in "3.1" "3.2" "4.0" "4.1" "4.2" ; do
10+
for activerecord_version in "3.1" "3.2" "4.0" "4.1" "4.2" "5.0" ; do
1111
export AR_VERSION=$activerecord_version
1212

1313
bundle update activerecord

0 commit comments

Comments
 (0)