class ActsAsTaggableOnMongoid::Taggable::Utils::TagListDiff

:reek: TooManyInstanceVariables :reek: InstanceVariableAssumption

Attributes

current_tags[R]
new_tags[R]
old_tags[R]
shared_tags[R]
tag_definition[R]
tags[R]

Public Class Methods

new(tag_definition:, tags:, current_tags:) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/utils/tag_list_diff.rb, line 18
def initialize(tag_definition:, tags:, current_tags:)
  @tag_definition = tag_definition
  @tags           = tags
  @current_tags   = current_tags.map(&:tag).compact

  @old_tags = {}
  @new_tags = {}
end

Public Instance Methods

call() click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/utils/tag_list_diff.rb, line 27
def call
  @old_tags = current_tags - tags
  @new_tags = tags - current_tags

  preserve_tag_list_order
end
create_new_tags(taggable) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/utils/tag_list_diff.rb, line 34
def create_new_tags(taggable)
  new_tags.each do |tag|
    tagging = taggable.
        public_send(tag_definition.taggings_name).
        new(tag_name: tag.name, context: tag_definition.tag_type, taggable: taggable, tag: tag, tagger: tag.owner)

    next if tagging.save
    next if ignore_tagging_error(tagging)

    # re-raise error.
    tagging.save!
  end
end
destroy_old_tags(taggable) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/utils/tag_list_diff.rb, line 48
def destroy_old_tags(taggable)
  return if old_tags.blank?

  taggable.
      public_send(tag_definition.taggings_name).
      by_tag_type(tag_definition.tag_type).
      where(:tag_id.in => old_tags.map(&:id)).
      destroy_all
end

Private Instance Methods

first_ordered_difference() click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/utils/tag_list_diff.rb, line 107
def first_ordered_difference
  return @first_ordered_difference if defined?(@first_ordered_difference)

  index = 0

  while index < shared_tags.length
    break unless shared_tags[index] == tags[index]

    index += 1
  end

  @first_ordered_difference = index
end
ignore_tagging_error(tagging) click to toggle source

:reek: UtilityFunction ignore the error if it is that the tagging already exists.

# File lib/acts_as_taggable_on_mongoid/taggable/utils/tag_list_diff.rb, line 69
def ignore_tagging_error(tagging)
  tagging_errors = tagging.errors

  tagging_errors.count == 1 &&
      tagging.tag_name.present? &&
      tagging.tag.present? &&
      (tagging_errors.key?(:tag_name) || tagging_errors.key?(:tag_id))
end
preserve_new_tag_list_order() click to toggle source

:reek: NestedIterators

# File lib/acts_as_taggable_on_mongoid/taggable/utils/tag_list_diff.rb, line 96
def preserve_new_tag_list_order
  # Order the array of tag objects to match the tag list
  @new_tags = tags.map do |tag|
    preserved_tags.detect { |preserved_tag| preserved_tag.name == tag.name && preserved_tag.owner == tag.owner }
  end.compact
end
preserve_tag_list_order() click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/utils/tag_list_diff.rb, line 78
def preserve_tag_list_order
  return unless tag_definition.preserve_tag_order?

  @shared_tags = current_tags & tags

  return if share_tags_sorted?

  # Update arrays of tag objects
  @old_tags |= current_tags[first_ordered_difference..-1]

  preserve_new_tag_list_order
end
preserved_tags() click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/utils/tag_list_diff.rb, line 103
def preserved_tags
  @preserved_tags ||= new_tags | current_tags[first_ordered_difference..-1] & shared_tags
end
share_tags_sorted?() click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/utils/tag_list_diff.rb, line 91
def share_tags_sorted?
  shared_tags.none? || tags[0...shared_tags.size] == shared_tags
end