Skip to content

Commit

Permalink
added popular method
Browse files Browse the repository at this point in the history
Feature added to find popular tags

Refractored using new lambda syntax, added 'least' method

Removing white space

Fixing test so that taggable counts are set in rails 3.2

Restoring space for readability

Included information on new features 'most_used' & 'least_used'

Editted to display properly
  • Loading branch information
lolaodelola committed Aug 29, 2014
1 parent 35be549 commit 34a2fd5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
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
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
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
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 34a2fd5

Please sign in to comment.