Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/onyxblade/smart_collection
rails plugin for shopify style smart collection
https://github.com/onyxblade/smart_collection
activerecord rails
Last synced: 15 days ago
JSON representation
rails plugin for shopify style smart collection
- Host: GitHub
- URL: https://github.com/onyxblade/smart_collection
- Owner: onyxblade
- Created: 2017-10-30T05:33:50.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-15T23:10:30.000Z (almost 2 years ago)
- Last Synced: 2024-12-21T13:07:00.723Z (about 1 month ago)
- Topics: activerecord, rails
- Language: Ruby
- Homepage:
- Size: 55.7 KB
- Stars: 21
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# smart_collection
[![Gem Version](https://badge.fury.io/rb/smart_collection.svg)](https://badge.fury.io/rb/smart_collection) [![Build Status](https://travis-ci.com/onyxblade/smart_collection.svg?branch=master)](https://travis-ci.com/onyxblade/smart_collection)
Smart collection or automated collection is [a concept putted forward by Shopify](https://help.shopify.com/en/manual/products/collections/automated-collections). Those collections automatically include or exclude items according to conditions defined by users.
This plugin allows you to define a collection as an union of multiple scopes; meanwhile preloading and inverse query are possible.
Install
------Add `gem 'smart_collection'` to your Gemfile and `bundle`.
Usage
------```ruby
# create collection table
class CreateCollections < ActiveRecord::Migration[5.0]
def change
create_table :collections do |t|
t.datetime :cache_expires_at
t.timestamps
end
end
end# create cache table
class CreateSmartCollectionCachedItems < ActiveRecord::Migration[5.0]
def change
create_table :smart_collection_cached_items do |t|
t.integer :collection_id
t.integer :item_id
end
end
end# define model
class Collection < ActiveRecord::Base
include SmartCollection::Mixin.new(
item_association: :products,
item_class_name: 'Product',
# scopes option can be a method name / proc that returns an array of scopes
scopes: -> (collection) {
[Product.all]
},
# the table :smart_collection_cached_items is used when cache_table_name setted :default
cache_table_name: :default,
# cache_expires_in can be a duration or a method name / proc that returns a duration
cache_expires_in: 1.hour,
# defines an inverse association on the item class
inverse_association: :collections
)
end
``````ruby
# create a collection:
collection = Collection.create
# fetch products by defined scope
collection.products # => all products will be returned
collection.products.where(in_stock: true).order(id: :desc) #=> association returns a scope
``````ruby
# use includes or preload to avoid n+1 queries
Collection.where(id: [1, 2]).preload(products: :properties)
```Test
------
```shell
bundle exec rake
```License
------[The MIT License](https://opensource.org/licenses/MIT)