ActiveRecord Model for Mint 9 comments

posted Thursday, February 16, 2006 by topfunky

UPDATE: Now in action on this very site.

As mentioned earlier, I use Shaun Inman’s Mint for some of my sites.

The data is stored in an accessible table called mint_visit. I made a simple model to query that table so the data can be used elsewhere in your Rails app.

Someone mentioned wanting to make a Typo plugin that shows the most popular pages on your site. You could do many other things as well.

This is a very simple model. I hope to make it more full featured, so if you enhance it, send me a patch!

ActiveRecord model for Mint

Update

I implemented the Mint.visits_to_resource method which works nicely. I pass it url_for(:only_path => true) which makes it so all the alternate urls for this site are summed together (with no arguments, url_for gives the url of the current page).

In Typo, you can use the article_url() method which makes partial urls by default.

I wrote a simple helper and hacked it into ”_article.rhtml” and “read.rhtml” in the article views folder.

Here’s the helper:

def mint_page_views(url)
  %(<div class="mint_summary">
  This article has been viewed roughly #{Mint.visits_to_resource url} times in the past two weeks.
  </div>)
end

I’m using page caching, so the count won’t update every time the page is accessed. Still, the numbers are roughly accurate. I might make a cron task to sweep the cache once a day.

Eventually, this will make a nice little Rails plugin or a Typo sidebar.

9 comments

Leave a response

  • wow! that was quick! I’ll see how I can tinker with this…

  • Gravatar icon topfunky

    Sorry, Geir. I remembered it being mentioned somewhere, but I forgot that it was you.

    Thanks for the inspiration!

  • Gravatar icon mike

    Ah rails… the code was shorter than the copyright/license statement in the file’s header.

  • You know what would really rock that might be part of a standard mint model, would be an easy method to find statistics of a given page, so let’s say on a typo post page, you could put something like..

    “This post has had X visitors since: Date”

  • Gravatar icon topfunky

    That would be easy to do. I’ll look at it later tonight.

  • I kinda like this syntax:

    set_table_name ‘mint_visit’

  • Gravatar icon Lee

    That’s definitely useful. Thanks!

  • excellent work :)

  • Gravatar icon topfunky

    I changed the default to return all recorded hits for a url. A new class method also returns a Ruby Time object for the oldest hit still in the database.

    Because of how Mint optimizes itself, older per-page hits are cleared out of the database. Mine appears to be good for about a month back (I think you can also adjust the size of the database to store more hits before they are deleted).

Your Comment

Nuby on Rails

Geoffrey Grosenbach / Ruby / Code / Graphics / Design / Rails / Merb / Javascript / CSS

Manufactured with

Subscribe

Subscribe (RSS)