mongoid-history
mongoid-history copied to clipboard
Change modifier to updater
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 method
updater' for #HistoryTracker:0x00000008ceb050`
But I when I write this: HistoryTracker.all.first.modifier
It returns me model.
Why modifier can not chenge to updater?
Looking at the code, the modifier
relationship name is hard-coded and doesn't change when you use a different field. See https://github.com/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.
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 method
updater' for #HistoryTracker:0x00000008e09cc0`
About tests... I'm just beginner in Ruby language.
It doesn't work how?
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
@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.
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).