GithubHelp home page GithubHelp logo

pragmaticed / active_touch Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kpheasey/active_touch

0.0 2.0 0.0 42 KB

A more robust touch for ActiveRecord associations

License: MIT License

Ruby 99.11% Shell 0.89%

active_touch's Introduction

ActiveTouch

A more robust touch for ActiveRecord associations.

  • Touch specific associations when specific attributes change
  • Call an optional method on those touched records
  • Perform the touch synchronously or asynchronously

Installation

Add the gem to your Gemfile:

gem 'active_touch', '~> 4.0'

Then run the installer:

rails g active_touch:install

Usage

Basic touch that runs after_commit. This will update the association's updated_at.

class Model < ActiveRecord::Base
  has_many :relations

  touch :relations
end

NOTE: It doesn't matter what type of association is given. The association can even reference an instance method that returns an ActiveRecord collection or record.

Attribute specific touches

To only call the touch when specific attributes are changed, supply an array of attributes with :watch. The following example will only touch :relations when :name or :code changes.

class Model < ActiveRecord::Base
  has_many :relations

  touch :relations, watch: [:name, :code]
end

After Touch Callback

To call a method on the touched records, use :after_touch. The following example will call :do_something on the associated records after a tocuh.

class Model < ActiveRecord::Base
  has_many :relations

  touch :relations, after_touch: :do_something
end

NOTE: The after_touch method must be an instance method defined on the associated Class.

Asynchronous touch

The touch can also be queued and run in the background using ActiveJob by setting the :async flag. The following example will run a touch in the background.

class Model < ActiveRecord::Base
  has_many :relations

  touch :relations, async: true
end

NOTE: The default is async: false

Options

There are a few options that you can change by updating config/initializers/active_touch.rb.

  • async: Default to asynchronous touches
  • ignored_attributes: When no watch argument is supplied, all attribute changes can trigger a touch. Define a default list of ignored attributes here. Default is [:updated_a]t.
  • queue: Specify which queue to put asynchronous jobs in.
  • timestamp_attribute: The timestamp attribute that is updated by a touch, default is :updated_at. You can set this to nil if you don't want to update any timestamp.

active_touch's People

Contributors

kpheasey avatar pragmaticed avatar

Watchers

 avatar  avatar

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.