Skip to content
NoamB edited this page Oct 21, 2011 · 14 revisions

In this tutorial we will build upon the app created at Simple Password Authentication so make sure you understand it.

Let's add the db stuff first:

    rails g sorcery_migration activity_logging

Which will create:

    class SorceryActivityLogging < ActiveRecord::Migration
      def self.up
        add_column :users, :last_login_at,     :datetime, :default => nil
        add_column :users, :last_logout_at,    :datetime, :default => nil
        add_column :users, :last_activity_at,  :datetime, :default => nil
        
        add_index :users, [:last_logout_at, :last_activity_at]
      end
    
      def self.down
        remove_index :users, [:last_logout_at, :last_activity_at]
        
        remove_column :users, :last_activity_at
        remove_column :users, :last_logout_at
        remove_column :users, :last_login_at
      end
    end

And add the submodule:

    # config/initializers/sorcery.rb
    Rails.application.config.sorcery.submodules = [:activity_logging, blabla, blublu, ...]

That's it, you have activity logging on! All recent activity times will be logged for each user.

Now for the fun stuff - Let's say you want to display on your site the usernames of the users currently logged in:

    # app/controllers/application_controller.rb
    ...
    helper_method :current_users_list
    protected
    def current_users_list
      current_users.map {|u| u.username}.join(", ")
    end
    ...
    # app/views/layouts/application.html.erb
    ...
    <% if current_user %>
      <div id="current_users"> Currently active users: <%= current_users_list %></div>
    <% end %>
    <%= yield %>
    ...

Important Note: - you must make the activity logging attributes you use accessible by using attr_accessible or by not naming them in attr_protected, otherwise the writing with update_attributes! inside the gem will fail! Be sure to use attr_accessible/protected only after the call to authenticates_with_sorcery!.

There you have it! Try logging in with two users from different browsers and they should see each other's name on the front page.