Module: DataMapper::Is::Taggable

Included in:
Resource::ClassMethods
Defined in:
lib/dm-is-taggable/is/version.rb,
lib/dm-is-taggable/is/taggable.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Constant Summary collapse

VERSION =
"0.9.11"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

fired when your plugin gets included into Resource



8
9
10
# File 'lib/dm-is-taggable/is/taggable.rb', line 8

def self.included(base)

end

Instance Method Details

#is_taggable(options = {}) ⇒ Object

Methods that should be included in DataMapper::Model. Normally this should just be your generator, so that the namespace does not get cluttered. ClassMethods and InstanceMethods gets added in the specific resources when you fire is :example



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dm-is-taggable/is/taggable.rb', line 19

def is_taggable(options={})

  # Add class-methods
  extend  DataMapper::Is::Taggable::ClassMethods
  # Add instance-methods
  include DataMapper::Is::Taggable::InstanceMethods
  
  # Make the magic happen
  options[:by] ||= []
  
  taggers_associations = ""
  options[:by].each do |tagger_class|
    taggers_associations << "belongs_to :#{Extlib::Inflection.underscore(tagger_class.to_s)}\n"
  end
  
  class_eval <<-RUBY
    remix n, :taggings

    enhance :taggings do
      belongs_to :tag
      belongs_to :#{Extlib::Inflection.underscore(self.to_s)}
      #{taggers_associations}
    end
    
    has n, :tags, :through => :#{Extlib::Inflection.underscore(self.to_s)}_tags
  RUBY
  
  Tag.class_eval <<-RUBY
    has n, :#{Extlib::Inflection.underscore(self.to_s)}_tags
    has n, :#{Extlib::Inflection.underscore(self.to_s).pluralize}, :through => :#{Extlib::Inflection.underscore(self.to_s)}_tags
  RUBY
  
  options[:by].each do |tagger_class|
    tagger_class.class_eval <<-RUBY
      is :tagger, :for => [#{self}]
    RUBY
  end
end