store_model 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +19 -5
- data/lib/active_model/validations/store_model_validator.rb +8 -13
- data/lib/store_model/combine_errors_strategies.rb +3 -3
- data/lib/store_model/combine_errors_strategies/mark_invalid_error_strategy.rb +2 -2
- data/lib/store_model/combine_errors_strategies/merge_error_strategy.rb +1 -1
- data/lib/store_model/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5f9b992038ac6e32409aeb104f8a6de62c848550a7b79160ce83f777547f0ea0
|
|
4
|
+
data.tar.gz: 8a2f3e43dbd4dea884581a5ff1caf017eb03e71c17fee015dd83cb95244dc267
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3675e1f7c3b1aea633b3931bde2d4c51e3841633b5712c8b73c7227d07d49edbb7c9ec8e029c5a1b98cf5a2ac6afcc78a2f2a3e2eb39966d4a9f5d9ccbd71e83
|
|
7
|
+
data.tar.gz: 619425140ebbf8b9cb4fa6d3171977d19aab43b4981647a7c6b4bc28c65e79b0026083459cdec9e6d3761839225e4079ad4496ac361ba0080247d472cf0fd215
|
data/README.md
CHANGED
|
@@ -13,7 +13,7 @@ For instance, imagine that you have a model `Product` with a `jsonb` column call
|
|
|
13
13
|
|
|
14
14
|
```ruby
|
|
15
15
|
product = Product.find(params[:id])
|
|
16
|
-
if product.configuration["model"]
|
|
16
|
+
if product.configuration["model"] == "spaceship"
|
|
17
17
|
product.configuration["color"] = "red"
|
|
18
18
|
end
|
|
19
19
|
product.save
|
|
@@ -23,14 +23,12 @@ This approach works fine when you don't have a lot of keys with logic around the
|
|
|
23
23
|
|
|
24
24
|
```ruby
|
|
25
25
|
product = Product.find(params[:id])
|
|
26
|
-
if product.configuration.model
|
|
26
|
+
if product.configuration.model == "spaceship"
|
|
27
27
|
product.configuration.color = "red"
|
|
28
28
|
end
|
|
29
29
|
product.save
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
> **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).
|
|
33
|
-
|
|
34
32
|
## Installation
|
|
35
33
|
|
|
36
34
|
Add this line to your application's Gemfile:
|
|
@@ -62,7 +60,7 @@ class Configuration
|
|
|
62
60
|
end
|
|
63
61
|
```
|
|
64
62
|
|
|
65
|
-
Attributes
|
|
63
|
+
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.
|
|
66
64
|
|
|
67
65
|
Register the field in the ActiveRecord model class:
|
|
68
66
|
|
|
@@ -172,6 +170,22 @@ class Product < ApplicationRecord
|
|
|
172
170
|
end
|
|
173
171
|
```
|
|
174
172
|
|
|
173
|
+
**Note**: `:store_model` validator does not allow nils by default, if you want to change this behavior - configure the validation with `allow_nil: true`:
|
|
174
|
+
|
|
175
|
+
```ruby
|
|
176
|
+
class Product < ApplicationRecord
|
|
177
|
+
attribute :configuration, Configuration.to_type
|
|
178
|
+
|
|
179
|
+
validates :configuration, store_model: true, allow_nil: true
|
|
180
|
+
end
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Alternatives
|
|
184
|
+
|
|
185
|
+
- [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)
|
|
186
|
+
- [jsonb_accessor](https://github.com/devmynd/jsonb_accessor) - same thing, but with built-in queries
|
|
187
|
+
- [attr_json](https://github.com/jrochkind/attr_json) - works like previous one, but using `ActiveModel::Type`
|
|
188
|
+
|
|
175
189
|
## License
|
|
176
190
|
|
|
177
191
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
@@ -5,24 +5,19 @@ require "store_model/combine_errors_strategies"
|
|
|
5
5
|
|
|
6
6
|
module ActiveModel
|
|
7
7
|
module Validations
|
|
8
|
-
class StoreModelValidator < ActiveModel::
|
|
9
|
-
def
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
class StoreModelValidator < ActiveModel::EachValidator
|
|
9
|
+
def validate_each(record, attribute, value)
|
|
10
|
+
if value.nil?
|
|
11
|
+
record.errors.add(attribute, :blank)
|
|
12
|
+
elsif value.invalid?
|
|
13
|
+
strategy.call(attribute, record.errors, value.errors)
|
|
13
14
|
end
|
|
14
15
|
end
|
|
15
16
|
|
|
16
17
|
private
|
|
17
18
|
|
|
18
|
-
def
|
|
19
|
-
|
|
20
|
-
store_model_errors = record.send(attribute).errors
|
|
21
|
-
|
|
22
|
-
base_errors.delete(attribute)
|
|
23
|
-
|
|
24
|
-
strategy = StoreModel::CombileErrorsStrategies.configure(options)
|
|
25
|
-
strategy.call(attribute, base_errors, store_model_errors)
|
|
19
|
+
def strategy
|
|
20
|
+
StoreModel::CombineErrorsStrategies.configure(options)
|
|
26
21
|
end
|
|
27
22
|
end
|
|
28
23
|
end
|
|
@@ -4,7 +4,7 @@ require "store_model/combine_errors_strategies/mark_invalid_error_strategy"
|
|
|
4
4
|
require "store_model/combine_errors_strategies/merge_error_strategy"
|
|
5
5
|
|
|
6
6
|
module StoreModel
|
|
7
|
-
module
|
|
7
|
+
module CombineErrorsStrategies
|
|
8
8
|
module_function
|
|
9
9
|
|
|
10
10
|
# Finds a strategy based on options and global config
|
|
@@ -14,9 +14,9 @@ module StoreModel
|
|
|
14
14
|
if configured_strategy.respond_to?(:call)
|
|
15
15
|
configured_strategy
|
|
16
16
|
elsif configured_strategy == true
|
|
17
|
-
StoreModel::
|
|
17
|
+
StoreModel::CombineErrorsStrategies::MergeErrorStrategy.new
|
|
18
18
|
elsif configured_strategy.nil?
|
|
19
|
-
StoreModel::
|
|
19
|
+
StoreModel::CombineErrorsStrategies::MarkInvalidErrorStrategy.new
|
|
20
20
|
else
|
|
21
21
|
const_get(configured_strategy.to_s.camelize).new
|
|
22
22
|
end
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module StoreModel
|
|
4
|
-
module
|
|
4
|
+
module CombineErrorsStrategies
|
|
5
5
|
class MarkInvalidErrorStrategy
|
|
6
6
|
def call(attribute, base_errors, _store_model_errors)
|
|
7
|
-
base_errors.add(attribute,
|
|
7
|
+
base_errors.add(attribute, :invalid)
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
end
|
data/lib/store_model/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: store_model
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- DmitryTsepelev
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2019-
|
|
11
|
+
date: 2019-03-14 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|
|
@@ -118,8 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
118
118
|
- !ruby/object:Gem::Version
|
|
119
119
|
version: '0'
|
|
120
120
|
requirements: []
|
|
121
|
-
|
|
122
|
-
rubygems_version: 2.7.6
|
|
121
|
+
rubygems_version: 3.0.3
|
|
123
122
|
signing_key:
|
|
124
123
|
specification_version: 4
|
|
125
124
|
summary: Gem for working with JSON-backed attributes as ActiveRecord models
|