rails_api_validation_errors 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c67df19656e245cb94ce548bb15cc5e0e9f45f49
4
+ data.tar.gz: 616f41d59a76b0674d2de3839df632c039e2a439
5
+ SHA512:
6
+ metadata.gz: 0d845967d8bb35854eb37a0bdab081eaebd9a3118bbe0723d94c8c101621b748582c30bc36873c60cba473418e5610c8f4ea3acca97ae54794ab9f899803329a
7
+ data.tar.gz: 0e7f168c9c613c5bf466eed1bf65a19afbe065f352ebd3ed66a04340807595acbf1850d6c0272a91f54cb34722520a1d3f9f1675aed77cde6da4f129414fcda9
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Moritz Lawitschka
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ [![Build Status](http://img.shields.io/travis/lawitschka/rails-api-validation-errors.svg)](https://travis-ci.org/lawitschka/rails-api-validation-errors)
2
+ [![Coverage Status](http://img.shields.io/coveralls/lawitschka/rails-api-validation-errors.svg)](https://coveralls.io/r/lawitschka/rails-api-validation-errors)
3
+ [![Code Climate](http://img.shields.io/codeclimate/github/lawitschka/rails-api-validation-errors.svg)](https://codeclimate.com/github/lawitschka/rails-api-validation-errors)
4
+
5
+ # Rails API validation errors
6
+
7
+ Untranslated validation errors with all meta data for APIs behind Javascript frontends
8
+
9
+
10
+ ## Installing
11
+
12
+ The easiest way to install `Rails::API::ValidationErrors` is to add it to your
13
+ `Gemfile`:
14
+
15
+ ```ruby
16
+ gem "rails_api_validation_errors"
17
+ ```
18
+
19
+ Then, install it on the command line:
20
+
21
+ ```
22
+ $ bundle install
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ Include `Rails::API::HashValidationErrors` in your API's base controller. This makes
28
+ sure that Rails will not translate error messages, but returns a hash per attribute
29
+ and error including the error key and meta information.
30
+
31
+ ```ruby
32
+ class API::BaseController < ApplicationController
33
+ include Rails::API::HashValidationErrors
34
+ end
35
+ ```
36
+
37
+ To use the new error messages simply return the model's errors in JSON or XML
38
+ in your controllers:
39
+
40
+ ```ruby
41
+ class API::PeopleController < API::BaseController
42
+
43
+ def create
44
+ @person = Person.new(person_params)
45
+
46
+ if @person.valid?
47
+ render :json => @person
48
+ else
49
+ render :json => { :errors => @person.errors }
50
+ end
51
+ end
52
+
53
+ end
54
+ ```
55
+
56
+ This will result in the following JSON response in case of validation errors:
57
+
58
+ ```json
59
+ {
60
+ "errors": {
61
+ "name": [
62
+ {
63
+ "message": "blank",
64
+ "meta":{}
65
+ }
66
+ ]
67
+ }
68
+ }
69
+ ```
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rspec/core/rake_task'
8
+ RSpec::Core::RakeTask.new('spec')
9
+
10
+ Bundler::GemHelper.install_tasks
11
+
12
+ # If you want to make this the default task
13
+ task :default => :spec
@@ -0,0 +1,13 @@
1
+ module Rails::API::HashValidationErrors
2
+
3
+ def self.included(base)
4
+ base.send :around_filter, :use_hash_validation_errors
5
+ end
6
+
7
+ def use_hash_validation_errors(&block)
8
+ ActiveModel::Errors.disable_translations
9
+ yield
10
+ ActiveModel::Errors.enable_translations
11
+ end
12
+
13
+ end
@@ -0,0 +1,32 @@
1
+ # Monkey patch ActiveModel::Errors class to return a hash containing translation
2
+ # key and options if translation is missing.
3
+ module ActiveModel
4
+ class Errors
5
+
6
+ # Option to specify wether to return hash or translation. Defaults to true
7
+ # to keep original behaviour.
8
+ @@translate_message = true
9
+
10
+ def self.disable_translations
11
+ @@translate_message = false
12
+ end
13
+
14
+ def self.enable_translations
15
+ @@translate_message = true
16
+ end
17
+
18
+ # Keep original method
19
+ alias _generate_message generate_message
20
+
21
+ def generate_message(attribute, type = :invalid, options = {})
22
+ if @@translate_message
23
+ _generate_message(attribute, type, options)
24
+ else
25
+ type = options.delete(:message) if options[:message].is_a?(Symbol)
26
+
27
+ { :message => type, :meta => options }
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module RailsApiValidationErrors
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,12 @@
1
+ require 'active_model'
2
+
3
+ module RailsApiValidationErrors
4
+ end
5
+
6
+ module Rails
7
+ module API
8
+ end
9
+ end
10
+
11
+ require 'rails_api_validation_errors/active_model_errors'
12
+ require 'rails/api/hash_validation_errors'
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_api_validation_errors
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Moritz Lawitschka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 4.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '4.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 4.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: sqlite3
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec-rails
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.14'
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.14.0
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '2.14'
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 2.14.0
67
+ - !ruby/object:Gem::Dependency
68
+ name: rr
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '1.1'
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 1.1.2
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '1.1'
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 1.1.2
87
+ - !ruby/object:Gem::Dependency
88
+ name: simplecov
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ type: :development
95
+ prerelease: false
96
+ version_requirements: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - "~>"
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ - !ruby/object:Gem::Dependency
102
+ name: coveralls
103
+ requirement: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - "~>"
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ type: :development
109
+ prerelease: false
110
+ version_requirements: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - "~>"
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ description: Instead of Rails translating validation errors automatically to current
116
+ locale, validation errors are returned as error meta hashes easily embedded in API
117
+ error responses and translated in the frontend.
118
+ email:
119
+ - me@moritzlawitschka.de
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - MIT-LICENSE
125
+ - README.md
126
+ - Rakefile
127
+ - lib/rails/api/hash_validation_errors.rb
128
+ - lib/rails_api_validation_errors.rb
129
+ - lib/rails_api_validation_errors/active_model_errors.rb
130
+ - lib/rails_api_validation_errors/version.rb
131
+ homepage: https://github.com/lawitschka/rails-api-validation-errors
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.2.1
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Untranslated validation errors with all meta data for APIs behind Javascript
155
+ frontends
156
+ test_files: []