Skip to content

Commit

Permalink
Merge remote-tracking branch 'chewi/scope-awareness'
Browse files Browse the repository at this point in the history
  • Loading branch information
zdennis committed Dec 14, 2012
2 parents 394e0b5 + 2247314 commit a4f17e6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/activerecord-import/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ def import_with_validations( column_names, array_of_attributes, options={} )
# information on +column_names+, +array_of_attributes_ and
# +options+.
def import_without_validations_or_callbacks( column_names, array_of_attributes, options={} )
scope_columns, scope_values = scope_attributes.to_a.transpose

unless scope_columns.blank?
column_names.concat scope_columns
array_of_attributes.each { |a| a.concat scope_values }
end

columns = column_names.each_with_index.map do |name, i|
column = columns_hash[name.to_s]

Expand Down
16 changes: 15 additions & 1 deletion test/import_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,18 @@
assert_equal "2010/05/14".to_date, Topic.last.last_read.to_date
end
end
end

context "importing through an association scope" do
[ true, false ].each do |b|
context "when validation is " + (b ? "enabled" : "disabled") do
it "should automatically set the foreign key column" do
books = [[ "David Chelimsky", "The RSpec Book" ], [ "Chad Fowler", "Rails Recipes" ]]
topic = Factory.create :topic
topic.books.import [ :author_name, :title ], books, :validate => b
assert_equal 2, topic.books.count
assert topic.books.all? { |b| b.topic_id == topic.id }
end
end
end
end
end
5 changes: 5 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,8 @@
require adapter_schema if File.exists?(adapter_schema)

Dir[File.dirname(__FILE__) + "/models/*.rb"].each{ |file| require file }

# Prevent this deprecation warning from breaking the tests.
module Rake::DeprecatedObjectDSL
remove_method :import
end

0 comments on commit a4f17e6

Please sign in to comment.