Skip to content

Commit

Permalink
Fix bootstrapping without ids not working on update for certain datab…
Browse files Browse the repository at this point in the history
…ase adapters
  • Loading branch information
Aaron Pfeifer committed May 3, 2009
1 parent 0e1b64a commit d2c597f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rdoc
@@ -1,5 +1,7 @@
== master

* Fix bootstrapping without ids not working on update for certain database adapters

== 0.4.1 / 2009-05-01

* Improve #fast_bootstrap speed by 50% by using the connection directly
Expand Down
8 changes: 6 additions & 2 deletions lib/enumerate_by.rb
Expand Up @@ -253,7 +253,10 @@ def bootstrap(*records)
uncached do
# Remove records that are no longer being used
records.flatten!
delete_all(['id NOT IN (?)', records.map {|record| record[:id]}])
ids = records.map {|record| record[:id]}.compact
delete_all(ids.any? ? ['id NOT IN (?)', ids] : nil)

# Find remaining existing records (to be updated)
existing = all.inject({}) {|existing, record| existing[record.id] = record; existing}

records.map! do |attributes|
Expand Down Expand Up @@ -302,7 +305,8 @@ def bootstrap(*records)
def fast_bootstrap(*records)
# Remove records that are no longer being used
records.flatten!
delete_all(['id NOT IN (?)', records.map {|record| record[:id]}])
ids = records.map {|record| record[:id]}.compact
delete_all(ids.any? ? ['id NOT IN (?)', ids] : nil)

# Find remaining existing records (to be updated)
quoted_table_name = self.quoted_table_name
Expand Down
38 changes: 38 additions & 0 deletions test/unit/enumerate_by_test.rb
Expand Up @@ -295,6 +295,25 @@ def test_should_synchronize_all_attributes
end
end

class EnumerationBootstrappedWithExistingDynamicRecordsTest < ActiveRecord::TestCase
def setup
create_color(:name => 'RED')
create_color(:name => 'GREEN')

@red, @green = Color.bootstrap(
{:name => 'red'},
{:name => 'green'}
)
end

def test_should_synchronize_all_attributes
assert_equal 2, Color.count

assert_equal 'red', @red.name
assert_equal 'green', @green.name
end
end

class EnumerationBootstrappedWithDefaultsTest < ActiveRecord::TestCase
def setup
@red = create_color(:name => 'RED', :html => '#f00')
Expand Down Expand Up @@ -375,6 +394,25 @@ def test_should_synchronize_all_attributes
end
end

class EnumerationFastBootstrappedWithExistingDynamicRecordsTest < ActiveRecord::TestCase
def setup
create_color(:name => 'RED')
create_color(:name => 'GREEN')

Color.fast_bootstrap(
{:name => 'red'},
{:name => 'green'}
)
end

def test_should_synchronize_all_attributes
assert_equal 2, Color.count

assert Color.exists?(:name => 'red')
assert Color.exists?(:name => 'green')
end
end

class EnumerationFastBootstrappedWithDefaultsTest < ActiveRecord::TestCase
def setup
@red = create_color(:name => 'RED', :html => '#f00')
Expand Down

0 comments on commit d2c597f

Please sign in to comment.