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

Add No Touching #12772

Merged
merged 1 commit into from Nov 13, 2013

Conversation

Projects
None yet
@dmathieu
Contributor

dmathieu commented Nov 5, 2013

This adds #no_touching on all ActiveRecord models, allowing to ignore touching for the duration of a block.

Example:

ActiveRecord::Base.no_touching do
  Post.first.touch #=> Does nothing
end

Comment.no_touching do
  Comment.first.touch #=> Does nothing
  Post.first.touch #=> Updates, but won't update related comments if there are any
end

cc @dhh

end
end
class << self

This comment has been minimized.

@rafaelfranca

rafaelfranca Nov 5, 2013

Member

Why is not this inside the ClassMethods module?

@rafaelfranca

rafaelfranca Nov 5, 2013

Member

Why is not this inside the ClassMethods module?

This comment has been minimized.

@dmathieu

dmathieu Nov 5, 2013

Contributor

If I put them in the ClassMethods module, I can't access them through NoTouching.apply_to, like we do at line 22.
And it means the model gets the apply_to and applied_to? methods, while here, they're only in the module.

@dmathieu

dmathieu Nov 5, 2013

Contributor

If I put them in the ClassMethods module, I can't access them through NoTouching.apply_to, like we do at line 22.
And it means the model gets the apply_to and applied_to? methods, while here, they're only in the module.

This comment has been minimized.

@rafaelfranca

rafaelfranca Nov 5, 2013

Member

Oh, right. Please put # :nodoc: in the methods

@rafaelfranca

rafaelfranca Nov 5, 2013

Member

Oh, right. Please put # :nodoc: in the methods

This comment has been minimized.

@dmathieu

dmathieu Nov 5, 2013

Contributor

Done! 🌞

@dmathieu

dmathieu Nov 5, 2013

Contributor

Done! 🌞

#
# Project.no_touching do
# Project.first.touch # does nothing
# Message.first.touch # works, but does not touch the associated project

This comment has been minimized.

@rafaelfranca

rafaelfranca Nov 5, 2013

Member

missing test for this case

@rafaelfranca

rafaelfranca Nov 5, 2013

Member

missing test for this case

This comment has been minimized.

@dmathieu

dmathieu Nov 5, 2013

Contributor

Hum we're testing this in test_touching_related_objects. Would you see the test written differently?

@dmathieu

dmathieu Nov 5, 2013

Contributor

Hum we're testing this in test_touching_related_objects. Would you see the test written differently?

This comment has been minimized.

@rafaelfranca

rafaelfranca Nov 5, 2013

Member

right

@rafaelfranca

rafaelfranca Nov 5, 2013

Member

right

@egilburg

This comment has been minimized.

Show comment
Hide comment
@egilburg

egilburg Nov 5, 2013

Contributor

I think without_touch or 'without_touching` sounds better.

Contributor

egilburg commented Nov 5, 2013

I think without_touch or 'without_touching` sounds better.

@dhh

This comment has been minimized.

Show comment
Hide comment
@dhh

dhh Nov 5, 2013

Member

I like the fun of no_touching.

On Nov 5, 2013, at 8:33, Eugene Gilburg notifications@github.com wrote:

I think without_touch or 'without_touching` sounds better.


Reply to this email directly or view it on GitHub.

Member

dhh commented Nov 5, 2013

I like the fun of no_touching.

On Nov 5, 2013, at 8:33, Eugene Gilburg notifications@github.com wrote:

I think without_touch or 'without_touching` sounds better.


Reply to this email directly or view it on GitHub.

@sausman

This comment has been minimized.

Show comment
Hide comment
@sausman

sausman Nov 5, 2013

I like the sound of no_touch.

sausman commented Nov 5, 2013

I like the sound of no_touch.

@nijikon

This comment has been minimized.

Show comment
Hide comment
@nijikon

nijikon Nov 5, 2013

+1 for no_touch

nijikon commented Nov 5, 2013

+1 for no_touch

@mbhnyc

This comment has been minimized.

Show comment
Hide comment
@mbhnyc

mbhnyc Nov 6, 2013

Contributor

no_touchy_touchy maximizes fun, while also maximizing awkwardness!

Contributor

mbhnyc commented Nov 6, 2013

no_touchy_touchy maximizes fun, while also maximizing awkwardness!

@dmathieu

This comment has been minimized.

Show comment
Hide comment
@dmathieu

dmathieu Nov 6, 2013

Contributor

From experience the "fun" things in rails don't stick around (I'm thinking of the snowman for example).
So I renamed it to no_touch in my pull request.

Contributor

dmathieu commented Nov 6, 2013

From experience the "fun" things in rails don't stick around (I'm thinking of the snowman for example).
So I renamed it to no_touch in my pull request.

@nijikon

This comment has been minimized.

Show comment
Hide comment
@nijikon

nijikon commented Nov 6, 2013

👍

@dhh

This comment has been minimized.

Show comment
Hide comment
@dhh

dhh Nov 6, 2013

Member

Eh, what? Look up #forty_two. Do rename it back.

On Nov 6, 2013, at 16:59, Damien Mathieu notifications@github.com wrote:

From experience the "fun" things in rails don't stick around (I'm thinking of the snowman for example).
So I renamed it to no_touch in my pull request.


Reply to this email directly or view it on GitHub.

Member

dhh commented Nov 6, 2013

Eh, what? Look up #forty_two. Do rename it back.

On Nov 6, 2013, at 16:59, Damien Mathieu notifications@github.com wrote:

From experience the "fun" things in rails don't stick around (I'm thinking of the snowman for example).
So I renamed it to no_touch in my pull request.


Reply to this email directly or view it on GitHub.

@rafaelfranca

View changes

Show outdated Hide outdated activerecord/lib/active_record/no_touching.rb Outdated
@thedarkone

View changes

Show outdated Hide outdated activerecord/lib/active_record/no_touching.rb Outdated
@dhh

View changes

Show outdated Hide outdated activerecord/CHANGELOG.md Outdated
@dhh

View changes

Show outdated Hide outdated activerecord/lib/active_record/no_touching.rb Outdated
@dhh

This comment has been minimized.

Show comment
Hide comment
@dhh

dhh Nov 12, 2013

Member

Besides these few comments I just added, are we otherwise ready to merge?

Member

dhh commented Nov 12, 2013

Besides these few comments I just added, are we otherwise ready to merge?

@dmathieu

This comment has been minimized.

Show comment
Hide comment
@dmathieu

dmathieu Nov 13, 2013

Contributor

I just applied your two comments. :shipit:

Contributor

dmathieu commented Nov 13, 2013

I just applied your two comments. :shipit:

rafaelfranca added a commit that referenced this pull request Nov 13, 2013

@rafaelfranca rafaelfranca merged commit 3d2e8cb into rails:master Nov 13, 2013

1 check failed

default The Travis CI build failed
Details
@sikachu

This comment has been minimized.

Show comment
Hide comment
@sikachu
Member

sikachu commented on b32ba36 Nov 13, 2013

This comment has been minimized.

Show comment
Hide comment
@dmathieu

dmathieu Nov 13, 2013

Contributor

💚

Contributor

dmathieu replied Nov 13, 2013

💚

This comment has been minimized.

Show comment
Hide comment
@tomdale

tomdale replied Nov 13, 2013

http://gifrific.com/wp-content/uploads/2013/02/No-Touching-George-Bluth-Arrested-Development.gif

This comment has been minimized.

Show comment
Hide comment
@fphilipe

fphilipe Nov 14, 2013

Contributor

@dmathieu Could you give an example of a use case for this? I guess it could come in handy, but I can't come up with an example from the top of my head. 😊

Contributor

fphilipe replied Nov 14, 2013

@dmathieu Could you give an example of a use case for this? I guess it could come in handy, but I can't come up with an example from the top of my head. 😊

This comment has been minimized.

Show comment
Hide comment
@dmathieu

dmathieu Nov 14, 2013

Contributor

@fphilipe: this was a feature request coming from @dhh. I don't have a personal use case for it, I just did the implementation.
He says, as a use case:

