Giter Site home page Giter Site logo

everett's Introduction

Everett

Gem Version Build Status Code Climate Test Coverage

Everett is a substitute for Active Record Observer on Rails 5.

Installation

Put this in your Gemfile:

gem 'everett'

Run the installation generator with:

$ rails g everett:install

They will install the initializer into config/initializers/everett.rb.

Usage

Overview

Observers allow you to implement trigger-like behavior outside the original classes.
You can put them anywhere, for example app/models/contact_observer.rb:

class ContactObserver < Everett::Observer
  def after_create(contact)
    Rails.logger.info('New contact has been added!')
  end

  def after_destroy(contact)
    Rails.logger.info("Contact with an id of #{contact.id} has been destroyed!")
  end
end

This observer prints a log message when specific callbacks are triggered.

Just like Active Record Observer, the convention is to name observers after the class they observe.
If you need to change this, or want to use one observer for several classes, use observe:

class NotificationsObserver < Everett::Observer
  observe :comment, :like

  def after_create(record)
    # notifiy users of new comment or like
  end
end

Note that you must register observers first to activate them.
This can be done by adding the following line into config/initializers/everett.rb:

config.observers = :contact_observer, :notifications_observer

after_{create,update,destroy}_commit

Since after_create_commit, after_update_commit and after_destroy_commit were introduced in Rails 5, you can also use them in observers:

class CommentObserver < Everett::Observer
  def after_create_commit(comment)
    CommentMailer.notification(comment).deliver_now
  end
end

This observer sends an email after a record has been created.

Migration from rails-observers

Since Everett is highly compatible with Active Record Observer, you can easily migrate from rails-observers.
All you need to do is as follows:

1. Replace ActiveRecord::Observer with Everett::Observer

-class ContactObserver < ActiveRecord::Observer
+class ContactObserver < Everett::Observer

2. Register observers in config/initializers/everett.rb

# config/application.rb
-config.active_record.observers = :contact_observer, :notifications_observer

# config/initializers/everett.rb
+Everett.configure do |config|
+  config.observers = :contact_observer, :notifications_observer
+end

3. Check the test suite passes

If you find any bugs, please document them on the issues page.

Contributing

You should follow the steps below.

  1. Fork the repository
  2. Create a feature branch: git checkout -b add-new-feature
  3. Commit your changes: git commit -am 'Add new feature'
  4. Push the branch: git push origin add-new-feature
  5. Send us a pull request

License

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.