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



50
51
52
53
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb', line 50

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

#owner_tag_list_on(owner, context) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb', line 55

def owner_tag_list_on(owner, context)
  add_custom_context(context)

  cache = cached_owned_tag_list_on(context)
  
  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
45
46
47
48
# 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.base_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
  if self.class.preserve_tag_order?
    scope.order("#{ActsAsTaggableOn::Tagging.table_name}.id")
  else 
    scope
  end
end

#reload(*args) ⇒ Object



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

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

  super(*args)
end

#save_owned_tagsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb', line 79

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)            

      # Tag objects for owned tags
      owned_tags = owner_tags_on(owner, context)
         
      # Tag maintenance based on whether preserving the created order of tags
      if self.class.preserve_tag_order?
        old_tags, new_tags = owned_tags - tags, tags - owned_tags

        shared_tags = owned_tags & tags

        if shared_tags.any? && tags[0...shared_tags.size] != shared_tags
          index = shared_tags.each_with_index { |_, i| break i unless shared_tags[i] == tags[i] }

          # Update arrays of tag objects
          old_tags |= owned_tags.from(index)
          new_tags |= owned_tags.from(index) & shared_tags

          # Order the array of tag objects to match the tag list
          new_tags = tags.map { |t| new_tags.find { |n| n.name.downcase == t.name.downcase } }.compact
        end
      else
        # Delete discarded tags and create new tags
        old_tags = owned_tags - tags
        new_tags = tags - owned_tags
      end
    
      # Find all taggings that belong to the taggable (self), are owned by the owner, 
      # have the correct context, and are removed from the list.
      if old_tags.present?
        old_taggings = ActsAsTaggableOn::Tagging.where(:taggable_id => id, :taggable_type => self.class.base_class.to_s,
                                                       :tagger_type => owner.class.base_class.to_s, :tagger_id => owner.id,
                                                       :tag_id => old_tags, :context => context)
      end
    
      # Destroy old taggings:
      if old_taggings.present?
        ActsAsTaggableOn::Tagging.destroy_all(:id => old_taggings.map(&:id))
      end

      # 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, new_list) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/ownership.rb', line 63

def set_owner_tag_list_on(owner, context, new_list)
  add_custom_context(context)
  
  cache = cached_owned_tag_list_on(context)

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