Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
soft delete Rails plugin.
Add this line to your application's Gemfile:
gem "soft_deleter"
And then execute:
$ bundle
Or install it yourself as:
$ gem install soft_deleter
Soft delete model User.
bundle exec rails g soft_deleter user
It creates migration file to create user with attributes which is needed to introduce soft delete.
Add some attributes to migration file, like name, email, age, ...etc.
And excute bundle exec rails db:migrate
. That's all.
Or if you already have User model, and you want introduce soft delete in it,
create migration file and add lines like bellow
class AddSoftDeleterAttributesToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :deleter_type, :string
add_column :users, :deleter_id, :integer
add_column :users, :deleted_at, :timestamp
end
end
and excute bundle exec rails db:migrate
Here, deleter_type
and deleter_id
, these are the infomations who soft delete.
Like current_admin
, when admin which is "Admin" class does soft delete user record, admin's class name and id can be recorded.
And add line to model
class User < ApplicationRecord
include SoftDeleter
end
This line is added automatically if you use rails g soft_deleter user
command to make user model.
When you load users whitout soft deleted records, you need to scope like bellow.
users = User.enabled.all
If you don't use enabled scope, you will load users in all records including soft deleted.
Otherwise, if you need to load records with soft deleted, excute like bellow.
deleted_users = User.deleted.all
user = User.enabled.first
user.soft_delete # soft delete
user.soft_delete! # soft delete or raise if fail to soft delete
user.restore # restore soft deleted user
If your app have some models other than user, like Admin
model,
and you need to record to that Admin user did soft delete.
Then,
user = User.enabled.first
admin = Admin.enabled.first # soft deleted by admin user
user.soft_delete(admin) # soft delete and set admin to deleter
user.soft_delete!(admin) # raise if fail to soft delete
user.deleter # => <Admin:0x00007f37f96a0c88
user.deleter_type # => Admin(id: integer, ...
user.deleter_id # => "admin.id" if deleter is not set, "user.id"
user.soft_deleted? # => true
user.alive? # => false
If associations some models, User, Book, Section.
# User model
class User < ApplicationRecord
include SoftDeleter
has_many :books, dependent: :destroy
end
# Book model
class Book < ApplicationRecord
include SoftDeleter
belongs_to :user
has_many :sections, dependent: :destroy
end
# Section model
class Section < ApplicationRecord
include SoftDeleter
belongs_to :book
end
So, if you excute user.soft_delete
, then associations books, and sections are soft deleted.
And excute user.restore
, then associations books, and sections are restored.
It works with dependent destroy descriptions. If not, it doesn't work.
Contribution directions go here.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that soft_deleter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.