{"id":20481172,"url":"https://github.com/mongoid/mongoid-observers","last_synced_at":"2025-04-13T14:10:50.835Z","repository":{"id":16015797,"uuid":"18759328","full_name":"mongoid/mongoid-observers","owner":"mongoid","description":"Mongoid observer (removed in Mongoid 4.0)","archived":false,"fork":false,"pushed_at":"2020-08-07T07:27:22.000Z","size":37,"stargazers_count":19,"open_issues_count":2,"forks_count":15,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-05-01T23:17:09.808Z","etag":null,"topics":["mongoid","rails","ruby"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/mongoid-observers","language":"Ruby","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/mongoid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-04-14T12:11:30.000Z","updated_at":"2021-10-27T14:50:34.000Z","dependencies_parsed_at":"2022-08-04T07:30:32.065Z","dependency_job_id":null,"html_url":"https://github.com/mongoid/mongoid-observers","commit_stats":null,"previous_names":["chamnap/mongoid-observers"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoid%2Fmongoid-observers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoid%2Fmongoid-observers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoid%2Fmongoid-observers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongoid%2Fmongoid-observers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mongoid","download_url":"https://codeload.github.com/mongoid/mongoid-observers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248724629,"owners_count":21151561,"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":["mongoid","rails","ruby"],"created_at":"2024-11-15T16:07:13.418Z","updated_at":"2025-04-13T14:10:50.815Z","avatar_url":"https://github.com/mongoid.png","language":"Ruby","readme":"# Mongoid::Observers [![Build Status](https://travis-ci.org/chamnap/mongoid-observers.svg?branch=master)](https://travis-ci.org/chamnap/mongoid-observers)[![Code Climate](https://codeclimate.com/github/chamnap/mongoid-observers.png)](https://codeclimate.com/github/chamnap/mongoid-observers)[![Coverage Status](https://coveralls.io/repos/chamnap/mongoid-observers/badge.png?branch=master)](https://coveralls.io/r/chamnap/mongoid-observers?branch=master)[![Dependency Status](https://gemnasium.com/chamnap/mongoid-observers.svg)](https://gemnasium.com/chamnap/mongoid-observers)\n\nMongoid Observers (removed from core in Mongoid 4.0). Because this gem doesn't exist and I need to use it very often. Therefore, I extract the code from mongoid on my own. It's basically the same code from mongoid before it's removed.\n\n## Installation\n\nFor Rails 5+, Add this line to your application's Gemfile:\n\n    gem 'mongoid-observers', '~\u003e 0.3.0'\n    gem 'rails-observers',  github: 'rails/rails-observers'\n\n**NOTE:** `mongoid-observers` depends on `rails-observers` mostly, but it is not yet ready for a new release on Rails 5 yet, https://github.com/rails/rails-observers/issues/53.\n\nFor Rails 4 and below, Add this line to your application's Gemfile:\n\n    gem 'mongoid-observers', '~\u003e 0.2.0'\n\nAnd then execute:\n\n    $ bundle\n\n## Usage\n\nObserver classes respond to life cycle callbacks to implement trigger-like\nbehavior outside the original class. This is a great way to reduce the\nclutter that normally comes when the model class is burdened with\nfunctionality that doesn't pertain to the core responsibility of the\nclass. Mongoid's observers work similar to ActiveRecord's. Example:\n\n```ruby\n  class CommentObserver \u003c Mongoid::Observer\n    def after_save(comment)\n      Notifications.comment(\n        \"admin@do.com\", \"New comment was posted\", comment\n      ).deliver\n    end\n  end\n```\n\nThis Observer sends an email when a Comment#save is finished.\n\n```ruby\n  class ContactObserver \u003c Mongoid::Observer\n    def after_create(contact)\n      contact.logger.info('New contact added!')\n    end\n\n    def after_destroy(contact)\n      contact.logger.warn(\"Contact with an id of #{contact.id} was destroyed!\")\n    end\n  end\n```\n\nThis Observer uses logger to log when specific callbacks are triggered.\n\n#### Observing a class that can't be inferred\n\nObservers will by default be mapped to the class with which they share a\nname. So CommentObserver will be tied to observing Comment,\nProductManagerObserver to ProductManager, and so on. If you want to\nname your observer differently than the class you're interested in\nobserving, you can use the Observer.observe class method which takes\neither the concrete class (Product) or a symbol for that class (:product):\n\n```ruby\n  class AuditObserver \u003c Mongoid::Observer\n    observe :account\n\n    def after_update(account)\n      AuditTrail.new(account, \"UPDATED\")\n    end\n  end\n```\n\nIf the audit observer needs to watch more than one kind of object,\nthis can be specified with multiple arguments:\n\n```ruby\n  class AuditObserver \u003c Mongoid::Observer\n    observe :account, :balance\n\n    def after_update(record)\n      AuditTrail.new(record, \"UPDATED\")\n    end\n  end\n```\n\nThe AuditObserver will now act on both updates to Account and Balance\nby treating them both as records.\n\n#### Available callback methods\n\n* after_initialize\n* before_validation\n* after_validation\n* before_create\n* around_create\n* after_create\n* before_update\n* around_update\n* after_update\n* before_upsert\n* around_upsert\n* after_upsert\n* before_save\n* around_save\n* after_save\n* before_destroy\n* around_destroy\n* after_destroy\n\n#### Storing Observers in Rails\n\nIf you're using Mongoid within Rails, observer classes are usually stored\nin `app/models` with the naming convention of `app/models/audit_observer.rb`.\n\n#### Configuration\n\nIn order to activate an observer, list it in the `config.mongoid.observers`\nconfiguration setting in your `config/application.rb` file.\n\n```ruby\n  config.mongoid.observers = :comment_observer, :signup_observer\n```\n\nObservers will not be invoked unless you define them in your\napplication configuration.\n\n#### Loading\n\nObservers register themselves with the model class that they observe,\nsince it is the class that notifies them of events when they occur.\nAs a side-effect, when an observer is loaded, its corresponding model\nclass is loaded.\n\nObservers are loaded after the application initializers, so that\nobserved models can make use of extensions. If by any chance you are\nusing observed models in the initialization, you can\nstill load their observers by calling `ModelObserver.instance` before.\nObservers are singletons and that call instantiates and registers them.\n\n## Authors\n\n* [Chamnap Chhorn](https://github.com/chamnap)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmongoid%2Fmongoid-observers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmongoid%2Fmongoid-observers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmongoid%2Fmongoid-observers/lists"}