module ActsAsTaggableOnMongoid::Taggable::Core

:reek: FeatureEnvy :reek: UtilityFunction

Constants

DYNAMIC_MODULE_NAME

Public Instance Methods

apply_post_processed_defaults() click to toggle source
Calls superclass method
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 44
def apply_post_processed_defaults
  defaults = super

  defaults |= set_tag_list_defaults

  defaults
end
taggable_mixin() click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 29
def taggable_mixin
  # https://thepugautomatic.com/2013/07/dsom/
  # Provides a description of what we're doing here and why.
  if const_defined?(DYNAMIC_MODULE_NAME, false)
    mod = const_get(DYNAMIC_MODULE_NAME)
  else
    mod = const_set(DYNAMIC_MODULE_NAME, Module.new)

    include mod
  end

  mod
end

Private Instance Methods

all_tags_list_on(tag_definition) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 174
def all_tags_list_on(tag_definition)
  tag_list_cache_on(tag_definition).flatten
end
all_tags_on(tag_definition) click to toggle source

Returns all tags of a given context

# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 180
def all_tags_on(tag_definition)
  scope = public_send(tag_definition.taggings_name).where(context: tag_definition.tag_type)

  # when preserving tag order, return tags in created order
  # if we added the order to the association this would always apply
  scope = scope.order_by(*tag_definition.taggings_order) if tag_definition.preserve_tag_order?

  scope
end
dirtify_tag_list(tagging) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 308
def dirtify_tag_list(tagging)
  tag_definition = tag_types[tagging.context]

  return unless tag_definition

  attribute_will_change! tag_definition.tag_list_name
end
extract_tag_list_changes(tag_definition) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 292
def extract_tag_list_changes(tag_definition)
  tag_list = tag_list_cache_on(tag_definition)

  # Find existing tags or create non-existing tags:
  tags         = find_or_create_tags_from_list_with_context(tag_definition, tag_list)
  current_tags = all_tags_on(tag_definition)

  tag_list_diff = ActsAsTaggableOnMongoid::Taggable::Utils::TagListDiff.new tag_definition: tag_definition,
                                                                            tags:           tags,
                                                                            current_tags:   current_tags

  tag_list_diff.call

  tag_list_diff
end
find_or_create_tags_from_list_with_context(tag_definition, tagger_tag_list) click to toggle source

Imported from `ActsAsTaggableOn`. It is simply easier to define a custom Tag class and define the tag to use that Tag class.

Override this hook if you wish to subclass {ActsAsTaggableOn::Tag} – context is provided so that you may conditionally use a Tag subclass only for some contexts.

@example Custom Tag class for one context

class Company < ActiveRecord::Base
  acts_as_taggable_on :markets, :locations

  def find_or_create_tags_from_list_with_context(tag_list)
    if context.to_sym == :markets
      MarketTag.find_or_create_all_with_like_by_name(tag_list)
    else
      super
    end
  end

@param [Array<String>] tagger_tag_list Tags to find or create grouped by tagger @param [Symbol] tag_definition The tag context for the tag_list

# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 338
def find_or_create_tags_from_list_with_context(tag_definition, tagger_tag_list)
  load_tags(tag_definition, tagger_tag_list)
end
get_tag_list_change(tag_definition) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 67
def get_tag_list_change(tag_definition)
  tag_list_name = tag_definition.tag_list_name

  return nil unless public_send("#{tag_list_name}_changed?")

  changed_value = new_record? ? tag_definition.default_tagger_tag_list(self) : changed_attributes[tag_list_name]
  current_value = tag_list_cache_on(tag_definition)

  return nil if (changed_value <=> current_value)&.zero?

  tag_list_change_value(tag_definition, changed_value, current_value)
end
get_tag_list_changed(tag_definition) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 88
def get_tag_list_changed(tag_definition)
  tag_list_name = tag_definition.tag_list_name

  return false unless changed_attributes.key?(tag_list_name)

  changed_value = new_record? ? tag_definition.default_tagger_tag_list(self) : changed_attributes[tag_list_name]
  current_value = tag_list_cache_on(tag_definition)

  !(changed_value <=> current_value)&.zero?
end
get_tag_list_was(tag_definition) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 99
def get_tag_list_was(tag_definition)
  default_tagger = tag_definition.tag_list_default_tagger(self)
  return tag_definition.default_tagger_tag_list(self)[default_tagger].dup if new_record?

  tag_list_name = tag_definition.tag_list_name

  if public_send "#{tag_list_name}_changed?"
    changed_attributes[tag_list_name][default_tagger].dup
  else
    public_send(tag_list_name).dup
  end
end
get_tag_lists_was(tag_definition) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 112
def get_tag_lists_was(tag_definition)
  return tag_definition.default_tagger_tag_list(self).dup if new_record?

  tag_list_name = tag_definition.tag_list_name

  if public_send "#{tag_list_name}_changed?"
    changed_attributes[tag_list_name].dup
  else
    public_send(tag_definition.tagger_tag_lists_name).dup
  end
end
get_tagger_list_was(tag_definition, tagger) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 124
def get_tagger_list_was(tag_definition, tagger)
  return nil unless tag_definition.tagger?
  return tag_definition.default_tagger_tag_list(self)[tagger].dup if new_record?

  tag_list_name = tag_definition.tag_list_name

  if public_send "#{tag_list_name}_changed?"
    changed_attributes[tag_list_name][tagger].dup
  else
    public_send(tag_definition.tagger_tag_list_name, tagger).dup
  end
end
load_tags(tag_definition, tagger_tag_list) click to toggle source

Find existing tags or create non-existing tags

# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 241
def load_tags(tag_definition, tagger_tag_list)
  tag_definition.tags_table.find_or_create_tagger_list_with_like_by_name(tag_definition, tagger_tag_list)
