Skip to content

Commit

Permalink
extract private #increment_all_with_touch and #decrement_all_with_tou…
Browse files Browse the repository at this point in the history
…ch methods.
  • Loading branch information
botandrose-machine committed Apr 19, 2016
1 parent 99bc150 commit d5c9418
Showing 1 changed file with 23 additions and 29 deletions.
52 changes: 23 additions & 29 deletions lib/acts_as_list/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,50 +344,36 @@ def assume_top_position

# 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} <= #{position}"
).tap { |scope| touch_all scope }.update_all(
"#{quoted_position_column} = (#{quoted_position_column} - 1)"
)
scope = acts_as_list_list.where("#{quoted_position_column} <= #{position}")
decrement_all_with_touch scope
end

# This has the effect of moving all the lower items up one.
def decrement_positions_on_lower_items(position=nil)
return unless in_list?
position ||= send(position_column).to_i
acts_as_list_list.where(
"#{quoted_position_column} > #{position}"
).tap { |scope| touch_all scope }.update_all(
"#{quoted_position_column} = (#{quoted_position_column} - 1)"
)
scope = acts_as_list_list.where("#{quoted_position_column} > #{position}")
decrement_all_with_touch scope
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} < #{send(position_column).to_i}"
).tap { |scope| touch_all scope }.update_all(
"#{quoted_position_column} = (#{quoted_position_column} + 1)"
)
scope = acts_as_list_list.where("#{quoted_position_column} < #{send(position_column).to_i}")
increment_all_with_touch scope
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 #{self.class.primary_key} != #{self.class.connection.quote(avoid_id)}" : ''

acts_as_list_list.where(
"#{quoted_position_column} >= #{position}#{avoid_id_condition}"
).tap { |scope| touch_all scope }.update_all(
"#{quoted_position_column} = (#{quoted_position_column} + 1)"
)
scope = acts_as_list_list.where("#{quoted_position_column} >= #{position}#{avoid_id_condition}")
increment_all_with_touch scope
end

# Increments position (<tt>position_column</tt>) of all items in the list.
def increment_positions_on_all_items
acts_as_list_list.tap { |scope| touch_all scope }.update_all(
"#{quoted_position_column} = (#{quoted_position_column} + 1)"
)
increment_all_with_touch acts_as_list_list
end

# Reorders intermediate items to support moving an item from old_position to new_position.
Expand All @@ -400,25 +386,23 @@ def shuffle_positions_on_intermediate_items(old_position, new_position, avoid_id
#
# e.g., if moving an item from 2 to 5,
# move [3, 4, 5] to [2, 3, 4]
acts_as_list_list.where(
scope = acts_as_list_list.where(
"#{quoted_position_column} > #{old_position}"
).where(
"#{quoted_position_column} <= #{new_position}#{avoid_id_condition}"
).tap { |scope| touch_all scope }.update_all(
"#{quoted_position_column} = (#{quoted_position_column} - 1)"
)
decrement_all_with_touch scope
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 = acts_as_list_list.where(
"#{quoted_position_column} >= #{new_position}"
).where(
"#{quoted_position_column} < #{old_position}#{avoid_id_condition}"
).tap { |scope| touch_all scope }.update_all(
"#{quoted_position_column} = (#{quoted_position_column} + 1)"
)
increment_all_with_touch scope
end
end

Expand Down Expand Up @@ -495,6 +479,16 @@ def quoted_table_name
@_quoted_table_name ||= acts_as_list_class.quoted_table_name
end

def decrement_all_with_touch(scope)
touch_all scope
scope.update_all "#{quoted_position_column} = (#{quoted_position_column} - 1)"
end

def increment_all_with_touch(scope)
touch_all scope
scope.update_all "#{quoted_position_column} = (#{quoted_position_column} + 1)"
end

def touch_all(scope)
attrs = timestamp_attributes_for_update_in_model
return if attrs.empty?
Expand Down

0 comments on commit d5c9418

Please sign in to comment.