Simple activity feeds solution for Rails applications. Gives a flexible way to build activity feeds infrastructure over it.
Installation
Add the gem to your Gemfile and run the bundle install command to install it.
gem 'protocolist'Run the generator to create Activity model and migration.
rails generate protocolist:installRun migration then:
rake db:migrateGetting started
Activity model has four attributes: actor("who did it"), activity_type("what they did"), target("what they did it to") and data(additional information). Actor will be set as current user by default.
Protocolist expects you to have current_user method in a
controller. See Changing Defaults to change this behavior.
If creation isn't possible it will silently skip it.
Usage in models
The simplest way is just to write inside your model:
fires :createWhen "create" event will be triggered, it will automatically create
Activity with current user set as actor, :create as type,
self as target and empty data.
The more convenient usage:
fires :edit, on: :update,
data: :changes,
if: 'changes.any?'The event type will be edit. A proc, symbol and a string for data
option represent a method, else types will be stored as is.
The unless option also can be passed.
The on option can be an array:
fires :comment_activity, on: [:create, :update, :destroy]The most flexible way is to use fire method:
def destroy_projects
self.projects.destroy_all
fire :destroy_all, target: false, data: { company_id: company_id }
endIf you run without :target option set, it will default to self.
Usage in controllers
The simple one:
fires :downloadwhich will strike after download action.
The customized one:
fires :download, only: [:download_report, :download_file, :download_map],
data: ->(c) { c.params[:city] },
if: ->(c) { c.response.status == 200 }The fire method can be used same way as in models, but also if type is not
set, it will be set as action_name, and target will try to store @model_instance.
def show
@article = Article.find(params[:id])
fire
endis the same as
def show
@article = Article.find(params[:id])
fire :show, target: @article
endContributing
I would appreciate any help with gem development: pull requests, issue posts, etc.
Also there is probably a bunch of mistakes in English grammar/spelling. Any corrections are also appreciated, especially from native speakers.
Thanks
Protocolist was inspired by timeline_fu. I used it, but its functionality wasn't enough for me, so I made my own with blackjack and stewardesses.