PublicActivity

repository·main·Indexed 25 days ago

https://github.com/public-activity/public_activity

A Rails gem providing activity tracking for ActiveRecord, Mongoid, and MongoMapper models. It records CRUD actions and custom events to create GitHub-style activity feeds, supporting customizable view templates, I18n translations, and polymorphic associations for owners and recipients.

Tokens
2.8K
Snippets
9
Records
31
Agent score
84%

What's inside public_activity

  1. Use I18n for activity translations

    main

    If no view template is found, the gem falls back to I18n translations. You can render pure I18n strings by passing {display: :i18n} to the render methods. Define translations in your locale .yml files using the activity key structure.

    activity:
      article:
        create: 'Article has been created'
        update: 'Someone has edited the article'
        destroy: 'Some user removed an article!'
  2. Test activities with RSpec

    main

    To test with RSpec, you can disable public_activity in rails_helper.rb and use PublicActivity.with_tracking or PublicActivity.without_tracking blocks to control tracking during tests.

    # rails_helper.rb
    require 'public_activity/testing'
    PublicActivity.enabled = false
    
    # file_spec.rb
    PublicActivity.with_tracking do
      # your test code goes here
    end
  3. Track activities in ActiveRecord, Mongoid, or MongoMapper models

    main

    To enable activity tracking, include PublicActivity::Model and call the tracked method within your model class.

    # ActiveRecord
    class Article < ActiveRecord::Base
      include PublicActivity::Model
      tracked
    end
    
    # Mongoid
    class Article
      include Mongoid::Document
      include PublicActivity::Model
      tracked
    end
    
    # MongoMapper
    class Article
      include MongoMapper::Document
      include PublicActivity::Model
      tracked
    end
  4. Configure ORM for Mongoid or MongoMapper

    main

    By default, public_activity uses ActiveRecord. To use Mongoid or MongoMapper, create an initializer file at config/initializers/public_activity.rb and set the ORM.

    # For Mongoid
    PublicActivity::Config.set do
      orm :mongoid
    end
    
    # For MongoMapper
    PublicActivity::Config.set do
      orm :mongo_mapper
    end