collection_errors 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +70 -0
- data/Rakefile +1 -0
- data/collection_errors.gemspec +23 -0
- data/lib/collection_errors/separate.rb +59 -0
- data/lib/collection_errors/unify.rb +30 -0
- data/lib/collection_errors/version.rb +3 -0
- data/lib/collection_errors.rb +10 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0a4f63955694824b8fa8d6f788f6ed3414efeaed
|
4
|
+
data.tar.gz: b5f1938ed0e2f3af1f8efa024e6ad86682116fd0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 375eb2fc1c59cc5744b1484ba6634e8371609aebdb418a175673b46f03703781c0dc481e7a1a57957d6ddd4f45ede786b90d33314268a1850ff6e9ba2ebbb944
|
7
|
+
data.tar.gz: bab7dacb28773ecd81ca1a719eb0e6101b4ddfd89d161f19c790842fd4d04549660571fad39d865989a758156ce3c60c057520fe4f685a081f51c0cbb2f67342
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 nay3
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# CollectionErrors
|
2
|
+
|
3
|
+
CollectionErrors provides custom validation error building for collections in your ActiveRecord model.
|
4
|
+
|
5
|
+
When you have validation errors in collections, you generally get error with key such as "children.name". Sometimes this is not good because you can't show detailed messages to your users because this key is shared between all elements in the collection.
|
6
|
+
|
7
|
+
CollectionErrors gives alternatives to that.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'collection_errors'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install collection_errors
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
CollectionErrors adds 2 types of validation error building for collection in your Rails application.
|
26
|
+
|
27
|
+
### Separate way
|
28
|
+
|
29
|
+
When you have validation errors in collections, you generally get error with key such as "children.name".
|
30
|
+
|
31
|
+
You can change this by calling separate_errors_on method in your model class.
|
32
|
+
|
33
|
+
has_many :items
|
34
|
+
separate_errors_on :items
|
35
|
+
|
36
|
+
Then you can get error messages like "Item 1 name is invalid".
|
37
|
+
|
38
|
+
You can cange the translation by setting 'collection_error' key. For example,
|
39
|
+
|
40
|
+
en
|
41
|
+
errors
|
42
|
+
messages
|
43
|
+
collection_error: "%{collection_name} %{index} : %{message}"
|
44
|
+
|
45
|
+
By default, index will count objects as well those are marked for destruction.
|
46
|
+
You can ignore those objects by specifying :ignore_marked_for_destruction option.
|
47
|
+
|
48
|
+
has_many :items
|
49
|
+
separate_errors_on :items, :ignore_marked_for_destruction => true
|
50
|
+
|
51
|
+
### Unify
|
52
|
+
|
53
|
+
Or you may want to get simply one message for one collection.
|
54
|
+
|
55
|
+
You can get an invalid error for the collection using unify_errors_on.
|
56
|
+
|
57
|
+
has_many :items
|
58
|
+
unify_errors_on: items
|
59
|
+
|
60
|
+
### Limitation
|
61
|
+
|
62
|
+
This gem is for ActiveRecord and it requires errors#delete. If you are using too old Rails, adding delete mtheod will help.
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
1. Fork it
|
67
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
68
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
69
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
70
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'collection_errors/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "collection_errors"
|
8
|
+
spec.version = CollectionErrors::VERSION
|
9
|
+
spec.authors = ["nay3"]
|
10
|
+
spec.email = ["y.ohba@everyleaf.com"]
|
11
|
+
spec.description = %q{Providing special validation error building for has_many collection in Rails.}
|
12
|
+
spec.summary = %q{Rails validation error extension}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module CollectionErrors
|
2
|
+
module Separate
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
private
|
8
|
+
def generate_error_message_for_collection_element(collection_name, index, message)
|
9
|
+
type = :collection_error
|
10
|
+
defaults = self.class.lookup_ancestors.map do |klass|
|
11
|
+
[ :"#{self.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{collection_name}.#{type}",
|
12
|
+
:"#{self.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.#{type}" ]
|
13
|
+
end
|
14
|
+
defaults << :"#{self.class.i18n_scope}.errors.messages.#{type}"
|
15
|
+
defaults << :"errors.attributes.#{collection_name}.#{type}"
|
16
|
+
defaults << :"errors.messages.#{type}"
|
17
|
+
defaults << "%{collection_name}%{index} %{message}"
|
18
|
+
|
19
|
+
defaults.compact!
|
20
|
+
defaults.flatten!
|
21
|
+
|
22
|
+
I18n.translate(defaults.shift, :collection_name => self.class.human_attribute_name(collection_name), :index => index, :message => message, :default => defaults)
|
23
|
+
end
|
24
|
+
|
25
|
+
module ClassMethods
|
26
|
+
def separate_errors_on(*args)
|
27
|
+
options = {:ignore_marked_for_destruction => false}.merge(args.extract_options!)
|
28
|
+
|
29
|
+
args.each do |collection_name|
|
30
|
+
method_name = "separate_errors_on_#{collection_name}"
|
31
|
+
|
32
|
+
define_method method_name do
|
33
|
+
|
34
|
+
# Remove 'collection_name.xxx' attributes from errors
|
35
|
+
targets = []
|
36
|
+
errors.each do |attribute, message|
|
37
|
+
targets << attribute if attribute.to_s =~ /^#{collection_name}\./
|
38
|
+
end
|
39
|
+
targets.each{|k| errors.delete(k) }
|
40
|
+
|
41
|
+
# Add original errors
|
42
|
+
index = 0
|
43
|
+
send(collection_name).each do |r|
|
44
|
+
next if options[:ignore_marked_for_destruction] && r.marked_for_destruction?
|
45
|
+
index += 1
|
46
|
+
next if r.errors.empty?
|
47
|
+
|
48
|
+
r.errors.full_messages.each do |full_message|
|
49
|
+
errors.add(:base, generate_error_message_for_collection_element(collection_name, index, full_message))
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
after_validation method_name
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module CollectionErrors
|
2
|
+
module Unify
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def unify_errors_on(*args)
|
9
|
+
args.each do |collection_name|
|
10
|
+
method_name = "unify_errors_on_#{collection_name}"
|
11
|
+
|
12
|
+
define_method method_name do
|
13
|
+
|
14
|
+
# Remove 'collection_name.xxx' attributes from errors
|
15
|
+
targets = []
|
16
|
+
errors.each do |attribute, message|
|
17
|
+
targets << attribute if attribute.to_s =~ /^#{collection_name}\./
|
18
|
+
end
|
19
|
+
targets.each{|k| errors.delete(k) }
|
20
|
+
|
21
|
+
# Add original errors
|
22
|
+
errors.add(collection_name, :invalid) if targets.any?
|
23
|
+
end
|
24
|
+
|
25
|
+
after_validation method_name
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require "collection_errors/version"
|
2
|
+
|
3
|
+
module CollectionErrors
|
4
|
+
end
|
5
|
+
|
6
|
+
require 'collection_errors/separate'
|
7
|
+
require 'collection_errors/unify'
|
8
|
+
|
9
|
+
ActiveRecord::Base.send(:include, CollectionErrors::Separate)
|
10
|
+
ActiveRecord::Base.send(:include, CollectionErrors::Unify)
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: collection_errors
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- nay3
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
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
|
+
description: Providing special validation error building for has_many collection in
|
42
|
+
Rails.
|
43
|
+
email:
|
44
|
+
- y.ohba@everyleaf.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- collection_errors.gemspec
|
55
|
+
- lib/collection_errors.rb
|
56
|
+
- lib/collection_errors/separate.rb
|
57
|
+
- lib/collection_errors/unify.rb
|
58
|
+
- lib/collection_errors/version.rb
|
59
|
+
homepage: ''
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.0.4
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Rails validation error extension
|
83
|
+
test_files: []
|