nested_attributes_validator 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/README.md +5 -0
- data/lib/nested_attributes_validator.rb +1 -0
- data/lib/nested_attributes_validator/active_model/validations/nested_attributes_order_validator.rb +8 -10
- data/lib/nested_attributes_validator/active_model/validations/nested_attributes_uniqueness_validator.rb +10 -8
- data/lib/nested_attributes_validator/nested_attributes_validator_util.rb +18 -0
- data/lib/nested_attributes_validator/version.rb +1 -1
- data/nested_attributes_validator.gemspec +1 -0
- metadata +16 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36dcdf83db4cf99946d9358c90098a23f9f20b1a
|
4
|
+
data.tar.gz: c80b9157145643f6b3917673db1fd89f98320d84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4f0bc8d50fa61d0343c131c410070928f090b2d5957edc4b2cbb969ad4834cfd9ade168d5e009d9ce909c824cfad5490d939961ea24723a7876012871a099d6
|
7
|
+
data.tar.gz: 25fdc36ab808e44e431ae3dd54ab3a79835f13337b2c13b97872c7c7f03ee0689a6ec3c2250dd4a047dc4d01d7e7cc10f84b4bf15a80d77d4877567228e876c0
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
#
|
2
2
|
# NestedAttributesValidator
|
3
3
|
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/nested_attributes_validator.svg)](https://badge.fury.io/rb/nested_attributes_validator)
|
5
|
+
[![Build Status](https://travis-ci.org/Kta-M/nested_attributes_validator.svg?branch=master)](https://travis-ci.org/Kta-M/nested_attributes_validator)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/Kta-M/nested_attributes_validator/badges/gpa.svg)](https://codeclimate.com/github/Kta-M/nested_attributes_validator)
|
7
|
+
[![Test Coverage](https://codeclimate.com/github/Kta-M/nested_attributes_validator/badges/coverage.svg)](https://codeclimate.com/github/Kta-M/nested_attributes_validator/coverage)
|
8
|
+
|
4
9
|
Nested Attributes Validation Collection for Rails
|
5
10
|
|
6
11
|
## Installation
|
data/lib/nested_attributes_validator/active_model/validations/nested_attributes_order_validator.rb
CHANGED
@@ -1,22 +1,20 @@
|
|
1
1
|
class NestedAttributesOrderValidator < ActiveModel::EachValidator
|
2
|
+
include NestedAttributesValidatorUtil
|
2
3
|
|
3
|
-
def validate_each(record,
|
4
|
-
|
4
|
+
def validate_each(record, _attribute, values)
|
5
|
+
trg_fields = target_fields
|
5
6
|
|
6
|
-
|
7
|
-
if
|
8
|
-
|
9
|
-
end
|
10
|
-
if fields.size == 1
|
11
|
-
h.keys.each {|k| h[k] = h[k].first}
|
7
|
+
trg_values = target_values(trg_fields, values)
|
8
|
+
if trg_fields.size == 1
|
9
|
+
trg_values.keys.each {|k| trg_values[k] = trg_values[k].first}
|
12
10
|
end
|
13
11
|
|
14
|
-
|
12
|
+
trg_values.each_cons(2) do |(v1, f1), (v2, f2)|
|
15
13
|
condition = options[:condition] || lambda {|a, b| a < b}
|
16
14
|
is_valid = condition.call(f1, f2)
|
17
15
|
|
18
16
|
unless is_valid
|
19
|
-
|
17
|
+
trg_fields.each do |field|
|
20
18
|
# set error to the parent record
|
21
19
|
attribute_name = :"#{attributes.first}.#{options[:display_field] || field}"
|
22
20
|
record.errors[attribute_name] << I18n.t('errors.messages.nested_attributes_invalid_order')
|
@@ -1,18 +1,20 @@
|
|
1
1
|
class NestedAttributesUniquenessValidator < ActiveModel::EachValidator
|
2
|
+
include NestedAttributesValidatorUtil
|
2
3
|
|
3
|
-
def validate_each(record,
|
4
|
-
|
4
|
+
def validate_each(record, _attribute, values)
|
5
|
+
trg_fields = target_fields
|
5
6
|
|
6
7
|
# detect duplicated values
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
duplicated_values = target_values(trg_fields, values)
|
9
|
+
.group_by{|_k ,v| v}.values
|
10
|
+
.select{|a| a.size>1}
|
11
|
+
.flatten(1)
|
12
|
+
.to_h
|
13
|
+
.keys
|
12
14
|
|
13
15
|
# set errors
|
14
16
|
duplicated_values.each do |value|
|
15
|
-
|
17
|
+
trg_fields.each do |field|
|
16
18
|
# set error to the parent record
|
17
19
|
attribute_name = :"#{attributes.first}.#{options[:display_field] || field}"
|
18
20
|
record.errors[attribute_name] << I18n.t('errors.messages.nested_attributes_not_unique')
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module NestedAttributesValidatorUtil
|
2
|
+
|
3
|
+
def target_fields
|
4
|
+
([options[:fields]] || [:self]).flatten.map(&:to_s)
|
5
|
+
end
|
6
|
+
|
7
|
+
def target_values(fields, values)
|
8
|
+
trg = values.inject({}) do |ret, v|
|
9
|
+
ret[v] = fields.map{|f| v.send(f)}
|
10
|
+
ret
|
11
|
+
end
|
12
|
+
if options[:ignore_nil]
|
13
|
+
trg = trg.reject{|_k, v| v.all?(&:nil?)}
|
14
|
+
end
|
15
|
+
trg
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nested_attributes_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kta-M
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: codeclimate-test-reporter
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
description: Nested Attributes Validation Collection for Rails
|
84
98
|
email:
|
85
99
|
- mohri1219@gmail.com
|
@@ -100,6 +114,7 @@ files:
|
|
100
114
|
- lib/nested_attributes_validator.rb
|
101
115
|
- lib/nested_attributes_validator/active_model/validations/nested_attributes_order_validator.rb
|
102
116
|
- lib/nested_attributes_validator/active_model/validations/nested_attributes_uniqueness_validator.rb
|
117
|
+
- lib/nested_attributes_validator/nested_attributes_validator_util.rb
|
103
118
|
- lib/nested_attributes_validator/version.rb
|
104
119
|
- nested_attributes_validator.gemspec
|
105
120
|
homepage: https://github.com/Kta-M/nested_form_validator
|