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

Improve disable and disable_tracking, add their inverses #240

Merged
merged 4 commits into from
Apr 14, 2020

Conversation

cgriego
Copy link
Contributor

@cgriego cgriego commented Apr 14, 2020

Mongoid::History.disable and disable_tracking now restore the
original state if given a block and can be run without a block plus
Mongoid::History.enable and enable_tracking have been added.

I was originally going to use these to globally disable tracking in our test suite and enable it selectively for individual tests, but we have request_store in our app and therefore the value got reset by individual requests. I ended up just stubbing Mongoid::History.enabled? instead.

`Mongoid::History.disable` and `disable_tracking` now restore the
original state if given a block and can be run without a block plus
`Mongoid::History.enable` and `enable_tracking` have been added.
@coveralls
Copy link

coveralls commented Apr 14, 2020

Coverage Status

Coverage increased (+0.01%) to 99.417% when pulling a2998df on getaroom:enable-tracking into 4c0f1fd on mongoid:master.

Copy link
Collaborator

@dblock dblock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff. See below for the interface changes, happy to discuss if you feel strongly otherwise.

Needs to be documented in README please.

def disable(&_block)
store[GLOBAL_TRACK_HISTORY_FLAG] = false
yield
def disable(&block)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that disable! and enable! that doesn't take a block along with enable and disable that does would be more idiomatic. In Ruby we want the developers to write "english", not pass in true/false. Then you don't need the track(boolean) method at all.

with_tracking(true, &block)
end

def with_tracking(state)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the class methods, these should be called with_tracking and without_tracking. I would alias disable_tracking to without_tracking and never expose a method that has a boolean parameter. It's OK to duplicate code a little bit too IMO for something so trivial.

@cgriego
Copy link
Contributor Author

cgriego commented Apr 14, 2020

I only added track/with_tracking as a way to reduce duplication, which is why I didn't test them directly or document them. I thought about prefixing them with an underscore.

I did think about having bang methods because that did seem nicer to me, but then there's duplication again and really the current non-bang block-or-no-block interface is very similar to Ruby's File.open in stdlib.

What do you think about just removing track/with_tracking?

@dblock
Copy link
Collaborator

dblock commented Apr 14, 2020

What do you think about just removing track/with_tracking?

I'm open to it. Try it let's see what it looks like?

Copy link
Collaborator

@dblock dblock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for me, let's make the API more ruby-ish?

CHANGELOG.md Outdated
@@ -4,6 +4,7 @@
* [#236](https://github.com/mongoid/mongoid-history/pull/236): Fix Ruby 2.7 keyword argument warnings - [@vasilysn](https://github.com/vasilysn).
* [#237](https://github.com/mongoid/mongoid-history/pull/237): Fix tracking subclasses with additional fields - [@getaroom](https://github.com/getaroom).
* [#239](https://github.com/mongoid/mongoid-history/pull/239): Optimize `modified_attributes_for_create` 6-7x - [@getaroom](https://github.com/getaroom).
* [#240](https://github.com/mongoid/mongoid-history/pull/240): `Mongoid::History.disable` and `disable_tracking` now restore the original state if given a block and can be run without a block plus `Mongoid::History.enable` and `enable_tracking` have been added - [@getaroom](https://github.com/getaroom).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split in two lines with the same PR. There are two changes here.

README.md Outdated
@@ -149,11 +149,34 @@ Comment.disable_tracking do
comment.update_attributes(:title => "Test 3")
end

# globally disable all history tracking
# disable tracking for comments by default
Comment.disable_tracking
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's alias disable_tracking to disable_tracking! and document the latter as the recommended way to do this. So either

Comment.disable_tracking!

or

Comment.disable_tracking do
 ...
end

README.md Outdated
Mongoid::History.disable do
comment.update_attributes(:title => "Test 3")
user.update_attributes(:name => "Eddie Van Halen")
end

# globally disable all history tracking by default
Mongoid::History.disable
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the above, disable! and enable!

@dblock dblock merged commit 6616cea into mongoid:master Apr 14, 2020
@dblock
Copy link
Collaborator

dblock commented Apr 14, 2020

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants