Socket
Book a DemoInstallSign in
Socket

acts_as_active

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

acts_as_active

0.3.0
bundlerRubygems
Version published
Maintainers
1
Created
Source

ActsAsActive

Acts As Active adds plug-and-play activity tracking to any ActiveRecord model, giving you instant daily stats, streak analytics, and heatmap-ready data.

It works by automatically establishing a polymorphic association with your model and generating an Activity record for each specified lifecycle event.

Installation

Install the gem and add to the application's Gemfile by executing:

bundle add acts_as_active

If bundler is not being used to manage dependencies, install the gem by executing:

gem install acts_as_active

Generators: ActsAsActive supports a metadata field in either json or jsonb format, depending on your database and preference (MySQL or PostgreSQL).

rails g acts_as_active:install --metadata=json/jsonb/auto

Usage

class Record < ApplicationRecord
  acts_as_active on: [:create, :update],                 # ➜ Track different actions
                 if:           -> { track_activity? },   # ➜ On different conditions
                 unless:       -> { skip_tracking? },
                 after_record: -> (activity) { activity.update(metadata: {be: "BOP!"}) }   # ➜ Hook: runs after an activity is created or updated
end
record = Record.create!(title: "First draft")   # ➜ Records activity for today
record.update!(title: "Second draft")           # ➜ Activity count for today = 2
 

record.active_today?                # => true
record.active_on?(Date.yesterday)   # => false
record.activity_count(range: 1.day.ago..Date.today)
# => 2   (two events in the last 24 h)


record.heatmap(range: 1.week.ago..Date.today)
# => { "2025-08-01" => 1, "2025-08-02" => 3, "2025-08-03" => 2 }

record.longest_streak   # => 3   (e.g. active 1-3 Aug)
record.current_streak   # => 2   (e.g. active 5-6 Aug, still “on a streak” today)

Accessing Activities

record = Record.find(1) 
record.activities  # Accessing activities for a model that includes ActsAsActive

ActsAsActive::Activity.where(trackable: record)  # Query the namespaced model directly
ActsAsActive::Activity.all                       # Get all activities across all trackable types

Metadata usage

PostgreSQL

# Where metadata contains key/value
ActsAsActive::Activity.where("metadata @> ?", { source: "api" }.to_json)

# Where nested key/value
ActsAsActive::Activity.where("metadata @> ?", { user: { id: 42 } }.to_json)

MySQL/SQLite

# MySQL: Find where metadata->source == 'api'
ActsAsActive::Activity.where("JSON_UNQUOTE(JSON_EXTRACT(metadata, '$.source')) = ?", "api")

# SQLite (with JSON1 extension):
ActsAsActive::Activity.where("json_extract(metadata, '$.source') = ?", "api")

Writing to metadata

activity.update!(
  metadata: {
    source: "api",
    user: { id: 42, name: "Amit" },
    tags: ["Dig", "Be", "Bop"]
  }
)

Run Tests

ruby -Ilib:ruby test/test_acts_as_active.rb

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/amitleshed/acts_as_active. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the ActsAsActive project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

FAQs

Package last updated on 12 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.