This is the initial code. Improvements are likely to be had.
- This creates an activity feed that can track updates, destroys, creates in a MySQL database.
- This is inspired by part of the "public_activity" gem.
- For Updates, what changed will be automatically stored as a hash.
- Bootstrap's usage is assumed, as is Devise's "current_user". ActiveRecord must be used as well. These can easily be changed however.
- The migration should be placed as a normal migration in the migrations folder and the
activity.rb
in the models folder. This will be the base of the work base_model.rb
should also be placed in the model folder and anything you would like to track should extend this model. This will default the tracking of updates, creates, and destroys.- 2 ERB snippets are included to help with rendering nice views. The
see_more.js.coffee
should be included to make the "see more" buttons on updates work. - You can track custom events by including an activity call.
Activity.register_activity(User.current_user, self, "changed their password", User.current_ip_address)
. The second arugment is what you are changing, the third argument is an action (or what happened). - Include a partial render call in a view you want to see the activity list
<%= render "activities/activities", activities: current_user.activities %>
. Note that the ERB templates are placed within an activities folder in the views directory. - Below are some snippets that also need to be added.
This snippet is a way to make the current_user and the ip address available to all models and callbacks for the current thread.
class << self
def current_user=(user)
Thread.current[:current_user] = user
end
def current_user
Thread.current[:current_user]
end
def current_ip_address=(ip)
Thread.current[:current_ip_address] = ip
end
def current_ip_address
Thread.current[:current_ip_address]
end
end
This snippet will set the current_user and ip_address for the current thread on every request.
before_filter :set_current_user_and_ip
def set_current_user_and_ip
User.current_user = current_user
User.current_ip_address = request.remote_ip
end
A simple helper that renders the activities
What rails will render when you call render activity
or render activities
The activity class, basic model with a class method to register an activity. This method is not necessary, but helps readability.
The magic class. This handles some callbacks to register activities on updates, creates and destroys. Subclass this for any model you would like to track.
Just some view helpers for keywords in the view, the icons, and using "a" vs "an"
Migration to add the activity model.
Handles clicking the see more link in _activity.html.erb