๐Ÿงฉ ConcernsOnRails

๐Ÿ‡ป๐Ÿ‡ณ Note: Hoร ng Sa and Trฦฐแปng Sa belong to Viแป‡t Nam.

A simple collection of reusable Rails concerns to keep your models clean and DRY.

โœจ Features

  • โœ… Sluggable: Generate friendly slugs from a specified field
  • ๐Ÿ”ข Sortable: Sort records based on a field using acts_as_list, with flexible sorting field and direction
  • ๐Ÿ“ค Publishable: Easily manage published/unpublished records using a simple published_at field
  • โŒ SoftDeletable: Soft delete records using a configurable timestamp field (e.g., deleted_at) with automatic scoping

๐Ÿ“ฆ Installation

Add this line to your application's Gemfile:

gem 'concerns_on_rails', github: 'VSN2015/concerns_on_rails'

Then execute:

bundle install

๐Ÿš€ Usage

1. ๐Ÿ“ Sluggable

Add slugs based on a specified attribute.

class Post < ApplicationRecord
  include ConcernsOnRails::Sluggable

  sluggable_by :title
end

post = Post.create!(title: "Hello World")
post.slug # => "hello-world"

If the slug source is changed, the slug will auto-update.


2. ๐Ÿ”ข Sortable

Use for models that need ordering.

class Task < ApplicationRecord
  include ConcernsOnRails::Sortable

  sortable_by :position
end

Task.create!(name: "B")
Task.create!(name: "A")
Task.first.name # => "B" (sorted by position ASC)

You can customize the sort field and direction:

class PriorityTask < ApplicationRecord
  include ConcernsOnRails::Sortable

  sortable_by priority: :desc
end

Additional features:

  • ๐Ÿ“Œ Automatically sets acts_as_list on the configured column
  • ๐Ÿ“‹ Adds default sorting scope to your model
  • โ†•๏ธ Supports custom direction: :asc or :desc
  • ๐Ÿ” Validates that the sortable field exists in the table schema
  • ๐Ÿง  Compatible with scopes and ActiveRecord queries
  • ๐Ÿ”„ Can be reconfigured dynamically within the model using sortable_by

3. ๐Ÿ“ค Publishable

Manage published/unpublished records using a published_at field.

class Article < ApplicationRecord
  include ConcernsOnRails::Publishable
end

Article.published   # => returns only published articles
Article.unpublished # => returns only unpublished articles

article = Article.create!(title: "Draft")
article.published? # => false

article.publish!
article.published? # => true

article.unpublish!
article.published? # => false

Additional features:

  • โœ… published? returns true if published_at is present and in the past
  • ๐Ÿ•’ publish! sets published_at to current time
  • ๐Ÿšซ unpublish! sets published_at to nil
  • ๐Ÿ”Ž Add scopes: .published, .unpublished, and a default scope (optional)
  • ๐Ÿ“ฐ Ideal for blog posts, articles, or any content that toggles visibility
  • ๐Ÿงฉ Lightweight and non-invasive
  • ๐Ÿงช Easy to test and override in custom implementations

4. โŒ SoftDeletable

Soft delete records using a timestamp field (default: deleted_at).

class User < ApplicationRecord
  include ConcernsOnRails::SoftDeletable

  soft_deletable_by :deleted_at
end

user = User.create!(name: "Alice")
user.soft_delete!
user.deleted? # => true

User.without_deleted   # => returns only active users
User.soft_deleted      # => returns soft-deleted users
User.all               # => returns only non-deleted by default (default_scope applied)

user.restore!
user.deleted? # => false

Additional features:

  • Default field is deleted_at, can be configured
  • Automatically applies default_scope to hide soft-deleted records
  • Scopes: without_deleted, soft_deleted, active
  • Methods: soft_delete!, restore!, deleted?, really_delete!
  • Callbacks: before_soft_delete, after_soft_delete, before_restore, after_restore
  • Touch support when soft deleting or restoring (can be turned off)
  • Aliases for deleted?: soft_deleted?, is_soft_deleted?

๐Ÿ› ๏ธ Development

To build the gem:

gem build concerns_on_rails.gemspec

To install locally:

gem install ./concerns_on_rails-1.0.0.gem

๐Ÿค Contributing

Bug reports and pull requests are welcome!


๐Ÿ“„ License

This project is licensed under the MIT License.


๐Ÿ‡ป๐Ÿ‡ณ Hoร ng Sa and Trฦฐแปng Sa belong to Viแป‡t Nam.


๐Ÿ”— Source Code

The source code is available on GitHub:

๐Ÿ‘‰ https://github.com/VSN2015/concerns_on_rails

Feel free to star โญ๏ธ, fork ๐Ÿด, or contribute with issues and PRs.