Skip to content

Instantly share code, notes, and snippets.

@joshk
Created October 16, 2009 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshk/211700 to your computer and use it in GitHub Desktop.
Save joshk/211700 to your computer and use it in GitHub Desktop.
module ScopedTags
module ActiveRecordAdditions
def self.included(base)
base.class_eval do
def self.scoped_tags(contexts, options = nil)
cattr_accessor :tag_contexts
raise ScopedTagsError, 'context is required for scoped-tags setup' if contexts.blank?
self.tag_contexts = [contexts].flatten
has_many :taggings, :as => :taggable, :class_name => 'Tagging', :dependent => :delete_all
has_many :tags, :through => :taggings, :class_name => 'Tag', :readonly => true
self.tag_contexts.each do |context|
has_many context, :through => :taggings, :class_name => 'Tag',
:source => :tag,
:conditions => ["context = ?", context.to_s.downcase]
c = context.to_s.singularize
define_method("#{c}_list") { get_tag_list(context.to_s.downcase) }
define_method("#{c}_list=") { |new_list| set_tag_list(context.to_s.downcase, new_list) }
self.class.instance_eval do
define_method("tagged_with_#{context}") { |context_names| }
end
end
self.send :include, InstanceMethods
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment