Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add #no_touching on ActiveRecord models
- Loading branch information
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module ActiveRecord | ||
# = Active Record No Touching | ||
module NoTouching | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
# Lets you selectively disable calls to `touch` for the | ||
# duration of a block. | ||
# | ||
# ==== Examples | ||
# ActiveRecord::Base.no_touching do | ||
# Project.first.touch # does nothing | ||
# Message.first.touch # does nothing | ||
# end | ||
# | ||
# Project.no_touching do | ||
# Project.first.touch # does nothing | ||
# Message.first.touch # works, but does not touch the associated project | ||
# end | ||
# | ||
def no_touching(&block) | ||
NoTouching.apply_to(self, &block) | ||
end | ||
end | ||
|
||
class << self | ||
def apply_to(klass) #:nodoc: | ||
klasses.push(klass) | ||
yield | ||
ensure | ||
klasses.pop | ||
end | ||
|
||
def applied_to?(klass) #:nodoc: | ||
klasses.any? { |k| k >= klass } | ||
end | ||
|
||
private | ||
def klasses | ||
Thread.current[:no_touching_classes] ||= [] | ||
end | ||
end | ||
|
||
def no_touching? | ||
NoTouching.applied_to?(self.class) | ||
end | ||
|
||
def touch(*) | ||
super unless no_touching? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b32ba36
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b32ba36
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💚
b32ba36
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b32ba36
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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. 😊
b32ba36
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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:
b32ba36
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dmathieu OK, thanks!
b32ba36
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are two use cases from Basecamp: