Giter VIP home page Giter VIP logo

Comments (11)

aq1018 avatar aq1018 commented on September 8, 2024

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.

camallen avatar camallen commented on September 8, 2024

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.

aq1018 avatar aq1018 commented on September 8, 2024

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.

aq1018 avatar aq1018 commented on September 8, 2024

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.

aq1018 avatar aq1018 commented on September 8, 2024

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.

camallen avatar camallen commented on September 8, 2024

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.

aq1018 avatar aq1018 commented on September 8, 2024

@camallen,

If you are still interested, could you answer the following questions:

  1. Which version of rails were you using?
  2. 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?
  3. Put a print / debug statement in Mongoid::History::Sweeper#before, and see if it is invoked.
  4. Put a print / debug statement in Mongoid::History::Sweeper#controller=, and see if it is invoked.

from mongoid-history.

camallen avatar camallen commented on September 8, 2024

In response:

  1. Rails 3.1.3
  2. My understanding was that by calling include Mongoid::History::Trackable in my model to be tracked this would setup the before callbacks.
  3. 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.

ElvinEfendi avatar ElvinEfendi commented on September 8, 2024

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.

DeathBorn avatar DeathBorn commented on September 8, 2024

I had similar problem. I only needed to set :modifier_field => :modifier on history tracker.

from mongoid-history.

aq1018 avatar aq1018 commented on September 8, 2024

Closing because:

  1. Rails specific
  2. Work around available.
  3. Observer will be dropped in future releases.

from mongoid-history.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.