Skip to content

Commit

Permalink
work in progress: some appraisals fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zharikovpro committed Dec 23, 2016
1 parent fa4c678 commit 1f5aedc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
24 changes: 7 additions & 17 deletions lib/acts_as_list/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ def shuffle_positions_on_intermediate_items(old_position, new_position, avoid_id
# unique constraint prevents regular increment_all and forces to do increments one by one
# http://stackoverflow.com/questions/7703196/sqlite-increment-unique-integer-field
# both SQLite and PostgreSQL (and most probably MySQL too) has same issue
update_one_by_one = acts_as_list_list.connection.index_exists?(acts_as_list_list.table_name, position_column, unique: true)

# we cannot reliably detect unique and other constraints across all environments (Rails 3, sqlite, etc)
# thus will sequentially update every item one by one

if old_position < new_position
# Decrement position of intermediate items
Expand All @@ -337,14 +339,8 @@ def shuffle_positions_on_intermediate_items(old_position, new_position, avoid_id
"#{quoted_position_column_with_table_name} > ?", old_position
).where(
"#{quoted_position_column_with_table_name} <= ?", new_position
)

if update_one_by_one
items.order("#{quoted_position_column_with_table_name} ASC").ids.each do |id|
acts_as_list_list.find(id).decrement!(position_column)
end
else
items.decrement_all
).order("#{quoted_position_column_with_table_name} ASC").pluck(:id).each do |id|
acts_as_list_list.find(id).decrement!(position_column)
end
else
# Increment position of intermediate items
Expand All @@ -355,14 +351,8 @@ def shuffle_positions_on_intermediate_items(old_position, new_position, avoid_id
"#{quoted_position_column_with_table_name} >= ?", new_position
).where(
"#{quoted_position_column_with_table_name} < ?", old_position
)

if update_one_by_one
items.order("#{quoted_position_column_with_table_name} DESC").ids.each do |id|
acts_as_list_list.find(id).increment!(position_column)
end
else
items.increment_all
).order("#{quoted_position_column_with_table_name} DESC").pluck(:id).each do |id|
acts_as_list_list.find(id).increment!(position_column)
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions test/test_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ def setup_db(position_options = {})
t.column :updated_at, :datetime
t.column :state, :integer

t.index :pos, unique: true if position_options[:unique] && !(sqlite && position_options[:positive])
if position_options[:unique] && !(sqlite && position_options[:positive])
if t.respond_to?(:index)
t.index :pos, unique: true
end
end
end

if position_options[:positive]
Expand Down Expand Up @@ -809,18 +813,14 @@ def setup
def test_insert_at
new = DefaultScopedMixin.create
assert_equal 5, new.pos
assert_equal [1, 2, 3, 4, 5], DefaultScopedMixin.all.order(id: :asc).pluck(:pos)

new.insert_at(1)
assert_equal 1, new.pos
assert_equal [1, 2, 3, 4, 5], DefaultScopedMixin.all.order(id: :asc).pluck(:pos)

new.insert_at(5)
assert_equal 5, new.pos
assert_equal [1, 2, 3, 4, 5], DefaultScopedMixin.all.order(id: :asc).pluck(:pos)

new.insert_at(3)
assert_equal 3, new.pos
assert_equal [1, 2, 3, 4, 5], DefaultScopedMixin.all.order(id: :asc).pluck(:pos)
end
end

0 comments on commit 1f5aedc

Please sign in to comment.