Useful when copying object graphs while preserving the original timestamps, for example.

Contributor

dmathieu replied Nov 14, 2013

@fphilipe: this was a feature request coming from @dhh. I don't have a personal use case for it, I just did the implementation.
He says, as a use case:

Useful when copying object graphs while preserving the original timestamps, for example.

This comment has been minimized.

Show comment
Hide comment
@fphilipe

fphilipe Nov 14, 2013

Contributor

@dmathieu OK, thanks!

Contributor

fphilipe replied Nov 14, 2013

@dmathieu OK, thanks!

This comment has been minimized.

Show comment
Hide comment
@dhh

dhh Nov 14, 2013

Member

Here are two use cases from Basecamp:

def create_taggings_from_mass_assignment
  if @tag_names
    # Normally, tagging a taggable touches the taggable to expire
    # its caches. We can skip the touches here since we're in an
    # after_create callback and we know there are no caches to
    # expire. (Skipping these touches also lets us preserve the
    # original timestamps when copying messages and uploads with
    # tagged attachments.)

    ActiveRecord::Base.no_touching do
      set_tag_names(@tag_names)
    end

    @tag_names = nil
  end
end
class Copier
  def initialize(source, account)
    @source, @account = source, account
  end

  def copy
    ActiveRecord::Base.no_touching do
      @creator = find_person || create_person
      @project = create_project

      copy_todolists
      copy_messages
      copy_documents

      @project.reset_counters
      @project
    end
  rescue
    @project.try(:destroy)
    raise
  ensure
    @creator.try(:trash)
  end
Member

dhh replied Nov 14, 2013

Here are two use cases from Basecamp:

def create_taggings_from_mass_assignment
  if @tag_names
    # Normally, tagging a taggable touches the taggable to expire
    # its caches. We can skip the touches here since we're in an
    # after_create callback and we know there are no caches to
    # expire. (Skipping these touches also lets us preserve the
    # original timestamps when copying messages and uploads with
    # tagged attachments.)

    ActiveRecord::Base.no_touching do
      set_tag_names(@tag_names)
    end

    @tag_names = nil
  end
end
class Copier
  def initialize(source, account)
    @source, @account = source, account
  end

  def copy
    ActiveRecord::Base.no_touching do
      @creator = find_person || create_person
      @project = create_project

      copy_todolists
      copy_messages
      copy_documents

      @project.reset_counters
      @project
    end
  rescue
    @project.try(:destroy)
    raise
  ensure
    @creator.try(:trash)
  end

@dmathieu dmathieu deleted the dmathieu:no_touching branch Nov 13, 2013

@lukaszx0

This comment has been minimized.

Show comment
Hide comment
@lukaszx0

lukaszx0 Nov 21, 2013

Member

I liked @steveklabnik's idea of calling it #cant_touch_this the most 🤘

Member

lukaszx0 commented Nov 21, 2013

I liked @steveklabnik's idea of calling it #cant_touch_this the most 🤘

@guilleiguaran

This comment has been minimized.

Show comment
Hide comment
@guilleiguaran

guilleiguaran Nov 21, 2013

Member

LOL :trollface:

Sent from my iPhone

On 20 Nov 2013, at 09:02 pm, Łukasz Strzałkowski notifications@github.com wrote:

I liked @steveklabnik's idea of calling it #cant_touch_this the most


Reply to this email directly or view it on GitHub.

Member

guilleiguaran commented Nov 21, 2013

LOL :trollface:

Sent from my iPhone

On 20 Nov 2013, at 09:02 pm, Łukasz Strzałkowski notifications@github.com wrote:

I liked @steveklabnik's idea of calling it #cant_touch_this the most


Reply to this email directly or view it on GitHub.

@arenoir

This comment has been minimized.

Show comment
Hide comment
@arenoir

arenoir Apr 18, 2014

@dmathieu this is very handy, however it seems after_touch callbacks still get run. Is there a way to disable after_touch callbacks?

arenoir commented Apr 18, 2014

@dmathieu this is very handy, however it seems after_touch callbacks still get run. Is there a way to disable after_touch callbacks?

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