Sha256: c2d31da1500431ee52c007bf0ada6bba794bd925d2105432039e6d380659fdf3

Contents?: true

Size: 1.83 KB

Versions: 14

Compression:

Stored size: 1.83 KB

Contents

module Tagalong
  class TagManager
    def initialize(taggable, tagger)
      @taggable = taggable
      @tagger = tagger
    end

    def add_tag(name)
      tagger_tag = tagger_used_tag?(name)
      if (tagger_tag != nil)
        if !taggable_has_tag?(name)
          associate_tag_with_taggable(tagger_tag, @taggable)
        end
      else
        tagger_tag = create_tag_for_tagger(name, @tagger)
        associate_tag_with_taggable(tagger_tag, @taggable)
      end
    end

    def remove_tag(name)
      tagger_tag = tagger_used_tag?(name)
      if tagger_tag && taggable_has_tag?(name)
        disassociate_tag_from_taggable(tagger_tag, @taggable)
      end
    end

    def tagger_used_tag?(name)
      @tagger.tagalong_tags.find_by_name(name)
    end

    def taggable_has_tag?(name)
      @taggable.tagalong_tags.find_by_name(name)
    end

    private

    def create_tag_for_tagger(name, tagger)
      return TagalongTag.create!(:tagger_id => tagger.id, :tagger_type => tagger.class.to_s, :name => name)
    end
    
    def associate_tag_with_taggable(tag, taggable)
      TagalongTagging.create!(:taggable_id => taggable.id, :taggable_type => taggable.class.to_s, :tagalong_tag_id => tag.id)
      increment_tag_number_of_references(tag)
    end

    def disassociate_tag_from_taggable(tag, taggable)
      taggable_tagging = TagalongTagging.find_by_tagalong_tag_id_and_taggable_id(tag.id, taggable.id)
      TagalongTagging.destroy(taggable_tagging.id)
      taggable.tagalong_tags(true)
      decrement_tag_number_of_references(tag)
    end

    def increment_tag_number_of_references(tag)
      tag.number_of_references = (tag.number_of_references || 0) + 1
      tag.save!
    end

    def decrement_tag_number_of_references(tag)
      tag.number_of_references = tag.number_of_references ? tag.number_of_references - 1 : 0
      tag.save!
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
tagalong-0.1.4 lib/tagalong/tag_manager.rb
tagalong-0.1.3 lib/tagalong/tag_manager.rb
tagalong-0.1.2 lib/tagalong/tag_manager.rb
tagalong-0.1.1 lib/tagalong/tag_manager.rb
tagalong-0.1.0 lib/tagalong/tag_manager.rb
tagalong-0.0.9 lib/tagalong/tag_manager.rb
tagalong-0.0.8 lib/tagalong/tag_manager.rb
tagalong-0.0.7 lib/tagalong/tag_manager.rb
tagalong-0.0.6 lib/tagalong/tag_manager.rb
tagalong-0.0.5 lib/tagalong/tag_manager.rb
tagalong-0.0.4 lib/tagalong/tag_manager.rb
tagalong-0.0.3 lib/tagalong/tag_manager.rb
tagalong-0.0.2 lib/tagalong/tag_manager.rb
tagalong-0.0.1 lib/tagalong/tag_manager.rb