active_record-nested_error_indexer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7045057f2814c8ca1983b936f9b8927153428d93
4
+ data.tar.gz: f91057bb2261af005c3a732257edb655722baa56
5
+ SHA512:
6
+ metadata.gz: 987cc7d9b55caf934209f13c308dd62865dde8ed5d183c31cc915157e1478d2469c223125e3b607b94ada1e3f270ec886618c3ae7554e27753cc19a30907573c
7
+ data.tar.gz: 3345ce521fcded4208292ef8ba8011184968cea0565cecd034f1f471a3bf49972a6ea907b1ce2ba32059d238397efde0975aaded125dd317dc5eca353a664c7d
@@ -0,0 +1,13 @@
1
+ # http://editorconfig.org
2
+ root = true
3
+
4
+ [*]
5
+ indent_style = space
6
+ indent_size = 2
7
+ end_of_line = lf
8
+ charset = utf-8
9
+ trim_trailing_whitespace = true
10
+ insert_final_newline = true
11
+
12
+ [*.md]
13
+ trim_trailing_whitespace = false
@@ -0,0 +1,4 @@
1
+ pkg
2
+ doc
3
+ log
4
+ *.lock
@@ -0,0 +1,39 @@
1
+ # Contributing
2
+
3
+ ## Issues
4
+
5
+ If you want to request a feature or report a bug, please use the following template.
6
+
7
+ ```markdown
8
+ ## Description
9
+
10
+ [Add feature/bug description here]
11
+
12
+ ## How to reproduce
13
+
14
+ [Add steps on how to reproduce this issue]
15
+
16
+ ## What do you expect
17
+
18
+ [Describe what do you expect to happen]
19
+
20
+ ## What happened instead
21
+
22
+ [Describe the actual results]
23
+
24
+ ## Gem version:
25
+
26
+ Version: [Add gem version here]
27
+ ```
28
+
29
+ ## Writing code
30
+
31
+ Once you've made your great commits (include tests, please):
32
+
33
+ 1. [Fork](http://help.github.com/forking/) the [original repository](http://github.com/dhyegofernando/active_record-nested_error_indexer)
34
+ 2. Create a topic branch - `git checkout -b my_branch`
35
+ 3. Push to your branch - `git push origin my_branch`
36
+ 4. [Create an Issue](http://github.com/dhyegofernando/active_record-nested_error_indexer/issues) with a link to your branch
37
+ 5. That's it!
38
+
39
+ Please respect the code style using [editorconfig](http://editorconfig.org/) in your text editor.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_record-nested_error_indexer.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Dhyego Fernando
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ # ActiveRecord Nested Error Indexer
2
+ In Rails 5, we have the option `index_errors: true` to output the nested item index for errors like this:
3
+
4
+ ```ruby
5
+ class Order < ApplicationRecord
6
+ has_many :items, index_errors: true
7
+
8
+ accepts_nested_attributes_for :items
9
+ end
10
+
11
+ order = Order.create(items_attributes: [{ price: 1 }, { price: -1 }])
12
+ order.errors # #<ActiveModel::Errors... @messages={:*"items[1].price"*: ["must be greater than 0"]}>
13
+ ```
14
+
15
+ BUT IT DOESN'T WORK FOR RAILS 4, so... let's monkey patch it :)
16
+
17
+ ## Maintainer
18
+ [Dhyego Fernando](https://github.com/dhyegofernando)
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['./test/**/*_test.rb']
7
+ end
8
+
9
+ task default: :test
@@ -0,0 +1,25 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+
3
+ require './lib/active_record/nested_error_indexer/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'active_record-nested_error_indexer'
7
+ spec.version = ActiveRecord::NestedErrorIndexer::Version::STRING
8
+ spec.authors = ['Dhyego Fernando']
9
+ spec.email = ['dhyegofernando@gmail.com']
10
+
11
+ spec.summary = 'Monkey patch to index nested errors for ActiveRecord 4'
12
+ spec.description = spec.summary
13
+ spec.homepage = 'http://github.com/dhyegofernando/active_record-nested_error_indexer'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+
18
+ spec.add_dependency 'activerecord', '~> 4.0'
19
+
20
+ spec.add_development_dependency 'bundler'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'minitest'
23
+ spec.add_development_dependency 'minitest-utils'
24
+ spec.add_development_dependency 'pry-meta'
25
+ end
Binary file
@@ -0,0 +1,13 @@
1
+ require 'active_record'
2
+
3
+ module ActiveRecord
4
+ module NestedErrorIndexer
5
+ end
6
+ end
7
+
8
+ require 'active_record/nested_error_indexer/version'
9
+ require 'active_record/nested_error_indexer/associations/builder/has_many'
10
+ require 'active_record/nested_error_indexer/autosave_association'
11
+
12
+ ActiveRecord::Associations::Builder::HasMany.prepend ActiveRecord::NestedErrorIndexer::Associations::Builder::HasMany
13
+ ActiveRecord::Base.prepend ActiveRecord::NestedErrorIndexer::AutosaveAssociation
@@ -0,0 +1,13 @@
1
+ module ActiveRecord
2
+ module NestedErrorIndexer
3
+ module Associations
4
+ module Builder
5
+ module HasMany
6
+ def valid_options
7
+ super + [:index_errors]
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,51 @@
1
+ module ActiveRecord
2
+ module NestedErrorIndexer
3
+ module AutosaveAssociation
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ mattr_accessor :index_nested_attribute_errors, instance_writer: false
8
+ self.index_nested_attribute_errors = false
9
+ end
10
+
11
+ private
12
+
13
+ # Validate the associated records if <tt>:validate</tt> or
14
+ # <tt>:autosave</tt> is turned on for the association specified by
15
+ # +reflection+.
16
+ def validate_collection_association(reflection)
17
+ if association = association_instance_get(reflection.name)
18
+ if records = associated_records_to_validate_or_save(association, new_record?, reflection.options[:autosave])
19
+ records.each_with_index { |record, index| association_valid?(reflection, record, index) }
20
+ end
21
+ end
22
+ end
23
+
24
+ # Returns whether or not the association is valid and applies any errors to
25
+ # the parent, <tt>self</tt>, if it wasn't. Skips any <tt>:autosave</tt>
26
+ # enabled records if they're marked_for_destruction? or destroyed.
27
+ def association_valid?(reflection, record, index=nil)
28
+ return true if record.destroyed? || (reflection.options[:autosave] && record.marked_for_destruction?)
29
+
30
+ validation_context = self.validation_context unless [:create, :update].include?(self.validation_context)
31
+ unless valid = record.valid?(validation_context)
32
+ if reflection.options[:autosave]
33
+ record.errors.each do |attribute, message|
34
+ attribute =
35
+ if index.nil? || (!reflection.options[:index_errors] && !ActiveRecord::Base.index_nested_attribute_errors)
36
+ "#{reflection.name}.#{attribute}"
37
+ else
38
+ "#{reflection.name}[#{index}].#{attribute}"
39
+ end
40
+ errors[attribute] << message
41
+ errors[attribute].uniq!
42
+ end
43
+ else
44
+ errors.add(reflection.name)
45
+ end
46
+ end
47
+ valid
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,10 @@
1
+ module ActiveRecord
2
+ module NestedErrorIndexer
3
+ module Version
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ PATCH = 0
7
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record-nested_error_indexer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dhyego Fernando
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-utils
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-meta
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'
97
+ description: Monkey patch to index nested errors for ActiveRecord 4
98
+ email:
99
+ - dhyegofernando@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".editorconfig"
105
+ - ".gitignore"
106
+ - CONTRIBUTING.md
107
+ - Gemfile
108
+ - LICENSE
109
+ - README.md
110
+ - Rakefile
111
+ - active_record-nested_error_indexer.gemspec
112
+ - demo.gif
113
+ - lib/active_record/nested_error_indexer.rb
114
+ - lib/active_record/nested_error_indexer/associations/builder/has_many.rb
115
+ - lib/active_record/nested_error_indexer/autosave_association.rb
116
+ - lib/active_record/nested_error_indexer/version.rb
117
+ homepage: http://github.com/dhyegofernando/active_record-nested_error_indexer
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.4.8
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Monkey patch to index nested errors for ActiveRecord 4
141
+ test_files: []