Class: ActsAsTaggableOnMongoid::Taggable::Utils::TagListDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_taggable_on_mongoid/taggable/utils/tag_list_diff.rb

Overview

:reek:TooManyInstanceVariables :reek:InstanceVariableAssumption

Instance Method Summary collapse

Constructor Details

#initialize(tag_definition:, tags:, current_tags:) ⇒ TagListDiff

Returns a new instance of TagListDiff.



18
19
20
21
22
23
24
25
# 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

  @old_tags = {}
  @new_tags = {}
end

Instance Method Details

#callObject



27
28
29
30
31
32
# 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) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# 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)

    next if tagging.save
    next if ignore_tagging_error(tagging)

    # re-raise error.
    tagging.save!
  end
end

#destroy_old_tags(taggable) ⇒ Object



48
49
50
51
52
53
54
55
56
# 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_context(tag_definition.tag_type).
      where(:tag_name.in => old_tags.map(&:name)).
      destroy_all
end