Sha256: ed5369f20bba193226650785bf2abb246fefe25fb38637539032be12dd211682

Contents?: true

Size: 1.72 KB

Versions: 32

Compression:

Stored size: 1.72 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)
          changed_attributes["tag_list"] = tag_list unless @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.strip) }
          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

32 entries across 32 versions & 10 rubygems

Version Path
SFEley-browsercms-3.0.2 lib/cms/behaviors/taggable.rb
buzzware-browsercms-3.0.2 lib/cms/behaviors/taggable.rb
coredumplings-browsercms-3.0.0 lib/cms/behaviors/taggable.rb
we5-browsercms-3.0.1.1 lib/cms/behaviors/taggable.rb
webficient-browsercms-3.0.1 lib/cms/behaviors/taggable.rb
webficient-browsercms-3.0.2 lib/cms/behaviors/taggable.rb
webficient-browsercms-3.0.3 lib/cms/behaviors/taggable.rb
webficient-browsercms-3.0.4 lib/cms/behaviors/taggable.rb
browsercms-3.1.5 lib/cms/behaviors/taggable.rb
browsercms-3.1.4 lib/cms/behaviors/taggable.rb
browsercms-3.1.3 lib/cms/behaviors/taggable.rb
bf4-browsercms-3.1.0 lib/cms/behaviors/taggable.rb
drujensen-browsercms-3.2.0 lib/cms/behaviors/taggable.rb
browsercmsi-3.1.2 lib/cms/behaviors/taggable.rb
browsercms-3.1.2 lib/cms/behaviors/taggable.rb
browsercms-3.1.1 lib/cms/behaviors/taggable.rb
browsercmsi-3.1.1 lib/cms/behaviors/taggable.rb
browsercmsi-3.1.0 lib/cms/behaviors/taggable.rb
browsercms-3.1.0 lib/cms/behaviors/taggable.rb
we5-browsercms-3.1.0 lib/cms/behaviors/taggable.rb