Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module ActiveRecord
- module Acts #:nodoc:
- module Taggable #:nodoc:
- def self.included(base)
- base.extend(ClassMethods)
- end
- module ClassMethods
- def acts_as_taggable(options = {})
- write_inheritable_attribute(:acts_as_taggable_options, {
- :taggable_type => ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s,
- :from => options[:from]
- })
- class_inheritable_reader :acts_as_taggable_options
- has_many :taggings, :as => :taggable, :dependent => true
- has_many :tags, :through => :taggings
- include ActiveRecord::Acts::Taggable::InstanceMethods
- extend ActiveRecord::Acts::Taggable::SingletonMethods
- end
- end
- module SingletonMethods
- def find_tagged_with(*list)
- find_by_sql([
- "SELECT #{table_name}.* FROM #{table_name}, tags, taggings " +
- "WHERE #{table_name}.#{primary_key} = taggings.taggable_id " +
- "AND taggings.taggable_type = ? " +
- "AND taggings.tag_id = tags.id AND tags.name IN (?)",
- acts_as_taggable_options[:taggable_type], list
- ]).uniq
- end
- def find_tagged_with_all (*list)
- tagged = list.collect { |name| find_tagged_with(name) }
- tagged.inject { |items, tagged| items & tagged }
- end
- def tags_with_count
- Tagging.count :conditions => ["taggable_type = ?", acts_as_taggable_options[:taggable_type]], :joins => "LEFT JOIN tags ON taggings.tag_id=tags.id", :group => 'tags.name', :order => 'tags.name'
- end
- end
- module InstanceMethods
- def tag_with(list)
- Tag.transaction do
- self.taggings.destroy_all
- Tag.parse(list).each do |name|
- if acts_as_taggable_options[:from]
- send(acts_as_taggable_options[:from]).tags.find_or_create_by_name(name).on(self)
- else
- Tag.find_or_create_by_name(name).on(self)
- end
- end
- end
- end
- def tag_list
- tags.collect { |tag| tag.name.include?(" ") ? "\"#{tag.name}\"" : tag.name }.join(" ")
- end
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment