Created
October 16, 2009 10:38
-
-
Save joshk/211700 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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