activemodel-errors-tree 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: 772373d73766843b2c2ca35fd7e54465de21f8a0
4
+ data.tar.gz: 0bbd37f0b7f9b15f9a34adbcf1acf4b19d8504d4
5
+ SHA512:
6
+ metadata.gz: a520c6f018fc91d711253bf0ae4f2e049181549b8d1a0d228d58a1b546918e8d1d43e2d9b7b8a19f32aea2f1b5293460cbf6d4bdc586086aa44d1a2e66bbf510
7
+ data.tar.gz: 25332dda1bb48247af3a735364a18c88533fdd4765888bd28c20937b245aacee20d35f4ec61c19bd7cfb68ae2088106dcb646b59e6ea4852c908ccca5b575707
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --format documentation
3
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.3
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activemodel-errors-tree.gemspec
4
+ gemspec
@@ -0,0 +1,91 @@
1
+ # ActiveModel::Errors::Tree
2
+
3
+ Did you too search Google for a way to encode your Rails errors in your API?
4
+ Well you probably encountered an awful response like this:
5
+
6
+ ```ruby
7
+ render json: { errors: @record.errors.messages }, status: :unprocessable_entity
8
+ ```
9
+
10
+ **This is not a good mechanism.**
11
+
12
+ Why? Let's say you have a model with associations. The way ActiveRecord encodes
13
+ `errors.messages` is this:
14
+
15
+ ```ruby
16
+ { :field => ["can't be blank"], :"association.field" => ["must be less than 5"] }
17
+ ```
18
+
19
+ Let's say you developed a great client side implementation to figure out
20
+ `"association.field"` is actually an error on the association model. What
21
+ happens when it's a one-to-many association?
22
+
23
+ ```ruby
24
+ { :field => ["can't be blank"], :"associations.field" => ["must be less than 5"] }
25
+ ```
26
+
27
+ What?! How are you supposed to know which instance of `associations` received
28
+ the error? How do you mark the correct textbox in red?
29
+
30
+ ## Installation
31
+
32
+ Add this line to your application's Gemfile:
33
+
34
+ ```ruby
35
+ gem 'activemodel-errors-tree'
36
+ ```
37
+
38
+ And then execute:
39
+
40
+ $ bundle
41
+
42
+ Or install it yourself as:
43
+
44
+ $ gem install activemodel-errors-tree
45
+
46
+ ## Usage
47
+
48
+ By calling `@record.errors.tree` you get a much prettier picture, that is easier
49
+ to decode on the client side.
50
+
51
+ Following the examples above:
52
+
53
+ ### One-to-one
54
+
55
+ ```ruby
56
+ @record.errors.tree.messages
57
+ # This is actually a ActiveSupport::HashWithIndifferentAccess
58
+ => { field: ["can't be blank"], association: { field: ["must be less than 5"] } }
59
+ ```
60
+
61
+ ### One-to-many
62
+
63
+ ```ruby
64
+ @record.errors.tree.messages
65
+ # Assuming the error is on the second association out of three.
66
+ => { field: ["can't be blank"], associations: [{}, { field: ["must be less than 5"] }, {}] }
67
+ ```
68
+
69
+ Now your client can iterate over the association's errors along with rendering
70
+ the associations, and render out each association's errors.
71
+
72
+ ### errors.details (the recommended way)
73
+
74
+ If you use Rails 5 or include the `active_model-errors_details` gem, you can use
75
+ `@record.errors.tree.details` instead of `messages`. This is more API friendly
76
+ since it let's your client decide on the actual messages.
77
+
78
+ ```ruby
79
+ @record.errors.tree.details
80
+ # Assuming the error is on the second association out of three.
81
+ => { field: [{ error: :blank }], associations: [{}, { field: [{ error: :less_than, count: 5 }] }, {}] }
82
+ ```
83
+
84
+ ## Development
85
+
86
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
87
+
88
+ ## Contributing
89
+
90
+ Bug reports and pull requests are welcome on GitHub at https://github.com/odedniv/activemodel-errors-tree. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
91
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
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 NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org/>
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "activemodel-errors-tree"
7
+ spec.version = "0.1.0" # for some reason Rails applications do not like it defined in activemodel/errors/tree/version
8
+ spec.authors = ["Oded Niv"]
9
+ spec.email = ["oded.niv@gmail.com"]
10
+
11
+ spec.summary = %q{ActiveModel errors format actually appropriate for APIs.}
12
+ spec.description = %q{Builds a tree of errors instead of the 1-dimension one generated by ActiveMode::Base#errors.messages.}
13
+ spec.homepage = "https://github.com/odedniv/activemodel-errors-tree"
14
+ spec.license = "UNLICENSE"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "activesupport", ">= 4.2"
22
+ spec.add_runtime_dependency "activemodel", ">= 4.2"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.2"
27
+ spec.add_development_dependency "sqlite3", "~> 1.3"
28
+ # TODO: switch to activerecord 5 and remove active_model-errors_details
29
+ spec.add_development_dependency "active_model-errors_details", "~> 1.2"
30
+ spec.add_development_dependency "activerecord", "~> 4.2"
31
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "activemodel/errors/tree"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,61 @@
1
+ require "active_model/errors"
2
+ require "active_support/hash_with_indifferent_access"
3
+
4
+ module ActiveModel
5
+ class Errors
6
+ class Tree
7
+ def initialize(base, errors)
8
+ @base = base
9
+ @errors = errors
10
+ end
11
+
12
+ ATTRIBUTE_SEPARATOR = "."
13
+
14
+ def messages
15
+ @messages ||= method_tree(:messages)
16
+ end
17
+
18
+ def details
19
+ @details ||= method_tree(:details)
20
+ end
21
+
22
+ private
23
+
24
+ def method_tree(method)
25
+ result = ActiveSupport::HashWithIndifferentAccess.new
26
+ @errors.keys.each do |original_attribute|
27
+ attribute, sub_attribute = original_attribute.to_s.split(ATTRIBUTE_SEPARATOR, 2) # we really only care about the first one
28
+ if sub_attribute.nil?
29
+ result[attribute] = @errors.send(method)[original_attribute]
30
+ else
31
+ sub_base = @base.send(attribute)
32
+ result[attribute] = sub_base.respond_to?(:map) ? sub_base.map { |r| r.errors.tree.send(method) } : sub_base.errors.tree.send(method)
33
+ end
34
+ end
35
+ result
36
+ end
37
+ end
38
+
39
+ def tree
40
+ @tree ||= Tree.new(@base, self)
41
+ end
42
+
43
+ alias_method :add_without_reset_tree, :add
44
+ def add(*args)
45
+ @tree = nil
46
+ add_without_reset_tree(*args)
47
+ end
48
+
49
+ alias_method :delete_without_reset_tree, :delete
50
+ def delete(*args)
51
+ @tree = nil
52
+ delete_without_reset_tree(*args)
53
+ end
54
+
55
+ alias_method :clear_without_reset_tree, :clear
56
+ def clear(*args)
57
+ @tree = nil
58
+ clear_without_reset_tree(*args)
59
+ end
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activemodel-errors-tree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Oded Niv
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: active_model-errors_details
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.2'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activerecord
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '4.2'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '4.2'
125
+ description: Builds a tree of errors instead of the 1-dimension one generated by ActiveMode::Base#errors.messages.
126
+ email:
127
+ - oded.niv@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - CODE_OF_CONDUCT.md
136
+ - Gemfile
137
+ - README.md
138
+ - Rakefile
139
+ - UNLICENSE
140
+ - activemodel-errors-tree.gemspec
141
+ - bin/console
142
+ - bin/setup
143
+ - lib/activemodel/errors/tree.rb
144
+ homepage: https://github.com/odedniv/activemodel-errors-tree
145
+ licenses:
146
+ - UNLICENSE
147
+ metadata: {}
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ requirements: []
163
+ rubyforge_project:
164
+ rubygems_version: 2.4.7
165
+ signing_key:
166
+ specification_version: 4
167
+ summary: ActiveModel errors format actually appropriate for APIs.
168
+ test_files: []