May 22nd, 12:06pm 0 comments

Tracking Views in Rails

ActsAsViewable is plugin that allows you to track page and asset views in your Rails application. For example, you can use it to track how many times a page is visited or how many times a particular image is viewed.


Trac: http://trac.intridea.com/trac/public/wiki/ActsAsViewable


Subversion repository: http://svn.intridea.com/svn/public/acts_as_viewable


Installation:

script/plugin install http://svn.intridea.com/svn/public/acts_as_viewable

OR


cd vendor/pluginssvn co http://svn.intridea.com/svn/public/acts_as_viewable

Create the tables where views will be tracked:


class CreateViewings < ActiveRecord::Migration  def self.up    create_table :viewings do |t|      t.column :viewable_type,  :string      t.column :viewable_id,    :integer      t.column :views,          :integer,   :default => 0      t.column :created_at,     :datetime, :null => false      t.column :updated_at,     :datetime    end  end  def self.down    drop_table :viewings  endend

Set the objects you want to track views for:

class SomeAsset < ActiveRecord::Base  acts_as_viewableend

Now you can increment views for these objects wherever you need to. For example in the show action of our SomeAssetController:


class SomeAssetController < ApplicationController  def show    @some_asset = SomeAsset.find(params[:id])    @some_asset.increment_views  endend

To get the number of views:


@some_asset.views
Posted
May 22nd, 11:59am 0 comments

Automatically Expiring Sessions in Rails

SessionExpiration is plugin that allows you to expire sessions after X seconds of
inactivity. Useful for when you want to automatically log out users if they’re idle.


Trac: http://trac.intridea.com/trac/public


Subversion repository: http://svn.intridea.com/svn/public/session_expiration/


Installation:

script/plugin install http://svn.intridea.com/svn/public/session_expiration

OR


cd vendor/pluginssvn co http://svn.intridea.com/svn/public/session_expiration

Specify when to expire session in your ApplicationController to do it site wide or you can do it for specific controllers:


class ApplicationController  expire_session_in 5.minutesend

If you want to run a method when the session expires use this:


class ApplicationController  expire_session_in 5.minutes, :after_expiration => :some_method  def some_method    flash[:notice] = "You have been logged out due to inactivity"  endend
Posted
May 20th, 11:41am 0 comments

Blogging Again, Finally

I think my blog has been down for at least 4 months now. I’ve had very little time to spare between work and the rest of life. I’m glad to say it’s finally back up and ported from Typo to Mephisto sitting on a brand new server. All the old blog posts and comments should still be there and the old URL’s should map to the same posts.


Also, this blog will probably take on a more personal tone now since most of the technical stuff will also be posted on the Intridea blog – http://www.intridea.com/blog. If you just want the Rails stuff sans me whining about life then check that out instead.


With any luck I’ll be making a few posts soon.

Posted