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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e9af2b38072f6fd3be516a9d2c4486f271b0ceba
4
- data.tar.gz: 2b9f87c8963b55e7b5478f8283787299d3d17b5e
3
+ metadata.gz: 36dcdf83db4cf99946d9358c90098a23f9f20b1a
4
+ data.tar.gz: c80b9157145643f6b3917673db1fd89f98320d84
5
5
  SHA512:
6
- metadata.gz: 7a8015ef770baaf7a74681d7d18ed01aff60a8f47ab4da9decc62de6ac41ef9cdb3da40f5b2f506970bb7520877a308c8af1c583bb5da41aaddc0d930e8fb2fb
7
- data.tar.gz: 2c5119f357c048cace35b014a702fa812040fb2ac6e54931b0d233143be3d9b13570055d1791898f3c053dd6f8c6959a99744f70603317e3e64c8fb2d3b31844
6
+ metadata.gz: b4f0bc8d50fa61d0343c131c410070928f090b2d5957edc4b2cbb969ad4834cfd9ade168d5e009d9ce909c824cfad5490d939961ea24723a7876012871a099d6
7
+ data.tar.gz: 25fdc36ab808e44e431ae3dd54ab3a79835f13337b2c13b97872c7c7f03ee0689a6ec3c2250dd4a047dc4d01d7e7cc10f84b4bf15a80d77d4877567228e876c0
data/.travis.yml CHANGED
@@ -3,3 +3,4 @@ language: ruby
3
3
  rvm:
4
4
  - 2.3.1
5
5
  before_install: gem install bundler -v 1.12.5
6
+ script: CODECLIMATE_REPO_TOKEN=8e0b1905147fe1ecadd30ce0811b7d8d67a1347d112baf2b17bca95b8c9056f8 bundle exec rake
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
@@ -1,5 +1,6 @@
1
1
  require 'active_model'
2
2
  require "nested_attributes_validator/version"
3
+ require "nested_attributes_validator/nested_attributes_validator_util"
3
4
 
4
5
  %w(
5
6
  uniqueness order
@@ -1,22 +1,20 @@
1
1
  class NestedAttributesOrderValidator < ActiveModel::EachValidator
2
+ include NestedAttributesValidatorUtil
2
3
 
3
- def validate_each(record, attribute, values)
4
- fields = ([options[:fields]] || [:self]).flatten.map(&:to_s)
4
+ def validate_each(record, _attribute, values)
5
+ trg_fields = target_fields
5
6
 
6
- h = values.inject({}){|ret, v| ret[v] = fields.map{|f| v.send(f)}; ret}
7
- if options[:ignore_nil]
8
- h = h.reject{|k, v| v.all?(&:nil?)}
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
- h.each_cons(2) do |(v1, f1), (v2, f2)|
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
- fields.each do |field|
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, attribute, values)
4
- fields = ([options[:fields]] || [:self]).flatten.map(&:to_s)
4
+ def validate_each(record, _attribute, values)
5
+ trg_fields = target_fields
5
6
 
6
7
  # detect duplicated values
7
- h = values.inject({}){|ret, v| ret[v] = fields.map{|f| v.send(f)}; ret}
8
- if options[:ignore_nil]
9
- h = h.reject{|k, v| v.all?(&:nil?)}
10
- end
11
- duplicated_values = h.group_by{|k ,v| v}.values.select{|a| a.size>1}.flatten(1).to_h.keys
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
- fields.each do |field|
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
@@ -1,3 +1,3 @@
1
1
  module NestedAttributesValidator
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
25
  spec.add_development_dependency "rspec", "~> 3.0"
26
26
  spec.add_development_dependency "pry-rails"
27
+ spec.add_development_dependency "codeclimate-test-reporter"
27
28
  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.0
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