Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request mbleigh#577 from wegotcoders/popular-feature
Browse files Browse the repository at this point in the history
Popular feature
  • Loading branch information
seuros committed Aug 29, 2014
2 parents aba90b2 + 6c2a6e8 commit 4de147a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ As such, a _Feature_ would map to either major or minor. A _bug fix_ to a patch.
* Performance
* Misc

### Master [changes](https://github.com/mbleigh/acts-as-taggable-on/compare/v3.3.0...v3.4.0)

* Features
* [@damzcodes #577 Popular feature](https://github.com/mbleigh/acts-as-taggable-on/pull/577)

### [3.3.0 / 2014-07-08](https://github.com/mbleigh/acts-as-taggable-on/compare/v3.2.6...v3.3.0)

Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,22 @@ end
@user.tag_list # => ["north", "east", "south", "west"]
```

### Finding most or least used tags

You can find the most or least used tags by using:

```ruby
User.most_used
User.least_used
```

You can also filter the results by passing the method a limit, however the default limit is 50.

```ruby
User.most_used(10)
User.least_most(10)
```

### Finding Tagged Objects

Acts As Taggable On uses scopes to create an association for tags.
Expand Down Expand Up @@ -201,6 +217,7 @@ You can also use `:wild => true` option along with `:any` or `:exclude` option.

__Tip:__ `User.tagged_with([])` or `User.tagged_with('')` will return `[]`, an empty set of records.


### Relationships

You can find objects of the same type based on similar tags on certain contexts.
Expand Down
5 changes: 5 additions & 0 deletions lib/acts_as_taggable_on/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def validates_name_uniqueness?
end

### SCOPES:
scope :most_used, ->(limit = 20) { order('taggings_count desc').limit(limit) }
scope :least_used, ->(limit = 20) { order(:taggings_count).limit(limit) }

def self.named(name)
if ActsAsTaggableOn.strict_case_match
Expand Down Expand Up @@ -101,6 +103,9 @@ def count
end

class << self



private

def comparable_name(str)
Expand Down
20 changes: 20 additions & 0 deletions spec/acts_as_taggable_on/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,24 @@
end
end
end

describe 'popular tags' do
before do
%w(sports rails linux tennis golden_syrup).each_with_index do |t, i|
tag = ActsAsTaggableOn::Tag.new(name: t)
tag.taggings_count = i
tag.save!
end
end

it 'should find the most popular tags' do
expect(ActsAsTaggableOn::Tag.most_used(3).first.name).to eq("golden_syrup")
expect(ActsAsTaggableOn::Tag.most_used(3).length).to eq(3)
end

it 'should find the least popular tags' do
expect(ActsAsTaggableOn::Tag.least_used(3).first.name).to eq("sports")
expect(ActsAsTaggableOn::Tag.least_used(3).length).to eq(3)
end
end
end

0 comments on commit 4de147a

Please sign in to comment.