Module: ActsAsTaggableOn::Taggable::Ownership::InstanceMethods

Defined in:
lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb

Instance Method Summary collapse

Instance Method Details

#cached_owned_tag_list_on(context) ⇒ Object



46
47
48
49
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb', line 46

def cached_owned_tag_list_on(context)
  variable_name = "@owned_#{context}_list"
  cache = instance_variable_get(variable_name) || instance_variable_set(variable_name, {})
end

#owner_tag_list_on(owner, context) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb', line 51

def owner_tag_list_on(owner, context)
  add_custom_context(context)

  cache = cached_owned_tag_list_on(context)
  cache.delete_if { |key, value| key.id == owner.id && key.class == owner.class }
  
  cache[owner] ||= ActsAsTaggableOn::TagList.new(*owner_tags_on(owner, context).map(&:name))
end

#owner_tags_on(owner, context) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb', line 32

def owner_tags_on(owner, context)
  if owner.nil?
    scope = base_tags.where([%(#{ActsAsTaggableOn::Tagging.table_name}.context = ?), context.to_s])                    
  else
    scope = base_tags.where([%(#{ActsAsTaggableOn::Tagging.table_name}.context = ? AND
                               #{ActsAsTaggableOn::Tagging.table_name}.tagger_id = ? AND
                               #{ActsAsTaggableOn::Tagging.table_name}.tagger_type = ?), context.to_s, owner.id, owner.class.to_s])          
  end
  # when preserving tag order, return tags in created order
  # if we added the order to the association this would always apply
  scope = scope.order("#{ActsAsTaggableOn::Tagging.table_name}.id") if self.class.preserve_tag_order?
  scope.all
end

#reload(*args) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb', line 69

def reload(*args)
  self.class.tag_types.each do |context|
    instance_variable_set("@owned_#{context}_list", nil)
  end

  super(*args)
end

#save_owned_tagsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb', line 77

def save_owned_tags
  tagging_contexts.each do |context|
    cached_owned_tag_list_on(context).each do |owner, tag_list|
      
      # Find existing tags or create non-existing tags:
      tags = ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name(tag_list.uniq, owner, context)

      # Tag objects for owned tags
      owned_tags = owner_tags_on(owner, context)
      new_tags = tags - owned_tags

      # Create new taggings:
      new_tags.each do |tag|
        taggings.create!(:tag_id => tag.id, :context => context.to_s, :tagger => owner, :taggable => self)
      end
    end
  end
  
  true
end

#set_owner_tag_list_on(owner, context, tag_ids) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb', line 60

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

  cache[owner] = ActsAsTaggableOn::TagList.from(tag_ids)
end