Skip to content

Commit

Permalink
KIll some whitespaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
spectator committed Jun 14, 2013
1 parent a8e8b62 commit 1cdd79f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
12 changes: 6 additions & 6 deletions lib/activerecord-import/synchronize.rb
@@ -1,10 +1,10 @@
module ActiveRecord # :nodoc:
class Base # :nodoc:

# Synchronizes the passed in ActiveRecord instances with data
# from the database. This is like calling reload on an individual
# ActiveRecord instance but it is intended for use on multiple instances.
#
#
# This uses one query for all instance updates and then updates existing
# instances rather sending one query for each instance
#
Expand All @@ -14,7 +14,7 @@ class Base # :nodoc:
# <.. out of system changes occur to change author name from Zach to Zachary..>
# Post.synchronize posts
# posts.first.author # => "Zachary" instead of Zach
#
#
# # Synchronizing using custom key fields
# posts = Post.find_by_author("Zach")
# <.. out of system changes occur to change the address of author 'Zach' to 1245 Foo Ln ..>
Expand All @@ -26,19 +26,19 @@ def self.synchronize(instances, keys=[self.primary_key])

conditions = {}
order = ""

key_values = keys.map { |key| instances.map(&"#{key}".to_sym) }
keys.zip(key_values).each { |key, values| conditions[key] = values }
order = keys.map{ |key| "#{key} ASC" }.join(",")

klass = instances.first.class

fresh_instances = klass.find( :all, :conditions=>conditions, :order=>order )
instances.each do |instance|
matched_instance = fresh_instances.detect do |fresh_instance|
keys.all?{ |key| fresh_instance.send(key) == instance.send(key) }
end

if matched_instance
instance.clear_aggregation_cache
instance.clear_association_cache
Expand Down
66 changes: 33 additions & 33 deletions test/import_test.rb
Expand Up @@ -6,7 +6,7 @@
assert_difference "Topic.count", +10 do
result = Topic.import Build(3, :topics)
assert result.num_inserts > 0

result = Topic.import Build(7, :topics)
assert result.num_inserts > 0
end
Expand All @@ -33,14 +33,14 @@
let(:columns) { %w(title author_name) }
let(:valid_values) { [[ "LDAP", "Jerry Carter"], ["Rails Recipes", "Chad Fowler"]] }
let(:invalid_values) { [[ "The RSpec Book", ""], ["Agile+UX", ""]] }

context "with validation checks turned off" do
it "should import valid data" do
assert_difference "Topic.count", +2 do
result = Topic.import columns, valid_values, :validate => false
end
end

it "should import invalid data" do
assert_difference "Topic.count", +2 do
result = Topic.import columns, invalid_values, :validate => false
Expand All @@ -53,26 +53,26 @@
end
end
end

context "with validation checks turned on" do
it "should import valid data" do
assert_difference "Topic.count", +2 do
result = Topic.import columns, valid_values, :validate => true
end
end

it "should not import invalid data" do
assert_no_difference "Topic.count" do
result = Topic.import columns, invalid_values, :validate => true
end
end

it "should report the failed instances" do
results = Topic.import columns, invalid_values, :validate => true
assert_equal invalid_values.size, results.failed_instances.size
results.failed_instances.each{ |e| assert_kind_of Topic, e }
end

it "should import valid data when mixed with invalid data" do
assert_difference "Topic.count", +2 do
result = Topic.import columns, valid_values + invalid_values, :validate => true
Expand Down Expand Up @@ -123,20 +123,20 @@
context "with :synchronize option" do
context "synchronizing on new records" do
let(:new_topics) { Build(3, :topics) }

it "doesn't reload any data (doesn't work)" do
Topic.import new_topics, :synchronize => new_topics
assert new_topics.all?(&:new_record?), "No record should have been reloaded"
end
end

context "synchronizing on new records with explicit conditions" do
let(:new_topics) { Build(3, :topics) }

it "reloads data for existing in-memory instances" do
Topic.import(new_topics, :synchronize => new_topics, :synchronize_keys => [:title] )
assert new_topics.all?(&:persisted?), "Records should have been reloaded"
end
end
end

context "synchronizing on destroyed records with explicit conditions" do
Expand All @@ -146,24 +146,24 @@
new_topics.each &:destroy
Topic.import(new_topics, :synchronize => new_topics, :synchronize_keys => [:title] )
assert new_topics.all?(&:persisted?), "Records should have been reloaded"
end
end
end
end

