Module: HasMachineTags::ClassMethods

Defined in:
lib/has_machine_tags.rb

Instance Method Summary collapse

Instance Method Details

#has_machine_tags(options = {}) ⇒ Object

Options:

[:console] When true, adds additional instance methods to use mainly in irb. [:reverse_has_many] Defines a has_many :through from tags to the model using the plural of the model name. [:quick_mode] When true, enables a quick mode to input machine tags with HasMachineTags::InstanceMethods.tag_list=(). See examples at HasMachineTags::TagList.new().



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/has_machine_tags.rb', line 18

def has_machine_tags(options={})
  class << self; attr_accessor :quick_mode; end
  self.quick_mode = options[:quick_mode] || false
  self.class_eval do
    has_many :taggings, :as=>:taggable, :dependent=>:destroy
    has_many :tags, :through=>:taggings
    after_save :save_tags
    
    include HasMachineTags::InstanceMethods
    extend HasMachineTags::Finder
    include HasMachineTags::Console::InstanceMethods if options[:console]

    scope_word = ActiveRecord::VERSION::STRING >= '3.0' ? 'scope' : 'named_scope'
    send scope_word, :tagged_with, lambda  { |*args|
      find_options_for_tagged_with(*args)
    }
  end
  if options[:reverse_has_many]
    model = self.to_s
    'Tag'.constantize.class_eval do
      has_many(model.tableize.to_sym, :through => :taggings, :source => :taggable, :source_type =>model)
    end
  end
end