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

Support :if and :unless options for callbacks which pass-thru to Rails #209

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 0.7.1 (Next)

* [#208](https://github.com/mongoid/mongoid-history/pull/208): Fix: history tracks fields declared after `track_history` - [@mikwat](https://github.com/mikwat).
* [#209](https://github.com/mongoid/mongoid-history/pull/209): `track_history` method support for `:if` and `:unless` options - [@johnnyshields](https://github.com/johnnyshields).
* Your contribution here.

### 0.7.0 (2017/11/14)
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,18 @@ Or perhaps you are namespacing to a module:
Mongoid::History.modifier_class_name = 'CMS::Author'
```

**Conditional :if and :unless options**

The `track_history` method supports `:if` and `:unless` options which will skip generating
the history tracker unless they are satisfied. These options can take either a method
`Symbol` or a `Proc`. They behave identical to how `:if` and `:unless` behave in Rails model callbacks.

```ruby
track_history on: [:ip],
if: :should_i_track_history?,
unless: ->(obj){ obj.method_to_skip_history }
```

**Using an alternate changes method**

Sometimes you may wish to provide an alternate method for determining which changes should be tracked. For example, if you are using embedded documents
Expand Down
7 changes: 4 additions & 3 deletions lib/mongoid/history/trackable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ def track_history(options = {})
delegate :history_trackable_options, to: 'self.class'
delegate :track_history?, to: 'self.class'

around_update :track_update if history_options.options[:track_update]
around_create :track_create if history_options.options[:track_create]
around_destroy :track_destroy if history_options.options[:track_destroy]
callback_options = history_options.options.slice(:if, :unless)
around_update :track_update, callback_options if history_options.options[:track_update]
around_create :track_create, callback_options if history_options.options[:track_create]
around_destroy :track_destroy, callback_options if history_options.options[:track_destroy]

Mongoid::History.trackable_class_options ||= {}
Mongoid::History.trackable_class_options[history_options.scope] = history_options
Expand Down
59 changes: 59 additions & 0 deletions spec/unit/callback_options_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'spec_helper'

describe Mongoid::History::Options do
before :all do
ModelOne = Class.new do
include Mongoid::Document
include Mongoid::History::Trackable
store_in collection: :model_ones
field :foo
attr_accessor :bar
track_history track_create: true,
track_update: true,
track_destroy: true,
if: :bar
end

ModelTwo = Class.new do
include Mongoid::Document
include Mongoid::History::Trackable
store_in collection: :model_twos
field :foo
attr_accessor :bar
track_history track_create: true,
track_update: true,
track_destroy: true,
unless: ->(obj){obj.bar}
end

ModelThree = Class.new do
include Mongoid::Document
include Mongoid::History::Trackable
store_in collection: :model_threes
field :foo
attr_accessor :bar, :baz
track_history track_create: true,
track_update: true,
track_destroy: true,
if: ->(obj){obj.bar}, unless: :baz
end
end

describe ':if' do
# TODO: ModelOne
end

describe ':unless' do
# TODO: ModelTwo
end

describe ':if and :unless' do
# TODO: ModelThree
end

after :all do
Object.send(:remove_const, :ModelOne)
Object.send(:remove_const, :ModelTwo)
Object.send(:remove_const, :ModelThree)
end
end