Comments (11)
Firstly, this seems to be a bug of mongoid istead of mongoid-history, as sweeper is a Mongoid::Observer
, and the #before
method is not being called correctly from mongoid itself.
As a best practice, I recommend you to set the modifier manually. This is because saving the controller in the model violates MVC concept of separation of concerns.
from mongoid-history.
Re MVC concerns: currently the modifier attribute is set using Sweeper before_create
automatically and not manually. When you say to set the modifier manually, do you mean setting the modifier_id
of the model it in the controller before the create call? If so this feels a little hackish as the modifier / creator of a model should be captured when tracking a create as per the update workflow when using this gem.
I agree with the callback ordering, from my reading the around_filter
should be calling the before
callback previous to the specific before_create
callback. I'll see what the mongoid people say about it and get back to you with any information if it happens.
cheers.
from mongoid-history.
You can manually set the modifier like this:
user = User.create(:name => "Aaron")
post = Post.create(:title => "Test", :body => "Post", :modifier => user)
:modifier
is just another field in mongodb, and is nothing special. Please refer to the README usage examples for more details.
Let me know if you find out anything new with mongoid folks. (Maybe you can temporarily hack your controller to add a around_filter
to manually call #before
and #after
methods? For me, I don't see these methods documented in mongoid, and guessing they are probably not intended to be public API.
Also, I believe the controller assignment is not thread safe, probably need to use thread local variables instead...
from mongoid-history.
Never mind, #around_filter
is added here:
https://github.com/aq1018/mongoid-history/blob/master/lib/mongoid/history/tracker.rb#L19
I didn't get the full picture of this feature since I didn't implement it. :P
from mongoid-history.
I find difficult it to reproduce your issue. However, I did find 2 bugs:
- Mongoid::History::Sweeper#controller was not thread safe, and now it is.
- Mongoid::History::Sweeper#before_create was calling trackable#modifer directly, instead of looking at
history_trackable_options[:modifer_field]
@camallen, did you have a custom :modifier_field
? If so, this is probably why it didn't work. The second fix addresses this issue.
from mongoid-history.
Hi,
I didn't have a custom :modifier_field setup. The problem i experienced was the Mongoid::History::Sweeper#before_create
didn't have the controller instance setup.
I've moved to a custom history tracker but it's good to hear you've got the thread safety working as this was a key problem for me.
from mongoid-history.
If you are still interested, could you answer the following questions:
- Which version of rails were you using?
- Where were you saving your mongoid records that supposed to trigger
Mongoid::History::Sweeper#before_create
? Was it inside a controller action, or before / around filters? - Put a print / debug statement in Mongoid::History::Sweeper#before, and see if it is invoked.
- Put a print / debug statement in Mongoid::History::Sweeper#controller=, and see if it is invoked.
from mongoid-history.
In response:
- Rails 3.1.3
- My understanding was that by calling
include Mongoid::History::Trackable
in my model to be tracked this would setup the before callbacks. - I have removed all my testing code on this as i've rolled my own leveraging the mongoid versioning functionality.
As stated above, I traced execution in the debugger and realised my controller instance wasn't setup in the sweeper on create. For updates, the controller instance was setup and hence the tracker was working as expected but not on the create action.
from mongoid-history.
I solved this problem by loading history tracker model manually and I think in production there is no problem. In development Rails does not load all models/classes when server starts therefore filter class is not registered to the appropriate controller, that is why mongoid_history cannot access to the current_user method so that modifier field does not get set. This is my blog post to solve this issue: http://elvinefendi.heroku.com/blog/2012/06/23/a-tip-regarding-to-mongoid-history-modifier-field/
from mongoid-history.
I had similar problem. I only needed to set :modifier_field => :modifier on history tracker.
from mongoid-history.
Closing because:
- Rails specific
- Work around available.
- Observer will be dropped in future releases.
from mongoid-history.
Related Issues (20)
- Unable to Contribute HOT 1
- undefined method `[]' for #<..::EmbedsMany> HOT 7
- Compatibility with Mongoid 7 HOT 4
- Specs create classes and are order-dependent
- Fix undo and redo modifier on embedded create/destroy
- Fix inheritance of history_trackable_options HOT 4
- Changing collection breaks history tracking HOT 5
- Release v. Next HOT 1
- `HistoryTracker` documents not created if `track_create` set to `false` HOT 2
- undefined method `collection_name' for nil:NilClass Did you mean? collectionize error coming HOT 6
- History tracker recording undo action as the last change.. HOT 1
- wrong number of arguments HOT 14
- Adding custom attributes before save HOT 1
- A tracker on an embeds_many relation is being limited by the parent configuration HOT 1
- Embeds many tracking fails if original value is `nil`
- How can we track changes done with set method? HOT 1
- It should not warn about `Overwriting existing field version` when `track_history` is called more than once
- Is it possible to specify a max number of history_tracks to maintain for each document? HOT 1
- How to get what the collection looked like on a version? HOT 3
- Change from non-existent to false not being tracked HOT 19
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoid-history.