ames_by_type 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +45 -0
- data/VERSION +1 -1
- data/ames_by_type.gemspec +60 -0
- data/lib/ames_by_type.rb +1 -36
- data/lib/ames_by_type/errors.rb +53 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 671825b8444b47a3e24596c262fb4426c77c43d1
|
4
|
+
data.tar.gz: 8a8f90602a3147ef59c53745de165986eedf28e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b34b39961f0a574b26a66be5f21612dccf3d8d0d19ff7c035fb03164175cb48d82982bde03dc3cc69037b49d520127f095455a800326359cd641a8fd9c6419d2
|
7
|
+
data.tar.gz: 1efa524ac699828bd3cbb641a43076673802a3b7dd338259b2a58eb9f8980d07fe457338aa3f15c233cae98192d9444aa2f77c44f6bc12a6335ed596b0de4b75
|
data/README.rdoc
CHANGED
@@ -5,6 +5,51 @@ retrieve a hash of error messages scoped by attribute and type. Empower your
|
|
5
5
|
server-sent, serialized form/record validation messages to play nicely with data-binding JS
|
6
6
|
frameworks. Get more granular with your validation UIs.
|
7
7
|
|
8
|
+
==== Installation
|
9
|
+
|
10
|
+
Add the following to your +Gemfile+ and +bundle install+.
|
11
|
+
|
12
|
+
gem 'ames_by_type'
|
13
|
+
|
14
|
+
==== Usage
|
15
|
+
|
16
|
+
When an error message is added to an instance of <tt>ActiveModel::Errors</tt>,
|
17
|
+
we add said error message (organized by attribute and message type) to a +messages_by_type+
|
18
|
+
hash. We can retrieve this hash as it exists or with full messages (by keyword argument, shown below).
|
19
|
+
|
20
|
+
zebra.errors.add(:name, :blank)
|
21
|
+
zebra.errors.add(:name, :too_short, count: 2)
|
22
|
+
zebra.errors.by_type
|
23
|
+
# => {
|
24
|
+
name: {
|
25
|
+
blank: "can't be blank",
|
26
|
+
too_short: "is too short (minimum is 2 characters)"
|
27
|
+
}
|
28
|
+
}
|
29
|
+
zebra.errors.by_type(full_messages: true)
|
30
|
+
# => {
|
31
|
+
name: {
|
32
|
+
blank: "Name can't be blank",
|
33
|
+
too_short: "Name is too short (minimum is 2 characters)"
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
In a Rails controller action:
|
38
|
+
|
39
|
+
...
|
40
|
+
def create
|
41
|
+
@zebra = Zebra.new(zebra_params)
|
42
|
+
if @zebra.save
|
43
|
+
render json: @zebra
|
44
|
+
else
|
45
|
+
render json: @zebra.errors.by_type
|
46
|
+
end
|
47
|
+
end
|
48
|
+
...
|
49
|
+
|
50
|
+
Learn how to set up translations for your ActiveRecord models (including error messages)
|
51
|
+
here[http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models].
|
52
|
+
|
8
53
|
==== Copyright
|
9
54
|
|
10
55
|
Copyright (c) 2014 Travis Loncar.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: ames_by_type 0.2.0 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "ames_by_type"
|
9
|
+
s.version = "0.2.0"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib"]
|
13
|
+
s.authors = ["Travis Loncar"]
|
14
|
+
s.date = "2014-02-18"
|
15
|
+
s.description = "This gem extends the functionality of Rails' ActiveModel::Errors\n with a #by_type method that returns a hash of error messages\n scoped by attribute and type."
|
16
|
+
s.email = "loncar.travis@gmail.com"
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"README.rdoc"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".document",
|
22
|
+
".rspec",
|
23
|
+
"Gemfile",
|
24
|
+
"Gemfile.lock",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"ames_by_type.gemspec",
|
29
|
+
"lib/ames_by_type.rb",
|
30
|
+
"lib/ames_by_type/errors.rb",
|
31
|
+
"spec/ames_by_type_spec.rb",
|
32
|
+
"spec/spec_helper.rb"
|
33
|
+
]
|
34
|
+
s.homepage = "http://github.com/tbloncar/ames_by_type"
|
35
|
+
s.licenses = ["MIT"]
|
36
|
+
s.rubygems_version = "2.2.1"
|
37
|
+
s.summary = "Extends ActiveModel::Errors with errors by type."
|
38
|
+
|
39
|
+
if s.respond_to? :specification_version then
|
40
|
+
s.specification_version = 4
|
41
|
+
|
42
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
43
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
|
44
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
45
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
46
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.7"])
|
47
|
+
else
|
48
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
49
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
50
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
51
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.7"])
|
52
|
+
end
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
55
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
56
|
+
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
57
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.7"])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
data/lib/ames_by_type.rb
CHANGED
@@ -1,36 +1 @@
|
|
1
|
-
|
2
|
-
class Errors
|
3
|
-
attr_reader :messages_by_type
|
4
|
-
|
5
|
-
def initialize(base)
|
6
|
-
@base = base
|
7
|
-
@messages = {}
|
8
|
-
@messages_by_type = {}
|
9
|
-
end
|
10
|
-
|
11
|
-
def add(attribute, message = :invalid, options = {})
|
12
|
-
normalized_message = normalize_message(attribute, message, options)
|
13
|
-
if exception = options[:strict]
|
14
|
-
exception = ActiveModel::StrictValidationFailed if exception == true
|
15
|
-
raise exception, full_message(attribute, message)
|
16
|
-
end
|
17
|
-
|
18
|
-
if message.is_a?(Symbol)
|
19
|
-
message_by_type = {}
|
20
|
-
message_by_type[message] = normalized_message
|
21
|
-
self.messages_by_type[attribute] = (self.messages_by_type[attribute] || {}).merge(message_by_type)
|
22
|
-
end
|
23
|
-
|
24
|
-
self[attribute] = normalized_message
|
25
|
-
end
|
26
|
-
|
27
|
-
def by_type(full_messages: false)
|
28
|
-
return messages_by_type.dup unless full_messages
|
29
|
-
messages_by_type.each do |attribute, messages|
|
30
|
-
messages.each do |type, message|
|
31
|
-
messages_by_type[attribute][type] = full_message(attribute, message)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
1
|
+
require 'ames_by_type/errors'
|
@@ -0,0 +1,53 @@
|
|
1
|
+
ActiveModel::Errors.class_eval do
|
2
|
+
attr_reader :messages_by_type
|
3
|
+
|
4
|
+
def initialize(base)
|
5
|
+
@base = base
|
6
|
+
@messages = {}
|
7
|
+
@messages_by_type = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
# If the provided message is a symbol, it is treated as a type.
|
11
|
+
# The +messages_by_type+ hash includes a key for each attribute
|
12
|
+
# (like +messages+), where the corresponding values are error
|
13
|
+
# messages organized (in hash form) by validation types.
|
14
|
+
#
|
15
|
+
# giraffe.errors.add(:name, :blank)
|
16
|
+
# giraffe.errors.add(:name, :too_short, count: 1)
|
17
|
+
# giraffe.errors.messages_by_type
|
18
|
+
# # => { name: { blank: "can't be blank",
|
19
|
+
# too_short: "is too short (minimum is 1 character)"
|
20
|
+
# }}
|
21
|
+
def add(attribute, message = :invalid, options = {})
|
22
|
+
normalized_message = normalize_message(attribute, message, options)
|
23
|
+
if exception = options[:strict]
|
24
|
+
exception = ActiveModel::StrictValidationFailed if exception == true
|
25
|
+
raise exception, full_message(attribute, message)
|
26
|
+
end
|
27
|
+
|
28
|
+
if message.is_a?(Symbol)
|
29
|
+
message_by_type = {}
|
30
|
+
message_by_type[message] = normalized_message
|
31
|
+
self.messages_by_type[attribute] = (self.messages_by_type[attribute] || {}).merge(message_by_type)
|
32
|
+
end
|
33
|
+
|
34
|
+
self[attribute] = normalized_message
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns +messages_by_type+ (outlined above). Accepts a +full_message+
|
38
|
+
# keyword argument. When +true+, full messages are included.
|
39
|
+
#
|
40
|
+
# zebra.errors.add(:name, :blank)
|
41
|
+
# zebra.errors.by_type
|
42
|
+
# # => { name: { blank: "can't be blank" }}
|
43
|
+
# zebra.errors.by_type(full_messages: true)
|
44
|
+
# # => { name: { blank: "Name can't be blank" }}
|
45
|
+
def by_type(full_messages: false)
|
46
|
+
return messages_by_type.dup unless full_messages
|
47
|
+
messages_by_type.each do |attribute, messages|
|
48
|
+
messages.each do |type, message|
|
49
|
+
messages_by_type[attribute][type] = full_message(attribute, message)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ames_by_type
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Travis Loncar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -83,7 +83,9 @@ files:
|
|
83
83
|
- README.rdoc
|
84
84
|
- Rakefile
|
85
85
|
- VERSION
|
86
|
+
- ames_by_type.gemspec
|
86
87
|
- lib/ames_by_type.rb
|
88
|
+
- lib/ames_by_type/errors.rb
|
87
89
|
- spec/ames_by_type_spec.rb
|
88
90
|
- spec/spec_helper.rb
|
89
91
|
homepage: http://github.com/tbloncar/ames_by_type
|