Sha256: 1ea5afbc41508c67ed1b102d05a3d95134b2ebbe906d01fbc7107e2f75c22eba

Contents?: true

Size: 1.64 KB

Versions: 3

Compression:

Stored size: 1.64 KB

Contents

module Cms
  module Behaviors
    module Taggable
      def self.included(model_class)
        model_class.extend(MacroMethods)
      end
      module MacroMethods      
        def taggable?
          !!@is_taggable
        end
        def is_taggable(options={})
          @is_taggable = true
          @tag_separator = options[:separator] || " "
          
          has_many :taggings, :as => :taggable
          has_many :tags, :through => :taggings, :order => "tags.name"                    
          
          named_scope :tagged_with, lambda{|t| {:include => {:taggings => :tag}, :conditions => ["tags.name = ?", t]} }          
                    
          after_save :save_tags          
                    
          extend ClassMethods
          include InstanceMethods
        end
      end
      module ClassMethods
        def tag_cloud
          Tagging.cloud(base_class.name)
        end
        def tag_separator
          @tag_separator
        end
      end
      module InstanceMethods
        def tag_list
          @tag_list ||= tags.reload.map(&:name).join(self.class.tag_separator)
        end
        def tag_list=(tag_names)
          @tag_list = tag_names
        end
        def save_tags
          tag_list_tags = tag_list.to_s.split(self.class.tag_separator).map{|t| Tag.find_or_create_by_name(t) }
          taggings.each do |tg|
            if tag_list_tags.include?(tg.tag)
              tag_list_tags.delete(tg.tag)
            else
              tg.destroy
            end
          end
          tag_list_tags.each{|t| taggings.create(:tag => t, :taggable => self) }
          self.tag_list = nil
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
nate-browsercms-3.0.210 lib/cms/behaviors/taggable.rb
nate-browsercms-3.0.211 lib/cms/behaviors/taggable.rb
browsercms-3.0.0 lib/cms/behaviors/taggable.rb