Skip to content

Commit

Permalink
Merge pull request #45 from kevmoo/master
Browse files Browse the repository at this point in the history
Replaced usage of update_attribute with update_attribute!
  • Loading branch information
swanandp committed Jul 27, 2012
2 parents c4aea82 + 7efbbaf commit caa41b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
28 changes: 14 additions & 14 deletions lib/acts_as_list/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ module ClassMethods
# Configuration options are:
#
# * +column+ - specifies the column name to use for keeping the position integer (default: +position+)
# * +scope+ - restricts what is to be considered a list. Given a symbol, it'll attach <tt>_id</tt>
# (if it hasn't already been added) and use that as the foreign key restriction. It's also possible
# * +scope+ - restricts what is to be considered a list. Given a symbol, it'll attach <tt>_id</tt>
# (if it hasn't already been added) and use that as the foreign key restriction. It's also possible
# to give it an entire string that is interpolated if you need a tighter scope than just a foreign key.
# Example: <tt>acts_as_list :scope => 'todo_list_id = #{todo_list_id} AND completed = 0'</tt>
# * +top_of_list+ - defines the integer used for the top of the list. Defaults to 1. Use 0 to make the collection
Expand All @@ -48,7 +48,7 @@ def scope_condition
elsif configuration[:scope].is_a?(Array)
scope_condition_method = %(
def scope_condition
attrs = %w(#{configuration[:scope].join(" ")}).inject({}) do |memo,column|
attrs = %w(#{configuration[:scope].join(" ")}).inject({}) do |memo,column|
memo[column.intern] = send(column.intern); memo
end
self.class.send(:sanitize_sql_hash_for_conditions, attrs)
Expand Down Expand Up @@ -136,20 +136,20 @@ def move_to_top
def remove_from_list
if in_list?
decrement_positions_on_lower_items
update_attribute position_column, nil
update_attributes! position_column => nil
end
end

# Increase the position of this item without adjusting the rest of the list.
def increment_position
return unless in_list?
update_attribute position_column, self.send(position_column).to_i + 1
update_attributes! position_column => self.send(position_column).to_i + 1
end

# Decrease the position of this item without adjusting the rest of the list.
def decrement_position
return unless in_list?
update_attribute position_column, self.send(position_column).to_i - 1
update_attributes! position_column => self.send(position_column).to_i - 1
end

# Return +true+ if this object is the first in the list.
Expand Down Expand Up @@ -184,11 +184,11 @@ def lower_item
def in_list?
!not_in_list?
end

def not_in_list?
send(position_column).nil?
end

def default_position
acts_as_list_class.columns_hash[position_column.to_s].default
end
Expand Down Expand Up @@ -230,12 +230,12 @@ def bottom_item(except = nil)

# Forces item to assume the bottom position in the list.
def assume_bottom_position
update_attribute(position_column, bottom_position_in_list(self).to_i + 1)
update_attributes!(position_column => bottom_position_in_list(self).to_i + 1)
end

# Forces item to assume the top position in the list.
def assume_top_position
update_attribute(position_column, acts_as_list_top)
update_attributes!(position_column => acts_as_list_top)
end

# This has the effect of moving all the higher items up one.
Expand Down Expand Up @@ -307,25 +307,25 @@ def insert_at_position(position)
else
increment_positions_on_lower_items(position)
end
self.update_attribute(position_column, position)
self.update_attributes!(position_column => position)
end

# used by insert_at_position instead of remove_from_list, as postgresql raises error if position_column has non-null constraint
def store_at_0
if in_list?
old_position = send(position_column).to_i
update_attribute(position_column, 0)
update_attributes!(position_column => 0)
decrement_positions_on_lower_items(old_position)
end
end

def update_positions
old_position = send("#{position_column}_was").to_i
new_position = send(position_column).to_i
return unless acts_as_list_class.unscoped.where("#{position_column} = #{new_position}").count > 1
shuffle_positions_on_intermediate_items old_position, new_position, id
end
end
end
end
end
end
16 changes: 8 additions & 8 deletions test/test_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,13 @@ def test_insert_at

def test_update_position
assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
DefaultScopedMixin.find(2).update_attribute(:pos, 4)
DefaultScopedMixin.find(2).update_attributes!(:pos => 4)
assert_equal [1, 3, 4, 2], DefaultScopedMixin.find(:all).map(&:id)
DefaultScopedMixin.find(2).update_attribute(:pos, 2)
DefaultScopedMixin.find(2).update_attributes!(:pos => 2)
assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
DefaultScopedMixin.find(1).update_attribute(:pos, 4)
DefaultScopedMixin.find(1).update_attributes!(:pos => 4)
assert_equal [2, 3, 4, 1], DefaultScopedMixin.find(:all).map(&:id)
DefaultScopedMixin.find(1).update_attribute(:pos, 1)
DefaultScopedMixin.find(1).update_attributes!(:pos => 1)
assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
end

Expand Down Expand Up @@ -337,13 +337,13 @@ def test_insert_at

def test_update_position
assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
DefaultScopedWhereMixin.where(:active => false).find(2).update_attribute(:pos, 4)
DefaultScopedWhereMixin.where(:active => false).find(2).update_attributes!(:pos => 4)
assert_equal [1, 3, 4, 2], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
DefaultScopedWhereMixin.where(:active => false).find(2).update_attribute(:pos, 2)
DefaultScopedWhereMixin.where(:active => false).find(2).update_attributes!(:pos => 2)
assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
DefaultScopedWhereMixin.where(:active => false).find(1).update_attribute(:pos, 4)
DefaultScopedWhereMixin.where(:active => false).find(1).update_attributes!(:pos => 4)
assert_equal [2, 3, 4, 1], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
DefaultScopedWhereMixin.where(:active => false).find(1).update_attribute(:pos, 1)
DefaultScopedWhereMixin.where(:active => false).find(1).update_attributes!(:pos => 1)
assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
end

Expand Down

0 comments on commit caa41b9

Please sign in to comment.