Module: Tagtical::Taggable::Ownership::InstanceMethods

Defined in:
lib/tagtical/taggable/ownership.rb

Instance Method Summary collapse

Instance Method Details

#cached_owned_tag_list_on(context) ⇒ Object



39
40
41
42
# File 'lib/tagtical/taggable/ownership.rb', line 39

def cached_owned_tag_list_on(context)
  variable_name = find_tag_type!(context).tag_list_ivar(:owned)
  instance_variable_get(variable_name) || instance_variable_set(variable_name, {})
end

#owner_tag_list_on(owner, context) ⇒ Object



44
45
46
47
48
49
# File 'lib/tagtical/taggable/ownership.rb', line 44

def owner_tag_list_on(owner, context)
  cache = cached_owned_tag_list_on(context)
  cache.delete_if { |key, value| key.id == owner.id && key.class == owner.class }

  cache[owner] ||= Tagtical::TagList.new(*owner_tags_on(owner, context).map(&:value))
end

#owner_taggings(owner) ⇒ Object

Find all taggings that belong to the taggable (self), are owned by the owner, have the correct context, and are removed from the list.



103
104
105
106
107
# File 'lib/tagtical/taggable/ownership.rb', line 103

def owner_taggings(owner)
  relation = taggings.where(:tagger_id   => owner.id)
  relation = relation.where(:tagger_type => owner.class.to_s) if Tagtical.config.polymorphic_tagger?
  relation
end

#owner_tags_on(owner, context, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/tagtical/taggable/ownership.rb', line 30

def owner_tags_on(owner, context, options={})
  scope = tag_scope(context, options)
  if owner
    scope = scope.where([%{#{Tagtical::Tagging.table_name}.tagger_id = ?}, owner.id])
    scope = scope.where([%{#{Tagtical::Tagging.table_name}.tagger_type = ?}, owner.class.to_s]) if Tagtical.config.polymorphic_tagger?
  end
  scope.all
end

#reload(*args) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/tagtical/taggable/ownership.rb', line 58

def reload(*args)
  tag_types.each do |tag_type|
    ivar = tag_type.tag_list_ivar(:owned)
    remove_instance_variable(ivar) if instance_variable_defined?(ivar)
  end

  super(*args)
end

#save_owned_tagsObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tagtical/taggable/ownership.rb', line 67

def save_owned_tags
  # Do the classes from top to bottom. We want the list from "tag" to run before "sub_tag" runs.
  # Otherwise, we will end up removing taggings from "sub_tag" since they aren't on "tag'.
  tag_types.sort_by(&:active_record_sti_level).each do |tag_type|
    cached_owned_tag_list_on(tag_type).each do |owner, tag_list|
      # Find existing tags or create non-existing tags:
      tag_value_lookup = tag_type.scoping { find_or_create_tags(tag_list) }
      tags = tag_value_lookup.keys

      owned_tags = owner_tags_on(owner, tag_type, :scope => [:current, :parents, :children])
      old_tags   = owned_tags - tags
      new_tags   = tags       - owned_tags

      # Find and remove old taggings:
      if old_tags.present? && (old_taggings = owner_taggings(owner).find_all_by_tag_id(old_tags)).present?
        old_taggings.reject! do |tagging|
          if tagging.tag.class > tag_type.klass # parent of current tag type/class, make sure not to remove these taggings.
            update_tagging_with_inherited_tag!(tagging, new_tags, tag_value_lookup)
            true
          end
        end
        Tagtical::Tagging.destroy_all :id => old_taggings.map(&:id) # Destroy old taggings:
      end

      # Create new taggings:
      new_tags.each do |tag|
        taggings.create!(:tagger => owner, :relevance => tag_value_lookup[tag].relevance, :tag_id => tag.id)
      end
    end
  end

  true
end

#set_owner_tag_list_on(owner, context, new_list) ⇒ Object



51
52
53
54
55
56
# File 'lib/tagtical/taggable/ownership.rb', line 51

def set_owner_tag_list_on(owner, context, new_list)
  cache = cached_owned_tag_list_on(context)
  cache.delete_if { |key, value| key.id == owner.id && key.class == owner.class }

  cache[owner] = Tagtical::TagList.from(new_list)
end