Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92b22b4
commit 2c4090c
Showing
7 changed files
with
244 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## Alternatives | ||
|
||
- [store_attribute](https://github.com/palkan/store_attribute)–work with JSON fields as an attributes, defined on the ActiveRecord model (not in the separate class) | ||
- [jsonb_accessor](https://github.com/devmynd/jsonb_accessor)–same thing, but with built-in queries | ||
- [attr_json](https://github.com/jrochkind/attr_json)–works like previous one, but using `ActiveModel::Type` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
## Array of stored models | ||
|
||
Should you store an array of stored models, use `#to_array_type` method: | ||
|
||
```ruby | ||
class Product < ApplicationRecord | ||
attribute :configurations, Configuration.to_array_type | ||
end | ||
``` | ||
|
||
After that, your attribute will return an array of `Configuration` instances. | ||
|
||
> **Heads up!** The attribute is not the same as the association, in this case–it's just a hash. `assign_attributes` (and similar) is going to _override_ the whole hash, not merge it with a previous value. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
## Enums | ||
|
||
If you worked with [Rails Enums](https://api.rubyonrails.org/v5.2.3/classes/ActiveRecord/Enum.html) or [enumerize](https://github.com/brainspec/enumerize)–built-in enums should look familiar to you: | ||
|
||
```ruby | ||
class Configuration | ||
include StoreModel::Model | ||
|
||
enum :status, %i[active archived], default: :active | ||
end | ||
|
||
config = Configuration.new | ||
config.status => # active | ||
|
||
config.status = :archived | ||
config.archived? # => true | ||
config.active? # => false | ||
config.status_value # => 0 | ||
|
||
config.status_values # => { :active => 0, :archived => 1 } | ||
``` | ||
|
||
Under the hood, values are stored as integers, according to the index of the element in the array: | ||
|
||
```ruby | ||
Configuration.new.inspect # => #<Configuration status: 0> | ||
``` | ||
|
||
You can specify values explicitly using the `:in` kwarg: | ||
|
||
```ruby | ||
class Review | ||
include StoreModel::Model | ||
|
||
enum :rating, in: { excellent: 100, okay: 50, bad: 25, awful: 10 }, default: :okay | ||
end | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
```ruby | ||
gem 'store_model' | ||
``` | ||
|
||
And then execute: | ||
```bash | ||
$ bundle | ||
``` | ||
|
||
Or install it yourself as: | ||
```bash | ||
$ gem install store_model | ||
``` |
Oops, something went wrong.