module ActsAsTaggableOnMongoid::Taggable::TaggerRelation

Overides of methods from Mongoid::Processing which process attributes for methods like `assign_attributes`, `create`, `new`, and `update_attributes`

The need for this override is because the base method splits the order that methods are processed in to process relationships after processing other attributes.

However, tag lists may rely upon values set in relationships - forcing us to process tag lists AFTER relationships have been processed - preventing the need to order attributes (which wouldn't help anyway because of the way process_attributes works.)

ONLY taggings that have a default and which accept tagger values and which default the tagger based on the taggable object could be affected by other attributes set at the same time as the tag list. Thus only those tag values are delayed until after all other attributes are set.

Public Instance Methods

process_attributes(attrs = nil) click to toggle source
Calls superclass method
# File lib/acts_as_taggable_on_mongoid/taggable/tagger_relation.rb, line 19
def process_attributes(attrs = nil)
  update_attrs, defaulted_attrs = atom_attributes_without_defaults(attrs)

  super(update_attrs)

  defaulted_attrs.each do |key, value|
    public_send("#{key}=", value)
  end
end

Private Instance Methods

atom_attributes_without_defaults(attrs) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/tagger_relation.rb, line 31
def atom_attributes_without_defaults(attrs)
  return_attributes    = {}
  defaulted_attributes = {}

  sanitize_for_mass_assignment(attrs)&.each do |key, value|
    tag_def = atom_tag_definition_from_type(key)

    if tag_def&.tag_list_uses_default_tagger?
      defaulted_attributes[key] = value
    else
      return_attributes[key] = value
    end
  end

  [return_attributes, defaulted_attributes]
end
atom_tag_definition_from_type(key) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/tagger_relation.rb, line 48
def atom_tag_definition_from_type(key)
  tag_types.detect { |_type, tag_definition| tag_definition.tag_list_name == key.to_s }&.last
end