Module: ActiveRecord::Acts::TaggableOn::ClassMethods

Defined in:
lib/acts_as_taggable_on/acts_as_taggable_on.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_taggableObject



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

def acts_as_taggable
  acts_as_taggable_on :tags
end

#acts_as_taggable_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
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 17

def acts_as_taggable_on(*args)
  args.flatten! if args
  args.compact! if args
  for tag_type in args
    tag_type = tag_type.to_s
    # 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 => ['#{aliased_join_table_name || Tagging.table_name rescue Tagging.table_name}.context = ?',tag_type], :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.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 all_#{tag_type}_list
        all_tags_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 )
  else
    class_eval do
      write_inheritable_attribute(:tag_types, args.uniq)
      class_inheritable_reader :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?(:named_scope)
        named_scope :tagged_with, lambda{ |*args|
          find_options_for_find_tagged_with(*args)
        }
      end
    end

    include ActiveRecord::Acts::TaggableOn::InstanceMethods
    extend ActiveRecord::Acts::TaggableOn::SingletonMethods
  end
end

#taggable?Boolean

Returns:

  • (Boolean)


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

def taggable?
  false
end