Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor update_all_with_touch #279

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ def self.define_class_methods(caller_class, position_column)
end

define_singleton_method :update_all_with_touch do |updates|
record = new
attrs = record.send(:timestamp_attributes_for_update_in_model)
now = record.send(:current_time_from_proper_timezone)
update_all(updates << touch_record_sql)
end

attrs.each do |attr|
updates << ", #{connection.quote_column_name(attr)} = #{connection.quote(connection.quoted_date(now))}"
end
private

update_all(updates)
define_singleton_method :touch_record_sql do
new.touch_record_sql
end
end
end
Expand All @@ -54,6 +52,23 @@ def self.define_instance_methods(caller_class, position_column)
write_attribute(position_column, position)
@position_changed = true
end

define_method :touch_record_sql do
cached_quoted_now = quoted_current_time_from_proper_timezone

timestamp_attributes_for_update_in_model.map do |attr|
", #{connection.quote_column_name(attr)} = #{cached_quoted_now}"
end.join
end

private

delegate :connection, to: self

def quoted_current_time_from_proper_timezone
connection.quote(connection.quoted_date(
current_time_from_proper_timezone))
end
end
end

Expand Down