Module: ActiveRecord::Acts::Taxonomy::ClassMethods

Defined in:
lib/taxonomy/has_taxonomy.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_taggableObject



13
14
15
# File 'lib/taxonomy/has_taxonomy.rb', line 13

def acts_as_taggable
  has_taxonomy_on :tags
end

#has_taxonomy_on(*args) ⇒ Object



17
18
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/taxonomy/has_taxonomy.rb', line 17

def has_taxonomy_on(*args)
  treed_args = []
  args.flatten! if args
  args.compact! if args 
  # pull out treed args
  args.each do |tag_type|
    if tag_type.is_a?(Hash)
      treed_tags = tag_type.values.flatten
      treed_tags.compact!
      
      treed_tags.each do |tree|
        treed_args << tree
      end
      args.delete(tag_type)
    end
  end
  
  if !(args & treed_args).empty?
    raise "duplicate taxonomy keys"
  end
  args.concat(treed_args)
  args.flatten!
  args.map!{|x| x.to_s} # convert to strings
  treed_args.map!{|x| x.to_s}
  args.each do |tag_type|
    # use aliased_join_table_name for context condition so that sphinx can join multiple
    # tag references from same model without getting an ambiguous column error
    class_eval do
      has_many "#{tag_type.singularize}_taggings".to_sym, :as => :taggable, :dependent => :destroy,
        :include => :tag, :conditions => ['tags.context = ?',tag_type.singularize], :class_name => "Tagging"
      has_many "#{tag_type}".to_sym, :through => "#{tag_type.singularize}_taggings".to_sym, :source => :tag
    end

    class_eval <<-RUBY
      def self.taggable?
        true
      end
      
      def self.#{tag_type}_treed?
        treed_list_on?("#{tag_type}")
      end

      def self.caching_#{tag_type.singularize}_list?
        caching_tag_list_on?("#{tag_type}")
      end

      def self.#{tag_type.singularize}_counts(options={})
        tag_counts_on('#{tag_type}',options)
      end

      def #{tag_type.singularize}_list
        tag_list_on('#{tag_type}')
      end

      def #{tag_type.singularize}_list=(new_tags)
        set_tag_list_on('#{tag_type}',new_tags)
      end

      def #{tag_type.singularize}_counts(options = {})
        tag_counts_on('#{tag_type}',options)
      end

      def #{tag_type}_from(owner)
        tag_list_on('#{tag_type}', owner)
      end

      def find_related_#{tag_type}(options = {})
        related_tags_for('#{tag_type}', self.class, options)
      end
      alias_method :find_related_on_#{tag_type}, :find_related_#{tag_type}

      def find_related_#{tag_type}_for(klass, options = {})
        related_tags_for('#{tag_type}', klass, options)
      end

      def find_matching_contexts(search_context, result_context, options = {})
        matching_contexts_for(search_context.to_s, result_context.to_s, self.class, options)
      end
    
      def find_matching_contexts_for(klass, search_context, result_context, options = {})
        matching_contexts_for(search_context.to_s, result_context.to_s, klass, options)
      end

      def top_#{tag_type}(limit = 10)
        tag_counts_on('#{tag_type}', :order => 'count desc', :limit => limit.to_i)
      end

      def self.top_#{tag_type}(limit = 10)
        tag_counts_on('#{tag_type}', :order => 'count desc', :limit => limit.to_i)
      end
    RUBY
    
  end
  if respond_to?(:tag_types)
    write_inheritable_attribute( :tag_types, (tag_types + args).uniq )
    write_inheritable_attribute( :treed_tag_types, treed_args)
  else
    class_eval do
      write_inheritable_attribute(:tag_types, args.uniq)
      class_inheritable_reader :tag_types
      
      write_inheritable_attribute( :treed_tag_types, treed_args.uniq)
      class_inheritable_reader :treed_tag_types

      has_many :taggings, :as => :taggable, :dependent => :destroy, :include => :tag
      has_many :base_tags, :class_name => "Tag", :through => :taggings, :source => :tag

      attr_writer :custom_contexts

      before_save :save_cached_tag_list
      after_save :save_tags

      if respond_to?(:scope)
        scope :tagged_with, lambda{ |*args|
          find_options_for_find_tagged_with(*args)
        }
      end
      
    end

    include ActiveRecord::Acts::Taxonomy::InstanceMethods
    extend ActiveRecord::Acts::Taxonomy::SingletonMethods
    alias_method_chain :reload, :tag_list
  end
end

#taggable?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/taxonomy/has_taxonomy.rb', line 9

def taggable?
  false
end