返回排行榜

public-activity/public_activity

Ruby

Easy activity tracking for models - similar to Github's Public Activity

activerecordrubyrailsactivity-streamtrackingnews-feedhackertoberfest
Star 增长趋势
Star
3k
Forks
330
周增长
Issues
17
1k2k
2023年1月2024年3月2025年5月2026年7月
制品库RubyGemsgem install public_activity
README

PublicActivity Code Climate Gem Version

public_activity provides easy activity tracking for your ActiveRecord, Mongoid 3 and MongoMapper models in Rails 6.1+. Simply put: it records what has been changed or created and gives you the ability to present those recorded activities to users - similarly to how GitHub does it.

Table of contents

Ruby/Rails version support

Version ~> 3.0`` supports Ruby 3.0+ and Rails 6.1+. For older Ruby versions (≤2.7) and Rails 5.0+ you can use version ~> 2.0` until you can upgrade to newer versions of Ruby + Rails.

Issues related to those unsupported versions of Ruby/Rails will be closed without resolution and PRs will not be accepted.

Example

Here is a simple example showing what this gem is about:

Example usage

Demo app

The source code of the demo is hosted here: https://github.com/pokonski/activity_blog

Screencast

Ryan Bates made a great screencast describing how to integrate Public Activity in your Rails Application.

Setup

Gem installation

You can install public_activity as you would any other gem:

gem install public_activity

or in your Gemfile:

gem 'public_activity'

Database setup

By default public_activity uses Active Record. If you want to use Mongoid or MongoMapper as your backend, create an initializer file in your Rails application with the corresponding code inside:

For Mongoid:

# config/initializers/public_activity.rb
PublicActivity::Config.set do
  orm :mongoid
end

For MongoMapper:

# config/initializers/public_activity.rb
PublicActivity::Config.set do
  orm :mongo_mapper
end

(ActiveRecord only) Create migration for activities and migrate the database (in your Rails project):

rails g public_activity:migration
rake db:migrate

Model configuration

Include PublicActivity::Model and add tracked to the model you want to keep track of:

For ActiveRecord:

class Article < ActiveRecord::Base
  include PublicActivity::Model
  tracked
end

For Mongoid:

class Article
  include Mongoid::Document
  include PublicActivity::Model
  tracked
end

For MongoMapper:

class Article
  include MongoMapper::Document
  include PublicActivity::Model
  tracked
end

And now, by default create/update/destroy activities are recorded in activities table. This is all you need to start recording activities for basic CRUD actions.

Optional: If you don't need #tracked but still want the comfort of #create_activity, you can include only the lightweight Common module instead of Model.

Custom activities

You can trigger custom activities by setting all your required parameters and triggering create_activity on the tracked model, like this:

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

See this entry http://rubydoc.info/gems/public_activity/PublicActivity/Common:create_activity for more details.

Displaying activities

To display them you simply query the PublicActivity::Activity model:

# notifications_controller.rb
def index
  @activities = PublicActivity::Activity.all
end

And in your views:

<%= render_activities(@activities) %>

Note: render_activity is a helper for use in view templates. render_activity(activity) can be written as activity.render(self) and it will have the same meaning.

Note: render_activities is an alias for render_activity and does the same.

Layouts

You can also pass options to both activity#render and #render_activity methods, which are passed deeper to the internally used render_partial method. A useful example would be to render activities wrapped in layout, which shares common elements of an activity, like a timestamp, owner's avatar etc:

<%= render_activities(@activities, layout: :activity) %>

The activity will be wrapped with the app/views/layouts/_activity.erb layout, in the above example.

Important: please note that layouts for activities are also partials. Hence the _ prefix.

Locals

Sometimes, it's desirable to pass additional local variables to partials. It can be done this way:

<%= render_activity(@activity, locals: {friends: current_user.friends}) %>

Note: Before 1.4.0, one could pass variables directly to the options hash for #render_activity and access it from activity parameters. This functionality is retained in 1.4.0 and later, but the :locals method is preferred, since it prevents bugs from shadowing variables from activity parameters in the database.

Activity views

public_activity looks for views in app/views/public_activity.

For example, if you have an activity with :key set to "activity.user.changed_avatar", the gem will look for a partial in app/views/public_activity/user/_changed_avatar.(erb|haml|slim|something_else).

Hint: the "activity." prefix in :key is completely optional and kept for backwards compatibility, you can skip it in new projects.

If a view file does not exist, then p_a falls back to the old behaviour and tries to translate the activity :key using I18n#translate method (see the section below).

I18n

Translations are used by the #text method, to which you can pass additional options in form of a hash. #render method uses translations when view templates have not been provided. You can render pure i18n strings by passing {display: :i18n} to #render_activity or #render.

Translations should be put in your locale .yml files. To render pure strings from I18n Example structure:

activity:
  article:
    create: 'Article has been created'
    update: 'Someone has edited the article'
    destroy: 'Some user removed an article!'

This structure is valid for activities with keys "activity.article.create" or "article.create". As mentioned before, "activity." part of the key is optional.

Testing

For RSpec you can first disable public_activity and add the test_helper in rails_helper.rb with:

#rails_helper.rb
require 'public_activity/testing'

PublicActivity.enabled = false

In your specs you can then blockwise decide whether to turn public_activity on or off.

# file_spec.rb
PublicActivity.with_tracking do
  # your test code goes here
end

PublicActivity.without_tracking do
  # your test code goes here
end

Documentation

For more documentation go here

Common examples

Help

If you need help with using public_activity please visit our discussion group and ask a question there:

https://groups.google.com/forum/?fromgroups#!forum/public-activity

Please do not ask general questions in the Github Issues.

License

Copyright (c) 2011-2013 Piotrek Okoński, released under the MIT license

相关仓库
rails/rails

Ruby on Rails

RubyRubyGemsMIT Licenserailsmvc
rubyonrails.org
58.6k22.4k
paper-trail-gem/paper_trail

Track changes to your rails models

RubyRubyGemsMIT Licenserubyactiverecord
7k911
activerecord-hackery/ransack

Object-based searching.

RubyRubyGemsMIT Licensesearchsearch-interface
activerecord-hackery.github.io/ransack/
5.9k823
aasm/aasm

AASM - State machines for Ruby classes (plain Ruby, ActiveRecord, Mongoid, NoBrainer, Dynamoid)

RubyRubyGemsMIT Licensestate-machineruby
5.2k644
ctran/annotate_models

Annotate Rails classes with schema and routes info

RubyRubyGemsOtherrubyrails
4.5k683
scenic-views/scenic

Versioned database views for Rails

RubyRubyGemsMIT Licenserubysql
thoughtbot.com/blog/announcing-scenic--versioned-database-views-for-rails
3.6k243
collectiveidea/audited

Audited (formerly acts_as_audited) is an ORM extension that logs all changes to your Rails models.

RubyRubyGemsMIT Licenseactiverecordaudit
3.5k658
sqlkata/querybuilder

SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird

C#MIT Licensesql-query-buildersql-server
sqlkata.com
3.4k526
elastic/elasticsearch-rails

Elasticsearch integrations for ActiveModel/Record and Ruby on Rails

RubyRubyGemsApache License 2.0rubyrails
3.1k801
thiagopradi/octopus

Database Sharding for ActiveRecord

RubyRubyGemsrubyruby-on-rails
2.5k500
magnusvk/counter_culture

Turbo-charged counter caches for your Rails app.

RubyRubyGemsMIT Licensecounter-cacherails
2.1k225
Shopify/identity_cache

IdentityCache is a blob level caching solution to plug into Active Record. Don't #find, #fetch!

RubyRubyGemsMIT Licensewebscaleactiverecord
shopify.github.io/identity_cache
2k173