Sha256: bbf5ceec734b6eb0c5fc5b3ba06e5f3b824ed08295f646ecbcf954495bfe39f4
Contents?: true
Size: 1.48 KB
Versions: 14
Compression:
Stored size: 1.48 KB
Contents
class Tagging < ActiveRecord::Base belongs_to :tag belongs_to :tagged, :polymorphic => true named_scope :with, lambda { |tag| { :conditions => ["taggings.tag_id = ?", tag.id] } } named_scope :with_any_of_these, lambda { |tags| { :select => "taggings.*", :conditions => ["taggings.tag_id IN (#{tags.map{'?'}.join(',')})", *tags.map{|t| t.is_a?(Tag) ? t.id : t}], :group => "taggings.tagged_type, taggings.tagged_id" } } # this scope underpins a lot of the faceting: # it returns a list of taggings of objects to whom all the supplied tags have been applied # and is always used with map(&:tagged) to get a list of objects named_scope :with_all_of_these, lambda { |tags| { :select => "taggings.*", :conditions => ["taggings.tag_id IN (#{tags.map{'?'}.join(',')})", *tags.map{|t| t.is_a?(Tag) ? t.id : t}], :group => "taggings.tagged_type, taggings.tagged_id", :having => "COUNT(taggings.tag_id) >= #{tags.length}" } } # this takes a class name and returns all the taggings of that class # eg. @tag.taggings.of_a('page') named_scope :of_a, lambda { |klass| klass = klass.to_s.titleize { :conditions => "tagged_type = '#{klass}'", } } # good housekeeping idea from tags extension. # if all the taggings for a particular tag are deleted, we want to delete the tag too def before_destroy tag.destroy_without_callbacks if Tagging.with(tag).count < 1 end end
Version data entries
14 entries across 14 versions & 1 rubygems