Listable collections
Makes collections accessible from a string list in rails.
Why
We did this gem to:
- Easily manage collections from input text field.
- Track changes like dirty module in activerecord.
- Have callbacks like has_many associations.
- Automatically handle join models creation/deletion.
Install
Put this line in your Gemfile:
gem 'listable_collections'
Then bundle:
$ bundle
Usage
Associations
If you want to listize a has_many association:
class Product < ApplicationRecord
has_many :tagizations
has_many :tags, through: :tagizations
listize :tags, by: :name
end
Associated records will be synced and chances will be tracked using the following helpers:
product.tags.map(&:name) => ['New']
product.tag_list => 'New'
product.tag_list = 'Natural'
product.tagizations.reject(&:marked_for_destruction?).map(&:tag).map(&:name) => ['Natural']
product.tagizations.select(&:marked_for_destruction?).map(&:tag).map(&:name) => ['New']
product.added_tags_to_list => ['Natural']
product.removed_tags_from_list => ['New']
NOTE: All associations are assumed to be has many through to be able to handle creations/deletions.
Attributes
If you want to list an array attribute:
class Product < ActiveRecord::Base
serialize :sizes, Array
listize :sizes
end
The attribute will be synced and chances will be tracked using the following helpers:
product.sizes => ['64GB']
product.size_list => '64GB'
product.size_list = '32GB,128GB'
product.sizes => ['32GB','128GB']
product.added_sizes_to_list => ['128GB']
product.removed_sizes_from_list => ['64GB']
Contributing
Any issue, pull request, comment of any kind is more than welcome!
We will mainly ensure compatibility to Rails, AWS, PostgreSQL, Redis, Elasticsearch and FreeBSD.
Credits
This gem is maintained and funded by museways.
License
It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.