public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
mephisto / lib / mephisto / taggable_methods.rb
100644 30 lines (27 sloc) 0.919 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module Mephisto
  module TaggableMethods
    def self.included(base)
      base.has_many :taggings, :as => :taggable, :class_name => '::Tagging'
      base.has_many :tags, :through => :taggings, :order => 'tags.name', :class_name => '::Tag'
      base.after_save :save_tags
      base.send :attr_writer, :tag
      base.extend ClassMethods
    end
    
    def tag
      @tag ||= tags.collect(&:name) * ', '
    end
    
    protected
      def save_tags
        Tagging.set_on self, @tag if @tag
        @tag = nil
      end
      
    module ClassMethods
      def find_tagged_with(list)
        find :all, :select => "#{table_name}.*", :from => "#{table_name}, tags, taggings",
          :conditions => ["#{table_name}.#{primary_key} = taggings.taggable_id
and taggings.taggable_type = ?
and taggings.tag_id = tags.id and tags.name IN (?)", name, Tag.parse(list)]
      end
    end
  end
end