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

Change modifier to updater #141

Open
SuperMasterBlasterLaser opened this issue Aug 1, 2015 · 6 comments
Open

Change modifier to updater #141

SuperMasterBlasterLaser opened this issue Aug 1, 2015 · 6 comments

Comments

@SuperMasterBlasterLaser
Copy link

I have class called Category:

     class Category
          include Mongoid::Document
          include Mongoid::Timestamps
          include Mongoid::History::Trackable

          field :name, type: String

          track_history   :on => [:name],       # track title and body fields only, default is :all
              :modifier_field => :updater, # adds "belongs_to :modifier" to track who made the change, default is :modifier
              :modifier_field_inverse_of => :nil, # adds an ":inverse_of" option to the "belongs_to :modifier" relation, default is not set
              :version_field => :version,   # adds "field :version, :type => Integer" to track current version, default is :version
              :track_create   =>  true,    # track document creation, default is false
              :track_update   =>  true,     # track document updates, default is true
              :track_destroy  =>  true     # track document destruction, default is false
      end

I have set modifier_field to :updater.

In my history tracker I have written this:

  class HistoryTracker
       include Mongoid::History::Tracker
       include Mongoid::Userstamp
  end

In my config/initializers/mongoid-history.rb I have written this:
Mongoid::History.tracker_class_name = :history_tracker
Mongoid::History.modifier_class_name = 'Person'

  Mongoid::Userstamp.config do |c|
       c.user_reader = :current_person
       c.created_name = :created_by
       c.updated_name = :updated_by
  end

When I create category: Category.create(name: "Test" updater: Person.all.first) and then I try to access to my HistoryTracker via console.

When I try to get updater like this

   HistoryTracker.all.first.updater

It gives me an error: NoMethodError: undefined methodupdater' for #HistoryTracker:0x00000008ceb050`

But I when I write this:
HistoryTracker.all.first.modifier

It returns me model.

Why modifier can not chenge to updater?

@dblock
Copy link
Collaborator

dblock commented Aug 1, 2015

Looking at the code, the modifier relationship name is hard-coded and doesn't change when you use a different field. See https:/aq1018/mongoid-history/blob/c2e91e1f2e4e078295e55e04a893298284c71cab/lib/mongoid/history/tracker.rb#L17. At a first glance this looks weird to me too, but seems a bit big to have been around all this time without anyone noticing.

Want to write some tests and try to change the relationship too? We can discuss it over code.

@SuperMasterBlasterLaser
Copy link
Author

I have tried to trick it and added into my HistoryTracker this code:

 class HistoryTracker
     include Mongoid::History::Tracker
     include Mongoid::Userstamp

   def updater
      self.modifier
   end
 end

It does not worked. When I write HistoryTracker.all.first.updater it throws me an error NoMethodError: undefined methodupdater' for #HistoryTracker:0x00000008e09cc0`

About tests... I'm just beginner in Ruby language.

@dblock
Copy link
Collaborator

dblock commented Aug 1, 2015

It doesn't work how?

@SuperMasterBlasterLaser
Copy link
Author

I have updated my post above.

What I understood, is that on class where I add include Mongoid::History::Trackable and track_history if I set :modifier_field => :updater it allows me only to change to Model.create(name: "name", modifier: Person.all.first) to Model.create(name: "name", updater: Person.all.first).

However in tracker it still will be modifier

@SuperMasterBlasterLaser
Copy link
Author

@dblock I have found out why my custom def updater didn't work.

I was using mongoid-audit gem. He had his OWN HistoryTracker which overlapped mine. That is why I forked his gem and added def updater to its tracker and added it to my gemfile.

@dblock
Copy link
Collaborator

dblock commented Aug 2, 2015

In general in Ruby you want to keep the original gems and monkey-patch them in your code (you can declare the class again and add a def updater anywhere in your code). So whatever you added to their fork, just add it as a monkey patch in your application (in Rails that goes to config/initializers/... typically).

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

No branches or pull requests

2 participants