context "with an array of unsaved model instances" do
let(:topic) { Build(:topic, :title => "The RSpec Book", :author_name => "David Chelimsky")}
let(:topics) { Build(9, :topics) }
let(:invalid_topics){ Build(7, :invalid_topics)}

it "should import records based on those model's attributes" do
assert_difference "Topic.count", +9 do
result = Topic.import topics
end

Topic.import [topic]
assert Topic.find_by_title_and_author_name("The RSpec Book", "David Chelimsky")
end

it "should not overwrite existing records" do
topic = Generate(:topic, :title => "foobar")
assert_no_difference "Topic.count" do
Expand All @@ -179,21 +179,21 @@
end
assert_equal "foobar", topic.reload.title
end

context "with validation checks turned on" do
it "should import valid models" do
assert_difference "Topic.count", +9 do
result = Topic.import topics, :validate => true
end
end

it "should not import invalid models" do
assert_no_difference "Topic.count" do
result = Topic.import invalid_topics, :validate => true
end
end
end

context "with validation checks turned off" do
it "should import invalid models" do
assert_difference "Topic.count", +7 do
Expand All @@ -202,20 +202,20 @@
end
end
end

context "with an array of columns and an array of unsaved model instances" do
let(:topics) { Build(2, :topics) }

it "should import records populating the supplied columns with the corresponding model instance attributes" do
assert_difference "Topic.count", +2 do
result = Topic.import [:author_name, :title], topics
end

# imported topics should be findable by their imported attributes
assert Topic.find_by_author_name(topics.first.author_name)
assert Topic.find_by_author_name(topics.last.author_name)
end

it "should not populate fields for columns not imported" do
topics.first.author_email_address = "zach.dennis@gmail.com"
assert_difference "Topic.count", +2 do
Expand All @@ -225,14 +225,14 @@
assert !Topic.find_by_author_email_address("zach.dennis@gmail.com")
end
end

context "with an array of columns and an array of values" do
it "should import ids when specified" do
Topic.import [:id, :author_name, :title], [[99, "Bob Jones", "Topic 99"]]
assert_equal 99, Topic.last.id
end
end

context "ActiveRecord timestamps" do
context "when the timestamps columns are present" do
setup do
Expand All @@ -243,24 +243,24 @@
end
@book = Book.last
end

it "should set the created_at column for new records" do
assert_equal 5.minutes.ago.strftime("%H:%M"), @book.created_at.strftime("%H:%M")
end

it "should set the created_on column for new records" do
assert_equal 5.minutes.ago.strftime("%H:%M"), @book.created_on.strftime("%H:%M")
end

it "should set the updated_at column for new records" do
assert_equal 5.minutes.ago.strftime("%H:%M"), @book.updated_at.strftime("%H:%M")
end

it "should set the updated_on column for new records" do
assert_equal 5.minutes.ago.strftime("%H:%M"), @book.updated_on.strftime("%H:%M")
end
end

context "when a custom time zone is set" do
setup do
original_timezone = ActiveRecord::Base.default_timezone
Expand All @@ -273,22 +273,22 @@
ActiveRecord::Base.default_timezone = original_timezone
@book = Book.last
end

it "should set the created_at and created_on timestamps for new records" do
assert_equal 5.minutes.ago.utc.strftime("%H:%M"), @book.created_at.strftime("%H:%M")
assert_equal 5.minutes.ago.utc.strftime("%H:%M"), @book.created_on.strftime("%H:%M")
end

it "should set the updated_at and updated_on timestamps for new records" do
assert_equal 5.minutes.ago.utc.strftime("%H:%M"), @book.updated_at.strftime("%H:%M")
assert_equal 5.minutes.ago.utc.strftime("%H:%M"), @book.updated_on.strftime("%H:%M")
end
end
end

context "importing with database reserved words" do
let(:group) { Build(:group, :order => "superx") }

it "should import just fine" do
assert_difference "Group.count", +1 do
result = Group.import [group]
Expand Down
2 changes: 1 addition & 1 deletion test/schema/mysql_schema.rb
@@ -1,5 +1,5 @@
ActiveRecord::Schema.define do

create_table :books, :options=>'ENGINE=MyISAM', :force=>true do |t|
t.column :title, :string, :null=>false
t.column :publisher, :string, :null=>false, :default => 'Default Publisher'
Expand Down

0 comments on commit 1cdd79f

Please sign in to comment.