Rails

Users Activity Tracking on Rails

How to enable users activity tracking using the public_activity gem?

On Rails, the public_activity provides easy way for tracking activity for ActiveRecord, Mongoid 3 and MongoMapper models. Simply we have to put records what has been changed or created and that will gives the ability to present those recorded activities to users. Following Github screenshot showing the expected output from the Rails users activity tracking-

Following steps we have to follow: (ActiveRecord only)

Step 1:

Add gem 'public_activity' on your Gemfile

Step 2:

Run bundle install

Step 3:

Run rails g public_activity:migration

Then

Run rake db:migrate

Now let’s configure the Model

class Post < ActiveRecord::Base

include PublicActivity::Model

tracked

end

Custom activities

@post.create_activity key: 'post.commented_on', owner: current_user

View activities

#=> app/controller/notifications_controller.rb

def index

@activities = PublicActivity::Activity.all

end

That’s all for tracking all activities.

Happy coding 🙂


Posted

in

,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *