fields-serializer 0.7.0 → 0.8.0
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7562bc90c01ce91c788ed85ba77cb9ea02d88d55
|
4
|
+
data.tar.gz: 725b4a8754a0f6f4af0231faa0624083dc0ce3b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72274afb9d88d5d6065ceb30e57394dbec24d92dc63819177b1a44ba1bdcbad3196c6f94567358c2cd6ff15b93d7816f10a67ef02ada440995de1f76ae1d7785
|
7
|
+
data.tar.gz: 257b6dfc89ff1f87258825ebcef67cb41e9176ed9fc294c64ce954bc9e4c275990af0f4e70b19f451295c3784151e27f621c0e684cfb0525304081fdde008a70
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Fields
|
2
|
+
module Serializer
|
3
|
+
module ActiveRecord
|
4
|
+
|
5
|
+
module Errors
|
6
|
+
# Return a hash with errors of the model merged with errors of the given associated models.
|
7
|
+
#
|
8
|
+
# {
|
9
|
+
# name: ["can't be blank"],
|
10
|
+
# age: ["must be greater than 18"],
|
11
|
+
# cars: {
|
12
|
+
# "xxx-xxxxxxxx-xxx-xxxxxx" => {
|
13
|
+
# make: ["can't be blank"],
|
14
|
+
# type: ["can't be blank"]
|
15
|
+
# },
|
16
|
+
# "0" => {
|
17
|
+
# make: ["can't be blank"],
|
18
|
+
# year: ["must be greater than 1800"]
|
19
|
+
# }
|
20
|
+
# "1" => {
|
21
|
+
# year: ["must be greater than 1800"]
|
22
|
+
# }
|
23
|
+
# }
|
24
|
+
# }
|
25
|
+
#
|
26
|
+
# where "xxx-xxxxxxxx-xxx-xxxxxx" is the id of an associated model and
|
27
|
+
# an incremental integer id is given to those associated models with empty id.
|
28
|
+
# Similar to ActiveRecord nested attributes notation.
|
29
|
+
def deep_errors(*association_keys)
|
30
|
+
association_keys.inject(errors.to_h) do |error_tree, association_key|
|
31
|
+
associate = send(association_key)
|
32
|
+
error_tree.merge!(association_key => __associate_errors(associate))
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# For single asssociated instance (has_one association):
|
39
|
+
# { name: ["can't be blank"], age: ["can't be less than 18"] }
|
40
|
+
#
|
41
|
+
# For multiple asssociated instances (has_many association):
|
42
|
+
# { "xxx-xxxxxxxx-xxx-xxxxxx" => { name: ["can't be blank"], age: ["can't be less than 18"] },
|
43
|
+
# "0" => { name: ["can't be blank"], age: ["can't be less than 18"] },
|
44
|
+
def __associate_errors(associate)
|
45
|
+
if associate.is_a?(Array)
|
46
|
+
associate.map.with_index { |object, i| [object.id || i, object.errors.to_h] }.to_h
|
47
|
+
else
|
48
|
+
associate.errors.to_h
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -1,13 +1,14 @@
|
|
1
1
|
require 'active_record'
|
2
2
|
require "active_model_serializers"
|
3
|
+
require_relative 'active_record/errors'
|
3
4
|
|
4
5
|
module Fields
|
5
6
|
module Serializer
|
6
7
|
module ActiveRecord
|
7
8
|
extend ActiveSupport::Concern
|
9
|
+
include Errors
|
8
10
|
|
9
11
|
class_methods do
|
10
|
-
|
11
12
|
# If key is an association of a given model class
|
12
13
|
def association?(key)
|
13
14
|
reflections.keys.include?(key.to_s)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fields-serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stuart Chinery
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: exe
|
12
12
|
cert_chain: []
|
13
|
-
date: 2018-
|
13
|
+
date: 2018-03-20 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -145,6 +145,7 @@ files:
|
|
145
145
|
- lib/fields/serializer.rb
|
146
146
|
- lib/fields/serializer/action_controller.rb
|
147
147
|
- lib/fields/serializer/active_record.rb
|
148
|
+
- lib/fields/serializer/active_record/errors.rb
|
148
149
|
- lib/fields/serializer/fields_tree.rb
|
149
150
|
- lib/fields/serializer/version.rb
|
150
151
|
homepage: https://github.com/ltello/fields-serializer
|
@@ -167,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
168
|
version: '0'
|
168
169
|
requirements: []
|
169
170
|
rubyforge_project:
|
170
|
-
rubygems_version: 2.6.
|
171
|
+
rubygems_version: 2.6.14
|
171
172
|
signing_key:
|
172
173
|
specification_version: 4
|
173
174
|
summary: Extensions to ActiveRecord and ActionController to serialize a subset of
|