Provide :touch option to save() to accommodate saving without updating t... #18225

Merged
merged 1 commit into from Dec 28, 2014

Conversation

Projects
None yet
3 participants
@DanOlson
Contributor

DanOlson commented Dec 27, 2014

...imestamps. [#18202]

- def save(*)
- create_or_update
+ def save(**args)
+ create_or_update(args)

This comment has been minimized.

@sgrif

sgrif Dec 28, 2014

Member

Let's just use normal splat here.

@sgrif

sgrif Dec 28, 2014

Member

Let's just use normal splat here.

- def save!(*)
- create_or_update || raise(RecordNotSaved.new(nil, self))
+ def save!(**args)
+ create_or_update(args) || raise(RecordNotSaved.new(nil, self))

This comment has been minimized.

@sgrif

sgrif Dec 28, 2014

Member

Let's just use normal splat here.

@sgrif

sgrif Dec 28, 2014

Member

Let's just use normal splat here.

raise ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
- result = new_record? ? _create_record : _update_record
+ result = new_record? ? _create_record : _update_record(args)

This comment has been minimized.

@sgrif

sgrif Dec 28, 2014

Member

Let's just use normal splat here.

@sgrif

sgrif Dec 28, 2014

Member

Let's just use normal splat here.

@@ -58,7 +58,8 @@ def _create_record
end
def _update_record(*args)
- if should_record_timestamps?
+ options = args.extract_options!

This comment has been minimized.

@sgrif

sgrif Dec 28, 2014

Member

This can be written as def _update_record(*args, **options)

@sgrif

sgrif Dec 28, 2014

Member

This can be written as def _update_record(*args, **options)

@@ -70,8 +71,10 @@ def _update_record(*args)
super
end
- def should_record_timestamps?
- self.record_timestamps && (!partial_writes? || changed?)
+ def should_record_timestamps?(**opts)

This comment has been minimized.

@sgrif

sgrif Dec 28, 2014

Member

Let's call this options, since that's what we've called it elsewhere.

@sgrif

sgrif Dec 28, 2014

Member

Let's call this options, since that's what we've called it elsewhere.

This comment has been minimized.

@sgrif

sgrif Dec 28, 2014

Member

Actually why not just use a normal touch: keyword argument?

@sgrif

sgrif Dec 28, 2014

Member

Actually why not just use a normal touch: keyword argument?

@@ -58,7 +58,8 @@ def _create_record
end
def _update_record(*args)
- if should_record_timestamps?
+ options = args.extract_options!
+ if should_record_timestamps?(options)

This comment has been minimized.

@sgrif

sgrif Dec 28, 2014

Member

How about instead of having options get passed to the predicate, we write this as:

def _update_record(touch: true)
  if touch && should_record_timestamps?
@sgrif

sgrif Dec 28, 2014

Member

How about instead of having options get passed to the predicate, we write this as:

def _update_record(touch: true)
  if touch && should_record_timestamps?

This comment has been minimized.

@DanOlson

DanOlson Dec 28, 2014

Contributor

I like this, although the addition of keyword arguments to the method signature will require handling :validate as well. Additionally, we have to account for a possible positional argument, namely an array of model attributes.

The adjusted method signature would read as:

def _update_record(*args, touch: true, validate: true)

WDYT?

@DanOlson

DanOlson Dec 28, 2014

Contributor

I like this, although the addition of keyword arguments to the method signature will require handling :validate as well. Additionally, we have to account for a possible positional argument, namely an array of model attributes.

The adjusted method signature would read as:

def _update_record(*args, touch: true, validate: true)

WDYT?

This comment has been minimized.

@sgrif

sgrif Dec 28, 2014

Member

If we aren't using validate in this method, let's just ignore it.

def _update_record(*args, touch: true, **options)
@sgrif

sgrif Dec 28, 2014

Member

If we aren't using validate in this method, let's just ignore it.

def _update_record(*args, touch: true, **options)

This comment has been minimized.

@DanOlson

DanOlson Dec 28, 2014

Contributor

Thanks a lot, Sean. I think I have everything addressed now.

@DanOlson

DanOlson Dec 28, 2014

Contributor

Thanks a lot, Sean. I think I have everything addressed now.

This comment has been minimized.

@DanOlson

DanOlson Dec 28, 2014

Contributor

Except for tests :P Standby...

@DanOlson

DanOlson Dec 28, 2014

Contributor

Except for tests :P Standby...

@sgrif

This comment has been minimized.

Show comment
Hide comment
@sgrif

sgrif Dec 28, 2014

Member

Looks like the tests are failing.

Member

sgrif commented Dec 28, 2014

Looks like the tests are failing.

@DanOlson

This comment has been minimized.

Show comment
Hide comment
@DanOlson

DanOlson Dec 28, 2014

Contributor

Sorry, all green now. Had to adjust the signatures of the other methods in the inheritance chain.

Contributor

DanOlson commented Dec 28, 2014

Sorry, all green now. Had to adjust the signatures of the other methods in the inheritance chain.

- def _update_record(*)
- partial_writes? ? super(keys_for_partial_write) : super
+ def _update_record(*args, **options)
+ partial_writes? ? super(keys_for_partial_write, options) : super

This comment has been minimized.

@sgrif

sgrif Dec 28, 2014

Member

Can you use the double splat here as well? Not strictly necessary, but will allow for potential performance improvements on the ruby side (skipping a hash allocation)

@sgrif

sgrif Dec 28, 2014

Member

Can you use the double splat here as well? Not strictly necessary, but will allow for potential performance improvements on the ruby side (skipping a hash allocation)

This comment has been minimized.

@sgrif

sgrif Dec 28, 2014

Member

Actually could this be written as:

def _update_record(*args)
  partial_writes? ? super(keys_for_partial_write(*args) : super
@sgrif

sgrif Dec 28, 2014

Member

Actually could this be written as:

def _update_record(*args)
  partial_writes? ? super(keys_for_partial_write(*args) : super
@@ -66,7 +66,7 @@ def increment_lock
send(lock_col + '=', previous_lock_value + 1)
end
- def _update_record(attribute_names = self.attribute_names) #:nodoc:
+ def _update_record(attribute_names = self.attribute_names, **options) #:nodoc:

This comment has been minimized.

@sgrif

sgrif Dec 28, 2014

Member

Does this part of the chain need to care about keyword args? Can we just use normal splat?

@sgrif

sgrif Dec 28, 2014

Member

Does this part of the chain need to care about keyword args? Can we just use normal splat?

This comment has been minimized.

@DanOlson

DanOlson Dec 28, 2014

Contributor

Good point. I found I didn't need any of the keyword handling in most of the chain. Calling super *args in timestamp.rb removes the need. Thanks for the great feedback!

@DanOlson

DanOlson Dec 28, 2014

Contributor

Good point. I found I didn't need any of the keyword handling in most of the chain. Calling super *args in timestamp.rb removes the need. Thanks for the great feedback!

@sgrif

This comment has been minimized.

Show comment
Hide comment
@sgrif

sgrif Dec 28, 2014

Member

Last set of minor comments, then this is good to go

Member

sgrif commented Dec 28, 2014

Last set of minor comments, then this is good to go

sgrif added a commit that referenced this pull request Dec 28, 2014

Merge pull request #18225 from DanOlson/update-without-changing-times…
…tamps

Provide :touch option to save() to accommodate saving without updating t...

@sgrif sgrif merged commit 3ba552f into rails:master Dec 28, 2014

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
@rafaelfranca

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca

rafaelfranca Dec 29, 2014

Member

What is the difference of this and no_touch?

Member

rafaelfranca commented Dec 29, 2014

What is the difference of this and no_touch?

@sgrif

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca

rafaelfranca Dec 29, 2014

Member

hmmmm. At least we can make the implementation of both use the same thing? I dislike the idea of having the same idea implemented in two places.

Member

rafaelfranca commented Dec 29, 2014

hmmmm. At least we can make the implementation of both use the same thing? I dislike the idea of having the same idea implemented in two places.

@rafaelfranca

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca

rafaelfranca Dec 29, 2014

Member

Ah, no_touching is just when calling touch.

Member

rafaelfranca commented Dec 29, 2014

Ah, no_touching is just when calling touch.

@DanOlson

This comment has been minimized.

Show comment
Hide comment
@DanOlson

DanOlson Dec 29, 2014

Contributor

I can certainly look into that.

On Dec 29, 2014, at 10:57 AM, Rafael Mendonça França notifications@github.com wrote:

hmmmm. At least we can make the implementation of both use the same thing? I dislike the idea of having the same idea implemented in two places.


Reply to this email directly or view it on GitHub.

Contributor

DanOlson commented Dec 29, 2014

I can certainly look into that.

On Dec 29, 2014, at 10:57 AM, Rafael Mendonça França notifications@github.com wrote:

hmmmm. At least we can make the implementation of both use the same thing? I dislike the idea of having the same idea implemented in two places.


Reply to this email directly or view it on GitHub.

@sgrif

This comment has been minimized.

Show comment
Hide comment
@sgrif

sgrif Dec 29, 2014

Member

Also even if we did use the same implementation for both, almost all of the code which changed was for the signature, not the actual implementation of skipping the timestamps.

Member

sgrif commented Dec 29, 2014

Also even if we did use the same implementation for both, almost all of the code which changed was for the signature, not the actual implementation of skipping the timestamps.

@DanOlson DanOlson deleted the DanOlson:update-without-changing-timestamps branch Dec 29, 2014

claudiob added a commit to claudiob/rails that referenced this pull request Dec 29, 2014

Add doc for `:touch` option of AR::Base#save
ActiveRecord::Base `save` and `save!` take an option boolean
`:touch` parameter since #18225 (stems from #18202).

This commit document that parameter.

[ci skip]

prathamesh-sonpatki added a commit to prathamesh-sonpatki/rails that referenced this pull request Jun 7, 2016

prathamesh-sonpatki added a commit to prathamesh-sonpatki/rails that referenced this pull request Jun 7, 2016

prathamesh-sonpatki added a commit to prathamesh-sonpatki/rails that referenced this pull request Jun 7, 2016

prathamesh-sonpatki added a commit to prathamesh-sonpatki/rails that referenced this pull request Jun 9, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment