Mr.D
Mr.D

Reputation: 7893

Mongoid, setting custom accessor field in mongoid-history gem

I'm adding mongoid-history gem to my project.

According to guide in github, when I add Userstamp to my tracker it creates created_by field with accessor called creator.

They have written that I can rename it via gem config.

How to rename this field?

Upvotes: 2

Views: 344

Answers (1)

nayiaw
nayiaw

Reputation: 1456

Based on the documentation, Userstamp is another gem called mongoid_userstamp. The documentation provided the sample code to configure the names via config file or inside each model:

config/mongoid_userstamp.rb:

# Default config (optional unless you want to customize the values)
Mongoid::Userstamp.config do |c|
  c.user_reader = :current_user
  c.created_name = :created_by
  c.updated_name = :updated_by
end

app/models/your_model.rb

# Example model class
class Product
  include Mongoid::Document
  include Mongoid::Userstamp

  # optional class-level config override
  # mongoid_userstamp user_model: 'MyUser',
  #                   created_name: :creator,
  #                   updated_name: :updater,
end

# Example user class
class MyUser
  include Mongoid::Document
  include Mongoid::Userstamp::User

  # optional class-level config override
  # mongoid_userstamp_user reader: :current_my_user
end

In Mongoid History, you can include the configuration inside the HistoryTracker class, for example:

# app/models/history_tracker.rb
class HistoryTracker
  include Mongoid::History::Tracker
  include Mongoid::Userstamp
  # optional class-level config override
  mongoid_userstamp created_name: :updater,
                    updated_name: :another_updater,
end

Upvotes: 1

Related Questions