Skip to content

Commit

Permalink
Fixed tests to prevent warning: too many arguments for format string (b…
Browse files Browse the repository at this point in the history
  • Loading branch information
brendon authored Nov 26, 2016
1 parent 4abf137 commit 53d0bd4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 32 deletions.
13 changes: 4 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ before_script:
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- 2.2.2
- 2.1.9
- 2.2.6
- 2.3.3
- jruby-19mode
- rbx-2
env:
- DB=sqlite
- DB=mysql
Expand All @@ -26,17 +26,12 @@ gemfile:
- gemfiles/rails_4_2.gemfile
- gemfiles/rails_5_0.gemfile
matrix:
# Travis is failing to install rbx-2 right now, so allow these failures without breaking the build.
allow_failures:
- rvm: rbx-2
exclude:
- rvm: 1.9.3
gemfile: gemfiles/rails_5_0.gemfile
- rvm: 2.0.0
gemfile: gemfiles/rails_5_0.gemfile
- rvm: 2.1.0
- rvm: 2.1.9
gemfile: gemfiles/rails_5_0.gemfile
- rvm: jruby-19mode
gemfile: gemfiles/rails_5_0.gemfile
- rvm: rbx-2
gemfile: gemfiles/rails_5_0.gemfile
8 changes: 4 additions & 4 deletions lib/acts_as_list/active_record/acts/column_method_definer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def self.call(caller_class, column)
attrs = record.send(:timestamp_attributes_for_update_in_model)
now = record.send(:current_time_from_proper_timezone)

query = attrs.map { |attr| "#{connection.quote_column_name(attr)} = :now" }
query.push updates
query = query.join(", ")
attrs.each do |attr|
updates << ", #{connection.quote_column_name(attr)} = #{connection.quote(connection.quoted_date(now))}"
end

update_all([query, now: now])
update_all(updates)
end
end
end
Expand Down
44 changes: 26 additions & 18 deletions lib/acts_as_list/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,23 @@ def assume_top_position
set_list_position(acts_as_list_top)
end

# This has the effect of moving all the higher items down one.
def increment_positions_on_higher_items
return unless in_list?
acts_as_list_list.where("#{quoted_position_column_with_table_name} < ?", send(position_column).to_i).increment_all
end

# This has the effect of moving all the lower items down one.
def increment_positions_on_lower_items(position, avoid_id = nil)
scope = acts_as_list_list

if avoid_id
scope = scope.where("#{quoted_table_name}.#{self.class.primary_key} != ?", self.class.connection.quote(avoid_id))
end

scope.where("#{quoted_position_column_with_table_name} >= ?", position).increment_all
end

# This has the effect of moving all the higher items up one.
def decrement_positions_on_higher_items(position)
acts_as_list_list.where("#{quoted_position_column_with_table_name} <= ?", position).decrement_all
Expand All @@ -292,19 +309,6 @@ def decrement_positions_on_lower_items(position=nil)
acts_as_list_list.where("#{quoted_position_column_with_table_name} > ?", position).decrement_all
end

# This has the effect of moving all the higher items down one.
def increment_positions_on_higher_items
return unless in_list?
acts_as_list_list.where("#{quoted_position_column_with_table_name} < #{send(position_column).to_i}").increment_all
end

# This has the effect of moving all the lower items down one.
def increment_positions_on_lower_items(position, avoid_id = nil)
avoid_id_condition = avoid_id ? " AND #{quoted_table_name}.#{self.class.primary_key} != #{self.class.connection.quote(avoid_id)}" : ''

acts_as_list_list.where("#{quoted_position_column_with_table_name} >= #{position}#{avoid_id_condition}").increment_all
end

# Increments position (<tt>position_column</tt>) of all items in the list.
def increment_positions_on_all_items
acts_as_list_list.increment_all
Expand All @@ -313,27 +317,31 @@ def increment_positions_on_all_items
# Reorders intermediate items to support moving an item from old_position to new_position.
def shuffle_positions_on_intermediate_items(old_position, new_position, avoid_id = nil)
return if old_position == new_position
avoid_id_condition = avoid_id ? " AND #{quoted_table_name}.#{self.class.primary_key} != #{self.class.connection.quote(avoid_id)}" : ''
scope = acts_as_list_list

if avoid_id
scope = scope.where("#{quoted_table_name}.#{self.class.primary_key} != ?", self.class.connection.quote(avoid_id))
end

if old_position < new_position
# Decrement position of intermediate items
#
# e.g., if moving an item from 2 to 5,
# move [3, 4, 5] to [2, 3, 4]
acts_as_list_list.where(
scope.where(
"#{quoted_position_column_with_table_name} > ?", old_position
).where(
"#{quoted_position_column_with_table_name} <= #{new_position}#{avoid_id_condition}"
"#{quoted_position_column_with_table_name} <= ?", new_position
).decrement_all
else
# Increment position of intermediate items
#
# e.g., if moving an item from 5 to 2,
# move [2, 3, 4] to [3, 4, 5]
acts_as_list_list.where(
scope.where(
"#{quoted_position_column_with_table_name} >= ?", new_position
).where(
"#{quoted_position_column_with_table_name} < #{old_position}#{avoid_id_condition}"
"#{quoted_position_column_with_table_name} < ?", old_position
).increment_all
end
end
Expand Down
4 changes: 4 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# $DEBUG = true

require "rubygems"
require "bundler/setup"
begin
Expand All @@ -19,3 +21,5 @@
end

require "shared"

# ActiveRecord::Base.logger = Logger.new(STDOUT)
2 changes: 1 addition & 1 deletion test/test_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def test_insert_race_condition
wait_for_it = false
threads.each(&:join)

assert_equal (1..n).to_a, ListMixin.where(parent_id: 1).order('pos').map(&:pos)
assert_equal((1..n).to_a, ListMixin.where(parent_id: 1).order('pos').map(&:pos))
end
end

Expand Down

0 comments on commit 53d0bd4

Please sign in to comment.