Punching Bag is a hit tracking plugin for Ruby on Rails that specializes in simple trending.
- Total hit count
- Hit counts for the last day, week, month, etc.
- Simple trending based on most hits in the last day, week, month, etc.
- Rake task to group old hit records for better performance
- ActsAsTaggableOn integration for trending tags/topics support
- Voight-Kampff integration for bot checking
- An existing Rails app
- Ruby >= 1.9
In your Gemfile add:
In the terminal run:
bundle install rails g punching_bag rake db:migrate
In your model add:
Tracking hits in your controller
class PostsController < ApplicationController def show @post.punch(request) end end
Getting a total hit count in your view
Getting a hit count for a time period in your view
Getting a list of the five all-time most hit posts
Getting a list of the 10 most hit posts for the last 24 hours
Post.most_hit(1.day.ago, 10) # limit is 5 by default, pass nil for no limits
Sorting posts based on all time hit count
Post.sort_by_popularity('DESC') # DESC by default, can also use ASC
Getting a hit count on a tag for the last month
Getting a list of the 10 most hit tags in the last week
Compressing old hit records to improve performance
The default settings combine records by day if they're older than 7 days, by month if they're older than 1 month and by year if they're older than 1 year
Compressing old hit records using custom settings
This time we'll combine records by day if they're older than 14 days, by month if they're older than 3 months and by year if they're older than 2 years
punching_bag:combinerake tasks is not run automatically. You'll have to run it manually or add it as a cron job.
punching_bag:combinerake task can take a while depending on how many records need to be combined.
- Passing the
requestobject to the
punchmethod is optional but without it requests from bots, crawlers and spiders will be tracked.
- See the Voight-Kampff documentation if you'd like to customize the list of user-agents considered bots.
- The tag related features will only work if you have ActsAsTaggableOn installed and enabled on the same models as Punching Bag.