module Mongoid::History::Trackable::SingletonMethods

Public Instance Methods

clear_trackable_memoization() click to toggle source
# File lib/mongoid/history/trackable.rb, line 572
def clear_trackable_memoization
  @reserved_tracked_fields = nil
  @history_trackable_options = nil
  @trackable_settings = nil
  @tracked_fields = nil
  @tracked_embeds_one = nil
  @tracked_embeds_many = nil
end
dynamic_field?(field) click to toggle source

Checks if field is dynamic.

@param [ String | Symbol ] field The name of the dynamic field

@return [ Boolean ] whether or not the field is dynamic

# File lib/mongoid/history/trackable.rb, line 462
def dynamic_field?(field)
  dynamic_enabled? &&
    !fields.keys.include?(database_field_name(field)) &&
    !embedded_relations.map { |_, v| v.key }.include?(database_field_name(field))
end
field_format(field) click to toggle source
# File lib/mongoid/history/trackable.rb, line 468
def field_format(field)
  field_formats[database_field_name(field)]
end
field_formats() click to toggle source
# File lib/mongoid/history/trackable.rb, line 503
def field_formats
  @field_formats ||= history_trackable_options[:format]
end
history_trackable_options() click to toggle source
# File lib/mongoid/history/trackable.rb, line 568
def history_trackable_options
  @history_trackable_options ||= mongoid_history_options.prepared
end
inherited(subclass) click to toggle source
Calls superclass method
# File lib/mongoid/history/trackable.rb, line 581
def inherited(subclass)
  super
  subclass.mongoid_history_options = Mongoid::History::Options.new(subclass, mongoid_history_options.options)
end
reserved_tracked_fields() click to toggle source

Retrieves the memoized list of reserved tracked fields, which are only included for certain actions.

@return [ Array < String > ] the list of reserved database field names

# File lib/mongoid/history/trackable.rb, line 494
def reserved_tracked_fields
  @reserved_tracked_fields ||= begin
                                 fields = ['_id', history_trackable_options[:version_field].to_s]
                                 modifier_field = history_trackable_options[:modifier_field]
                                 fields << "#{modifier_field}_id" if modifier_field
                                 fields
                               end
end
trackable_scope() click to toggle source
# File lib/mongoid/history/trackable.rb, line 564
def trackable_scope
  collection_name.to_s.singularize.to_sym
end
tracked?(field_or_relation, action = :update) click to toggle source

Whether or not the field or embedded relation should be tracked.

@param [ String | Symbol ] field_or_relation The name or alias of the field OR the name of embedded relation @param [ String | Symbol ] action The optional action name (:create, :update, or :destroy)

@return [ Boolean ] whether or not the field or embedded relation is tracked for the given action

# File lib/mongoid/history/trackable.rb, line 443
def tracked?(field_or_relation, action = :update)
  tracked_field?(field_or_relation, action) || tracked_relation?(field_or_relation)
end
tracked_embeds_many() click to toggle source

Retrieves the memoized list of tracked embeds_many relations

@return [ Array < String > ] the list of tracked embeds_many relations

# File lib/mongoid/history/trackable.rb, line 552
def tracked_embeds_many
  @tracked_embeds_many ||= begin
    reflect_on_all_associations(:embeds_many)
      .map(&:key)
      .select { |rel| history_trackable_options[:relations][:embeds_many].include? rel }
  end
end
tracked_embeds_many?(relation) click to toggle source

Whether or not the embeds_many relation should be tracked.

@param [ String | Symbol ] relation The name of the embeds_many relation

@return [ Boolean ] whether or not the embeds_many relation is tracked

# File lib/mongoid/history/trackable.rb, line 545
def tracked_embeds_many?(relation)
  tracked_embeds_many.include?(database_field_name(relation))
end
tracked_embeds_many_attributes(relation) click to toggle source
# File lib/mongoid/history/trackable.rb, line 560
def tracked_embeds_many_attributes(relation)
  history_trackable_options[:relations][:embeds_many][database_field_name(relation)]
end
tracked_embeds_one() click to toggle source

Retrieves the memoized list of tracked embeds_one relations

@return [ Array < String > ] the list of tracked embeds_one relations

# File lib/mongoid/history/trackable.rb, line 528
def tracked_embeds_one
  @tracked_embeds_one ||= begin
    reflect_on_all_associations(:embeds_one)
      .map(&:key)
      .select { |rel| history_trackable_options[:relations][:embeds_one].include? rel }
  end
end
tracked_embeds_one?(relation) click to toggle source

Whether or not the embeds_one relation should be tracked.

@param [ String | Symbol ] relation The name of the embeds_one relation

@return [ Boolean ] whether or not the embeds_one relation is tracked

# File lib/mongoid/history/trackable.rb, line 521
def tracked_embeds_one?(relation)
  tracked_embeds_one.include?(database_field_name(relation))
end
tracked_embeds_one_attributes(relation) click to toggle source
# File lib/mongoid/history/trackable.rb, line 536
def tracked_embeds_one_attributes(relation)
  history_trackable_options[:relations][:embeds_one][database_field_name(relation)]
end
tracked_field?(field, action = :update) click to toggle source

Whether or not the field should be tracked.

@param [ String | Symbol ] field The name or alias of the field @param [ String | Symbol ] action The optional action name (:create, :update, or :destroy)

@return [ Boolean ] whether or not the field is tracked for the given action

# File lib/mongoid/history/trackable.rb, line 453
def tracked_field?(field, action = :update)
  dynamic_field?(field) || tracked_fields_for_action(action).include?(database_field_name(field))
end
tracked_fields() click to toggle source

Retrieves the memoized base list of tracked fields, excluding reserved fields.

@return [ Array < String > ] the base list of tracked database field names

# File lib/mongoid/history/trackable.rb, line 487
def tracked_fields
  @tracked_fields ||= history_trackable_options[:fields] + history_trackable_options[:dynamic]
end
tracked_fields_for_action(action) click to toggle source

Retrieves the list of tracked fields for a given action.

@param [ String | Symbol ] action The action name (:create, :update, or :destroy)

@return [ Array < String > ] the list of tracked fields for the given action

# File lib/mongoid/history/trackable.rb, line 477
def tracked_fields_for_action(action)
  case action.to_sym
  when :destroy then tracked_fields + reserved_tracked_fields
  else tracked_fields
  end
end
tracked_relation?(relation) click to toggle source

Whether or not the relation should be tracked.

@param [ String | Symbol ] relation The name of the relation

@return [ Boolean ] whether or not the relation is tracked

# File lib/mongoid/history/trackable.rb, line 512
def tracked_relation?(relation)
  tracked_embeds_one?(relation) || tracked_embeds_many?(relation)
end