Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

replace_with_destroy

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

replace_with_destroy

  • 0.1.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Replace with Destroy

Based on Rails guide Automatic deletion of join models is direct, no destroy callbacks are triggered., which means, Rails will not run destroy callbacks for has_many through join model.

This gem allow you running the destroy callbacks in the join model.

This could be useful if you are using paranoia and you want change it to un-active instead delete the join model.

Installation

Add this line to your application's Gemfile:

gem 'replace_with_destroy'

And then execute:

$ bundle

Or install it yourself as:

$ gem install replace_with_destroy

Usage

replace_with_destroy option.

Pass the option replace_with_destroy: true to the has_many method.

class User < ApplicationRecord
  has_many :bookings
  has_many :rooms, through: :bookings, replace_with_destroy: true
end

class Booking < ApplicationRecord
  belongs_to :user
  belongs_to :room
  
  after_destroy :my_custom_method

  def my_custom_method
    # do whatever you want here ...
  end
end

class Room < ApplicationRecord
  ...
end

Then, given the User user with rooms [1,2,3] you can change his rooms and this automatically call Booking destroy callbacks.

# given a User with rooms
> user.room_ids
[1, 2, 3]

# you can change his rooms
user.room_ids = [3, 4, 5]
# this add the rooms 4 and 5 and remove the rooms 1 and 2
# my_custom_method will be executed for rooms 1 and 2 when being removed

global replace_with_destroy option.

You can add this behavior for all has_many methods without need to pass it as option.

To do that, create an initializer, for example config/initializers/replace_with_destroy.rb, and put the following code

ReplaceWithDestroy::ALL = true

License

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

FAQs

Package last updated on 29 Mar 2019

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc