Skip to content

Commit

Permalink
Add methods for will_paginate compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Clinton R. Nixon committed Mar 13, 2008
1 parent 4925cab commit 551c75f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/acts_as_taggable.rb
Expand Up @@ -39,6 +39,13 @@ def find_tagged_with(*args)
options = find_options_for_find_tagged_with(*args)
options.blank? ? [] : find(:all, options)
end

# will_paginate's method_missing function wants to hit
# find_all_tagged_with if you call paginate_tagged_with, which is
# obviously suboptimal
def find_all_tagged_with(*args)
find_tagged_with(*args)
end

def find_options_for_find_tagged_with(tags, options = {})
tags = tags.is_a?(Array) ? TagList.new(tags.map(&:to_s)) : TagList.from(tags)
Expand Down Expand Up @@ -92,6 +99,12 @@ def find_options_for_find_tagged_with(tags, options = {})
def tag_counts(options = {})
Tag.find(:all, find_options_for_tag_counts(options))
end

# Find how many objects are tagged with a certain tag.
def count_by_tag(tag_name)
counts = tag_counts(:conditions => "tags.name = #{quote_value(tag_name)}")
counts[0].respond_to?(:count) ? counts[0].count : 0
end

def find_options_for_tag_counts(options = {})
options.assert_valid_keys :start_at, :end_at, :conditions, :at_least, :at_most, :order, :limit
Expand Down

0 comments on commit 551c75f

Please sign in to comment.