end
mark_tag_list_changed(new_list) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 214
def mark_tag_list_changed(new_list)
  tag_definition   = new_list.tag_definition
  current_tag_list = tag_list_cache_on(tag_definition)

  return if current_tag_list == new_list

  current_tag_list.notify_will_change
end
save_tags() click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 268
def save_tags
  # Don't call save_tags again if a related classes save while processing this funciton causes this object to re-save.
  return if @saving_tag_list

  @saving_tag_list = true

  tag_types.each_value do |tag_definition|
    next unless tag_list_cache_set_on(tag_definition)

    # List of currently assigned tag names
    tag_list_diff = extract_tag_list_changes(tag_definition)

    # Destroy old taggings:
    tag_list_diff.destroy_old_tags self

    # Create new taggings:
    tag_list_diff.create_new_tags self
  end

  @saving_tag_list = false

  true
end
set_default_value(tag_definition, default) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 263
def set_default_value(tag_definition, default)
  public_send(tag_definition.tagger_tag_lists_name)[default.tagger] = default
  changed_attributes.delete tag_definition.tag_list_name
end
set_tag_list(tag_definition, new_tags) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 54
def set_tag_list(tag_definition, new_tags)
  dup_tags        = Array.wrap(new_tags).dup
  options         = dup_tags.extract_options!.dup
  options[:parse] = options.fetch(:parse) { true }

  new_list          = tag_definition.parse(*dup_tags, options)
  new_list.taggable = self
  new_list.tagger   = tag_definition.tag_list_default_tagger(self) unless options.key?(:tagger)

  mark_tag_list_changed(new_list)
  tag_list_set(new_list)
end
set_tag_list_defaults() click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 245
def set_tag_list_defaults
  return [] unless new_record?

  defaulted_values = []
  tag_types.each_value do |tag_definition|
    next if instance_variable_defined?(tag_definition.tag_list_variable_name)

    default = tag_definition.taggable_default(self)
    next unless default.present?

    set_default_value(tag_definition, default)

    defaulted_values << tag_definition.tag_list_name
  end

  defaulted_values
end
tag_list_cache_on(tag_definition) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 143
def tag_list_cache_on(tag_definition)
  variable_name = tag_definition.tag_list_variable_name

  # if instance_variable_get(variable_name)
  #   instance_variable_get(variable_name)
  # elsif cached_tag_list_on(tag_definition) && ensure_included_cache_methods! && self.class.caching_tag_list_on?(tag_definition)
  #   instance_variable_set(variable_name, tag_definition.parse(cached_tag_list_on(tag_definition)))
  # else
  #   tag_list_set(ActsAsTaggableOnMongoid::TagList.new(tag_definition, tags_on(tag_definition).map(&:tag_name)))
  # end

  instance_variable_get(variable_name) ||
      tagger_tag_list_set(tagger_tag_list_from_taggings(tag_definition, all_tags_on(tag_definition)))
end
tag_list_cache_set_on(tag_definition) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 137
def tag_list_cache_set_on(tag_definition)
  variable_name = tag_definition.tag_list_variable_name

  instance_variable_defined?(variable_name) && instance_variable_get(variable_name)
end
tag_list_change_value(tag_definition, changed_value, current_value) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 80
def tag_list_change_value(tag_definition, changed_value, current_value)
  if tag_definition.tagger?
    [changed_value.dup, current_value.dup]
  else
    [changed_value[nil].dup, current_value[nil].dup]
  end
end
tag_list_on(tag_definition) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 168
def tag_list_on(tag_definition)
  # add_custom_context(tag_definition)

  tag_list_cache_on(tag_definition)[tag_definition.tag_list_default_tagger(self)]
end
tag_list_set(new_list) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 227
def tag_list_set(new_list)
  # add_custom_context(tag_definition, owner)

  new_list.taggable = self
  tag_definition    = new_list.tag_definition
  tagger_tag_list   = ActsAsTaggableOnMongoid::TaggerTagList.new(tag_definition, self)

  tagger_tag_list[new_list.tagger] = new_list

  instance_variable_set(tag_definition.tag_list_variable_name, tagger_tag_list)
end
tagger_tag_list_from_taggings(tag_definition, taggings) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 158
def tagger_tag_list_from_taggings(tag_definition, taggings)
  tagger_tag_list = ActsAsTaggableOnMongoid::TaggerTagList.new(tag_definition, self)

  taggings.each do |tagging|
    tagger_tag_list[tagging.tagger].silent_concat [tagging.tag_name]
  end

  tagger_tag_list
end
tagger_tag_list_set(new_list) click to toggle source
# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 223
def tagger_tag_list_set(new_list)
  instance_variable_set(new_list.tag_definition.tag_list_variable_name, new_list)
end
tagger_tags_on(tagger, tag_definition) click to toggle source

Returns all tags that are owned by a given tagger of a given context

# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 204
def tagger_tags_on(tagger, tag_definition)
  scope = public_send(tag_definition.taggings_name).where(context: tag_definition.tag_type, tagger: tagger)

  # when preserving tag order, return tags in created order
  # if we added the order to the association this would always apply
  scope = scope.order_by(*tag_definition.taggings_order) if tag_definition.preserve_tag_order?

  scope
end
tags_on(tag_definition) click to toggle source

Returns all tags that are not owned of a given context

# File lib/acts_as_taggable_on_mongoid/taggable/core.rb, line 192
def tags_on(tag_definition)
  scope = public_send(tag_definition.taggings_name).where(context: tag_definition.tag_type, :tagger_id.exists => false)

  # when preserving tag order, return tags in created order
  # if we added the order to the association this would always apply
  scope = scope.order_by(*tag_definition.taggings_order) if tag_definition.preserve_tag_order?

  scope
end