Skip to content

Commit

Permalink
perform update with touch in one db query.
Browse files Browse the repository at this point in the history
  • Loading branch information
botandrose-machine committed Apr 19, 2016
1 parent d5c9418 commit 29ce1b6
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions lib/acts_as_list/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -480,22 +480,19 @@ def quoted_table_name
end

def decrement_all_with_touch(scope)
touch_all scope
scope.update_all "#{quoted_position_column} = (#{quoted_position_column} - 1)"
update_all_with_touch scope, "#{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)"
update_all_with_touch scope, "#{quoted_position_column} = (#{quoted_position_column} + 1)"
end

def touch_all(scope)
def update_all_with_touch(scope, updates)
attrs = timestamp_attributes_for_update_in_model
return if attrs.empty?
query = attrs.inject({}) do |hash, attr|
hash.merge attr => Time.now.utc
end
scope.update_all(query)
query = attrs.map { |attr| "#{attr} = :now" }
query.push updates
query = query.join(", ")
scope.update_all([query, now: Time.now.utc])
end
end
end
Expand Down

0 comments on commit 29ce1b6

Please sign in to comment.