Skip to content
Menu
Railshero
  • Blog
  • About Me
  • Contact Me
  • Privacy Policy
Railshero
Displaying Public Activity with rails

Displaying Public Activity with public_activity gem

Posted on July 21, 2021July 21, 2021 by Rails Hero

Activity Logs

An activity log is a record of logs that track user actions performed in an app. Activity logs are useful, because if something goes wrong, an user can check their activity log and see where they messed things up. Activity logs are common in social media sites like facebook, even github has one.

github activity log example
Github example

And This is the activity log of my app which i use for weather research.

activity log example
Weather app example

 

The public activity gem

We can display public activity in our app by building such kind of functionality from scratch. But to make life easier, there is a ruby gem called ‘public_activity’ that comes with its own methods to display public activity logs easily in our rails app. This gem displays activity logs that is very similar to Github’s activity logs. Also you can customize the look of your logs however you want, this gem already comes with methods to exactly do that.

Public Activity Github Page: https://github.com/chaps-io/public_activity

To add activity logging with public activity

So, in order to add activity logs, follow the steps below.

1. Add public activity to your Gemfile and run bundler

#File: Gemfile
gem 'public_activity'
bundle install

2. Now generate and run the migrations, this is only required if you are using active record.

rails g public_activity:migration
rails db:migrate

This will create a new table called ‘activities’ with the following columns.

activities table
activities table

3. The Model

Make your model trackable by public activity, this is my weather model

#File: app/models/weather.rb
class Weather < ApplicationRecord
   include PublicActivity::Model
   tracked
   belongs_to :user
end

4. The Controller

Now in your controller, i.e the controller where you want to display your public activity, add the activities instance variable so that you can query and display activities from your views. For instance, i want to display my activity logs in my Pages/Index page, so i’ll add it in my PagesController’s index action. Please note that i used kaminari for pagination, so i added the per page param. Check out my blog post on kaminari, if you are new to it.

#File: app/controllers/pages_controller.rb
class PagesController < ApplicationController
#...Other Methods...
   def index
      unless signed_in?
         redirect_to new_user_session_path
      else
         @activities = PublicActivity::Activity.order("created_at DESC").page(params[:page]).per(12)
      end
   end

#...Other Methods...
end

5. The Views

Now in my pages’ index view, i added an activity stream that will display the activities/actions performed by all users. I also added pagination, so that it shows only a few activities per page.

#File: app/views/pages/index.html.erb
<div class="activity-stream">
<% @activities.each do |activity| %>
<%= render_activity activity %>
<% end %>
</div>

<div class="pagination">
<%= paginate @activities %>
</div>

6. The Partials

The above view will display activities with the most basic view, that means it will display all activities with no styles and no line breaks so it will look jumbled up. We need to fix that by adding different partials for different activities performed (such as create, update and destroy).

Public activity looks for view in: app/views/public_activity

So, go ahead and create a new folder in your ‘views’ directory and name it ‘public_activity’.

Now, since my model name is ‘weather’ i will create a new folder inside app/views/public_activity and name it ‘weather’. Name this folder with the name of your model, for instance if your model name is ‘book’ name it ‘book’

Now, in this directory/folder, i.e; app/views/public_activity/weather. I will create 3 new partials for each action, i.e; create, update and destroy. I customized the look of my partials by adding fontawesome and a few basic styles, you can customize it however you want. These partials will display the activity name, city_name(specific to my weather app), activity created/updated/destroyed timestamps, etc. You can also add other details that you want to display,  Please check out Public Activity Github page for more info

The Create Partial

This partial will be displayed whenever a new record is created/added.

#File: app/views/public_activity/weather/_create.html.erb
<div class="stream">
<div class="stream-badge">
<i class="fa fa-globe"></i>
</div>
<div class="stream-panel">
<div class="stream-info">
</div>

<% if activity.trackable %>

<i class="fa fa-plus"></i>
Added a weather location: <%= link_to truncate(activity.trackable.city_name, :length => 71), activity.trackable %>
<small><span class="date">on: <%= activity.created_at.strftime('%d %b, %Y at %I:%M %p') %></span></small>

<% else %>

<i class="fa fa-window-close"></i>
Added a weather location, which has been removed
<small><span class="date">on: <%= activity.created_at.strftime('%d %b, %Y at %I:%M %p') %></span></small>
<% end %>

</div>
</div>

The Update Partial

This partial will be displayed whenever an existing record is updated/edited.

#File: app/views/public_activity/weather/_update.html.erb
<div class="stream">
<div class="stream-badge">
<i class="fa fa-globe"></i>
</div>
<div class="stream-panel">
<div class="stream-info">
</div>

<% if activity.trackable %>

<i class="fa fa-edit"></i>
Edited a weather location: <%= link_to truncate(activity.trackable.city_name, :length => 71), activity.trackable %>
<small><span class="date">on: <%= activity.created_at.strftime('%d %b, %Y at %I:%M %p') %></span></small>

<% else %>

<i class="fa fa-window-close"></i>
Edited a weather location, which has been removed
<small><span class="date">on: <%= activity.created_at.strftime('%d %b, %Y at %I:%M %p') %></span></small>
<% end %>

</div>
</div>

The Destroy Partial

This partial will be displayed whenever an existing record is destroyed/deleted.

#File: app/views/public_activity/weather/_destroy.html.erb
<div class="stream">
<div class="stream-badge">
<i class="fa fa-globe"></i>
</div>
<div class="stream-panel">
<div class="stream-info">
</div>

<i class="fa fa-window-close"></i>
Removed a weather location
<small><span class="date">on: <%= activity.created_at.strftime('%d %b, %Y at %I:%M %p') %></span></small>

</div>
</div>

Final Thoughts

You can also use a separate layout for your @activities, and it supports i18n as well. You can even create your own custom activities such as ‘user commented on’, ‘user flagged post’, ‘user liked post’, etc. And apart from active record, public activity also supports Mongoid and MongoMapper, please read the documentation; if you want to do so.

1 thought on “Displaying Public Activity with public_activity gem”

  1. Pingback: Some Popular Gems For Your Rails Apps - Railshero

Leave a Reply Cancel reply

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

Tags

active record active storage assets assign to many associations attachment attachments authentication authorization avatar bootstrap cdn config database deploy deployments devise DRY environment variables file uploads Gemfile gems has_many helpers heroku index indexing initializer javascript pagination parameters postgres production public routes.rb ruby gem ruby gems search sendgrid server smtp stylesheets variants views voting

Recent Posts

  • Understanding the DRY Principle in Rails
  • Building Multi-tenant Applications with Rails
  • Rails Basics: Templating Engines
  • Deploying With Capistrano
  • Automated Testing in Rails

Archives

  • July 2023
  • June 2023
  • October 2021
  • September 2021
  • August 2021
  • July 2021

Categories

  • Active Record
  • Activity Logging
  • Apps
  • Assets
  • Attachments
  • Audio
  • Authentication
  • Authorization
  • Deployments
  • Error Pages
  • File Uploads
  • General
  • Heroku
  • Heroku
  • Pagination
  • Rails Basics
  • RubyGems
  • Search Engine Optimization
  • Search/Indexing
  • Testing
  • User Interface
  • Video
  • Views & Templating
  • Voting
  • Web Security
©2025 Railshero | Theme: Wordly by SuperbThemes
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT