LOGS

Generating the Logs of activities

I use the public_activity gme for generating the log of the user’s activities. Following are the steps used for generating logs:

Step 1: Add the gem in the gemfile.

      gem 'public_activity'

run the bundle install

      $ bundle install

Step 2: Create a new table activities, model and controller. By using these commands:

      $ rails g public_activity:migration
      $ rake db:migrate
      $ rails g controller activities index

Step 3: Add the following lines in the models you want to track the acitivities.

     include PublicActivity::Model
     tracked owner: Proc.new{ |controller, model| controller && controller.current_user }

Step4: In your application_controller file add the following code:

     include PublicActivity::StoreController

Step 5: Add the following lines in the activities_controller to fetch the value of the owner of the activity. Create a index method in the controller and add the following lines:

      def index
      @activities_by_owner = PublicActivity::Activity.where(owner: current_user)
      @activities = PublicActivity::Activity.order("created_at Desc")
      end

Step 6: Now add the following lines in the index view of the activity to show the activities of the users:

      <% @activities.each do |activity| %>
       <div class="activity">
        <%= link_to activity.owner.first_name, activity.owner if activity.owner %>
        <%= render_activity activity %>
       </div>
     <% end %>

Step 7: Create the partials for the tracking the creating, updating and deleting. Create a new folder in the views/public_activity//_create.html.erb. Same for the updating and deleting.

      <% if activity.trackable %>
       <%= link_to activity.trackable.name, activity.trackable %>
      <% else %>
       which has since been removed
      <% end %> **Step 8:** Repeat these steps for tracking activities in all the views.

I used the railscast episode #406 for the generating the logs. Here is the link for reference http://railscasts.com/episodes/406-public-activity. and Sitepoint.com site for the reference https://www.sitepoint.com/activity-feeds-rails/

Thanks :)

Written on July 24, 2016