Module: ActsAsTaggableOnPadrino::Taggable::Ownership::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#cached_owned_tag_list_on(context) ⇒ Object



40
41
42
43
# File 'lib/acts_as_taggable_on_padrino/taggable/ownership.rb', line 40

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



45
46
47
48
49
50
51
52
# File 'lib/acts_as_taggable_on_padrino/taggable/ownership.rb', line 45

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] ||= ActsAsTaggableOnPadrino::Taggable::TagList.new(*owner_tags_on(owner, context).names)
end

#owner_tags_on(owner, context) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/acts_as_taggable_on_padrino/taggable/ownership.rb', line 32

def owner_tags_on(owner, context)
  if owner
    base_tags.where(:taggings => {:context => context.to_s, :tagger_id => owner.id, :tagger_type => owner.class.to_s})
  else
    base_tags.where(:taggings => {:context => context.to_s})
  end
end

#reload(*args) ⇒ Object



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

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

  super
end

#save_owned_tagsObject



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
100
# File 'lib/acts_as_taggable_on_padrino/taggable/ownership.rb', line 71

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:
      tag_list = ActsAsTaggableOnPadrino::Tag.find_or_create_all_with_like_by_name(tag_list.uniq)

      owned_tags = owner_tags_on(owner, context)
      old_tags   = owned_tags - tag_list
      new_tags   = tag_list   - owned_tags

      # Find all taggings that belong to the taggable (self), are owned by the owner,
      # have the correct context, and are removed from the list.
      old_taggings = ActsAsTaggableOnPadrino::Tagging.where(:taggable_id => id, :taggable_type => self.class.base_class.to_s,
                                                     :tagger_type => owner.class.to_s, :tagger_id => owner.id,
                                                     :tag_id => old_tags, :context => context)

      if old_taggings.present?
        # Destroy old taggings:
        ActsAsTaggableOnPadrino::Tagging.destroy_all(:id => old_taggings.map {|tagging| tagging.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



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

def set_owner_tag_list_on(owner, context, new_list)
  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] = ActsAsTaggableOnPadrino::Taggable::TagList.from(new_list)
end