{"id":15288825,"url":"https://github.com/mudafar/activerecord_activity_tracker","last_synced_at":"2026-01-05T15:07:01.614Z","repository":{"id":59150350,"uuid":"132162510","full_name":"mudafar/activerecord_activity_tracker","owner":"mudafar","description":"Simple yet powerful activity tracker for  Ruby on Rails ActiveRecord models.","archived":false,"fork":false,"pushed_at":"2018-05-04T18:41:22.000Z","size":358,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-06T05:11:15.651Z","etag":null,"topics":["activerecord","activity","activity-tracker","gem","news-feed","rails","ruby-on-rails","tracker"],"latest_commit_sha":null,"homepage":"","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mudafar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-04T16:13:03.000Z","updated_at":"2018-05-06T03:21:45.000Z","dependencies_parsed_at":"2022-09-13T11:00:35.224Z","dependency_job_id":null,"html_url":"https://github.com/mudafar/activerecord_activity_tracker","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudafar%2Factiverecord_activity_tracker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudafar%2Factiverecord_activity_tracker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudafar%2Factiverecord_activity_tracker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudafar%2Factiverecord_activity_tracker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mudafar","download_url":"https://codeload.github.com/mudafar/activerecord_activity_tracker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245217791,"owners_count":20579297,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","activity","activity-tracker","gem","news-feed","rails","ruby-on-rails","tracker"],"created_at":"2024-09-30T15:53:19.388Z","updated_at":"2026-01-05T15:07:01.586Z","avatar_url":"https://github.com/mudafar.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ActiverecordActivityTracker\n`activerecord_activity_tracker` provides simple yet powerful activity tracker for your Rails **ActiveRecord** models.\n\nThis gem will allows you to create the data model for social **news feed** used in many modern platform in no time.\n\n`Note:` This is an abstract gem, thus there are no views or controllers provided.\n\n\n\n\n\n\n\n\n\n\n\n### Features\n- Track model's creation and/or updating activities. \n- Track any custom activity's event easily.\n- Add optional data to any custom activity.\n- Set owner once, and use it in all tracked models seamlessly.\n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n## Usage\n\n### Track a model\n- Include `ActiverecordActivityTracker::ActsAsTrackable`\n- For automatic tracking add `acts_as_trackable`\n- For custom event use `create_ar_activity`\n\n### Set default owner (user)\n- Include `ActiverecordActivityTracker::Owner`\n- To set the owner use `set_owner(current_user)`\n- To get the owner use `get_owner`\n- To clear owner use `clear_owner`\n\n`Note:` setting the owner is required, as each activity must have an owner, see [set default owner example](#set-default-owner-to-devise-current_user).\n \n\n### Access model's tracked activities\n- Include `ActiverecordActivityTracker::ActsAsTrackable`\n- Use `ar_activities`\n\n### Examples\n\n#### Track comment's creation\n```ruby\n# app/models/comment.rb\nclass Comment \u003c ApplicationRecord\n  include ActiverecordActivityTracker::ActsAsTrackable\n\n  acts_as_trackable [:create]\nend\n ```\n \n#### Track comment's creation and modification\n```ruby\n# app/models/comment.rb\nclass Comment \u003c ApplicationRecord\n  include ActiverecordActivityTracker::ActsAsTrackable\n\n  acts_as_trackable [:create, :update]\nend\n ``` \n\n#### Track comment's custom event (title's change)\n\n```ruby\n# app/models/comment.rb\nclass Comment \u003c ApplicationRecord\n  include ActiverecordActivityTracker::ActsAsTrackable\n\n  after_update :add_title_change_activity\n    \n  private\n    \n  def add_title_change_activity\n    create_ar_activity(key: 'comment.title.change') if saved_change_to_title?\n  end\n    \nend\n ``` \n\n#### Set default owner to devise current_user\n`Note:` owner is a polymorphic relation, thus any model can be used, not just user.\n \n```ruby\n# app/controllers/application_controller.rb\nclass ApplicationController \u003c ActionController::Base\n  include ActiverecordActivityTracker::Owner\n\n  protect_from_forgery with: :exception\n\n  around_action :set_ar_activity_owner\n\n\n  private\n\n  def set_ar_activity_owner\n    set_owner current_user\n    yield\n  ensure\n    clear_owner\n  end\n\nend\n```\n\n### Access comment's activities\n\n```html\n\u003c!-- app/views/comments/show.erb --\u003e\n  \u003c% @comment.ar_activities.each do |activity| %\u003e\n      \u003c%= activity.key %\u003e\n      \u003cbr\u003e\n  \u003c% end %\u003e\n```\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n## Installation\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'activerecord_activity_tracker'\n```\n\nAnd then execute:\n```bash\n$ bundle\n```\n\nOr install it yourself as:\n```bash\n$ gem install activerecord_activity_tracker\n```\n\n### Setup\nExecute this line in your application's directory:\n```bash\n$ rails generate activerecord_activity_tracker:install\n``` \n\nAnd then execute:\n```bash\n$ rails db:migrate\n```\n\n\n\n\n\n\n\n\n\n\n\n\n\n## Documentation / API\n#### ActiverecordActivityTracker::ActsAsTrackable\n- `acts_as_trackable(tracked = [:create, :update]) =\u003e nil`\n    Overview:\n    Track model automatically.\n\n    Parameters:\n    tracked (array) (defaults to: [:create, :update]) -- instance options to set the tracked events.\n\n\n- `create_ar_activity(options = {}) =\u003e nil`\n    Overview:\n    Create custom activity, prevent duplication.\n\n    Options:\n    - key (string) (defaults to: \"#{model_name.param_key}.create\"). \n    - owner (active_record_relation) (defaults to: get_owner).\n    - data (string) (defaults to: nil).\n\n    Parameters:\n-- options (hash) (defaults to: {}) -- instance options to set custom params.\n\n\n- `create_ar_activity!(options = {}) =\u003e nil`\n    Overview:\n    Similar to `create_ar_activity` but allows duplication.\n\n\n\n- `has_many :ar_activities, as: :trackable, dependent: :destroy`\n    Overview: \n    Handle activities' relations.    \n    \n#### ActiverecordActivityTracker::Owner\n- `get_owner() =\u003e owner (active_record_model or nil)`\n    Overview: Get the current owner.\n\n- `set_owner(owner) =\u003e nil`\n    Overview: Set the current owner.\n    \n    Parameters:\n    owner (active_record_model) -- instance active record model to set current owner.\n    \n    \n- `clear_owner =\u003e nil`\n    Overview: Set the current owner to nil.   \n\n\n#### ArActivityTracker \n- ##### Description\n    Activity model, to handle trackable and owner relations.\n\n- ##### Data structure\n    `    t.string \"trackable_type\"\n     t.integer \"trackable_id\"\n     t.string \"owner_type\"\n     t.integer \"owner_id\"\n     t.string \"key\"\n     t.string \"data\"\n`\n- ##### Key default format\n    MODEL_NAME.ACTIVITY_TYPE\n\n    **For example:**\nGiven a `tracked` comment model `Comment`, then the keys (by default) will be:\n`comment.create` and `comment.update` for creation and modification activities respectively. \n\n\nFor more details see:\n[rubydoc](https://www.rubydoc.info/gems/activerecord_activity_tracker)\n\n\n\n\n\n\n\n## Contributing\n\n1. Fork it ( https://github.com/mudafar/activerecord_activity_tracker/fork ).\n2. Create your feature branch (`git checkout -b my-new-feature`).\n3. Test your changes to ensure they pass all tests (`bin/test`) .\n4. Commit your changes (`git commit -am 'Add some feature'`).\n5. Push to the branch (`git push origin my-new-feature`).\n6. Create a new Pull Request\n\n\n## License\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmudafar%2Factiverecord_activity_tracker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmudafar%2Factiverecord_activity_tracker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmudafar%2Factiverecord_activity_tracker/lists"}