README.md in store_model-0.1.1 vs README.md in store_model-0.1.2
- old
+ new
@@ -11,28 +11,26 @@
For instance, imagine that you have a model `Product` with a `jsonb` column called `configuration`. Your usual workflow probably looks like:
```ruby
product = Product.find(params[:id])
-if product.configuration["model"] = "spaceship"
+if product.configuration["model"] == "spaceship"
product.configuration["color"] = "red"
end
product.save
```
This approach works fine when you don't have a lot of keys with logic around them and just read the data. However, when you start working with that data more intensively (for instance, adding some validations around it) - you may find the code a bit verbose and error-prone. With this gem, the snipped above could be rewritten this way:
```ruby
product = Product.find(params[:id])
-if product.configuration.model = "spaceship"
+if product.configuration.model == "spaceship"
product.configuration.color = "red"
end
product.save
```
-> **Note**: if you want to work with JSON fields as an attributes, defined on the ActiveRecord model (not in the separate class) - consider using [store_attribute](https://github.com/palkan/store_attribute) or [jsonb_accessor](https://github.com/devmynd/jsonb_accessor).
-
## Installation
Add this line to your application's Gemfile:
```ruby
@@ -60,11 +58,11 @@
attribute :model, :string
attribute :color, :string
end
```
-Attributes shoould be defined using [Rails Attributes API](https://api.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.html). There is a number of types available out of the box, and you can always extend the type system with your own ones.
+Attributes should be defined using [Rails Attributes API](https://api.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.html). There is a number of types available out of the box, and you can always extend the type system with your own ones.
Register the field in the ActiveRecord model class:
```ruby
class Product < ApplicationRecord
@@ -169,9 +167,25 @@
attribute :configuration, Configuration.to_type
validates :configuration, store_model: { merge_errors: FhtagnErrorStrategy.new }
end
```
+
+**Note**: `:store_model` validator does not allow nils by default, if you want to change this behavior - configure the validation with `allow_nil: true`:
+
+```ruby
+class Product < ApplicationRecord
+ attribute :configuration, Configuration.to_type
+
+ validates :configuration, store_model: true, allow_nil: true
+end
+```
+
+## 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`
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).