errors-renderer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/LICENSE +7 -0
- data/README.md +29 -0
- data/Rakefile +11 -0
- data/lib/errors-renderer/engine.rb +5 -0
- data/lib/errors-renderer/errors.rb +104 -0
- data/lib/errors-renderer/implants/active_model_errors.rb +13 -0
- data/lib/errors-renderer/implants/controller_actions.rb +44 -0
- data/lib/errors-renderer/implants/railtie.rb +9 -0
- data/lib/errors-renderer/implants.rb +12 -0
- data/lib/errors-renderer/localizor.rb +11 -0
- data/lib/errors-renderer.rb +9 -0
- data/spec/errors-renderer_spec.rb +11 -0
- data/spec/spec_helper.rb +12 -0
- metadata +84 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2012 De Marque inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
Errors Renderer
|
2
|
+
===============
|
3
|
+
|
4
|
+
Error rendering engine to standardize all error outputs.
|
5
|
+
|
6
|
+
Install
|
7
|
+
-------
|
8
|
+
|
9
|
+
```
|
10
|
+
gem install errors-renderer
|
11
|
+
```
|
12
|
+
|
13
|
+
### Rails 3
|
14
|
+
|
15
|
+
In your Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'errors-renderer'
|
19
|
+
```
|
20
|
+
|
21
|
+
IMPORTANT
|
22
|
+
-----
|
23
|
+
|
24
|
+
Errors-Renderer currently have some dependencies with a private project. It is therefore unusable for other projects. We hope to fix that soon.
|
25
|
+
|
26
|
+
Copyright
|
27
|
+
---------
|
28
|
+
|
29
|
+
Copyright (c) 2013 De Marque inc. See LICENSE for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
module ErrorsRenderer
|
2
|
+
class Errors
|
3
|
+
def initialize(errors_or_record, options={})
|
4
|
+
options.reverse_merge! controller: nil, action: nil
|
5
|
+
|
6
|
+
@errors = parse_raw_errors errors_or_record
|
7
|
+
@root_name = get_root_name options[:controller], options[:action]
|
8
|
+
end
|
9
|
+
|
10
|
+
def all
|
11
|
+
@errors
|
12
|
+
end
|
13
|
+
|
14
|
+
def codes
|
15
|
+
@errors.map{ |e| e[:code] }.compact
|
16
|
+
end
|
17
|
+
|
18
|
+
def empty?
|
19
|
+
@errors.empty?
|
20
|
+
end
|
21
|
+
|
22
|
+
def full_codes
|
23
|
+
@errors.map { |e| e[:full_code] }.compact
|
24
|
+
end
|
25
|
+
|
26
|
+
def full_messages
|
27
|
+
@errors.map{ |e| e[:full_message] }.compact
|
28
|
+
end
|
29
|
+
|
30
|
+
def localize
|
31
|
+
ErrorsRenderer::Localizor
|
32
|
+
end
|
33
|
+
|
34
|
+
def root_name
|
35
|
+
@root_name
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_json
|
39
|
+
result = @errors.map do |e|
|
40
|
+
err = { code: e[:full_code], message: e[:full_message] }
|
41
|
+
err[:field] = e[:field] if e[:field]
|
42
|
+
|
43
|
+
err
|
44
|
+
end
|
45
|
+
|
46
|
+
return result.to_json
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def get_root_name(controller, action)
|
52
|
+
if action == 'index'
|
53
|
+
controller
|
54
|
+
else
|
55
|
+
controller.singularize
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def parse_error(code, message, field=nil, full_message=nil)
|
60
|
+
error = { code: code, message: message }
|
61
|
+
|
62
|
+
if field == :model_error
|
63
|
+
field = nil
|
64
|
+
full_message = message
|
65
|
+
end
|
66
|
+
|
67
|
+
error[:field] = field if field
|
68
|
+
error[:full_message] = full_message || message
|
69
|
+
error[:full_code] = [field, code].compact.join('_')
|
70
|
+
|
71
|
+
return error
|
72
|
+
end
|
73
|
+
|
74
|
+
def parse_error_from_code(code)
|
75
|
+
parse_error code, I18n.t("errors.messages.#{code}")
|
76
|
+
end
|
77
|
+
|
78
|
+
def parse_raw_errors(errors_or_record)
|
79
|
+
if errors_or_record.is_a? String
|
80
|
+
[parse_error_from_code(errors_or_record)]
|
81
|
+
elsif errors_or_record.is_a? Array
|
82
|
+
errors_or_record.map { |e| parse_error_from_code e }
|
83
|
+
else
|
84
|
+
parse_record_errors errors_or_record
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# TOFIX: Dependencies
|
89
|
+
# translate_with_article
|
90
|
+
# bbl.field_label
|
91
|
+
# downcase_utf8
|
92
|
+
def parse_record_errors(record)
|
93
|
+
errors = []
|
94
|
+
|
95
|
+
record.errors.each do |e,b|
|
96
|
+
full_message = [I18n.translate_with_article('form.field'), record.bbl.field_label(e).downcase_utf8, b].join(' ')
|
97
|
+
|
98
|
+
errors << parse_error(localize.get_code_from_message(b), b, e, full_message)
|
99
|
+
end
|
100
|
+
|
101
|
+
return errors
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module ErrorsRenderer::Implants
|
2
|
+
module ActiveModelErrors
|
3
|
+
def add_from_code(attribute, code)
|
4
|
+
self.add attribute, ErrorsRenderer::Localizor.get_message_from_code(code)
|
5
|
+
end
|
6
|
+
|
7
|
+
def add_to_model(code)
|
8
|
+
self.add 'model_error', ErrorsRenderer::Localizor.get_message_from_code(code)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveModel::Errors.send :include, ErrorsRenderer::Implants::ActiveModelErrors if defined? ActiveModel
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module ErrorsRenderer::Implants
|
2
|
+
module ControllerActions
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
helper_method :response_errors
|
7
|
+
end
|
8
|
+
|
9
|
+
def render_errors(status, errors_or_record, options={})
|
10
|
+
options.reverse_merge! template: 'errors/response'
|
11
|
+
|
12
|
+
self.response.extend ResponseErrorAttribute
|
13
|
+
|
14
|
+
@response_errors = ErrorsRenderer::Errors.new(errors_or_record, controller: self.request[:controller], action: self.request[:action])
|
15
|
+
@response_error_codes = @response_errors.full_codes
|
16
|
+
|
17
|
+
self.response.error_codes = @response_error_codes
|
18
|
+
self.response.status = status
|
19
|
+
self.respond_to do |format|
|
20
|
+
format.html { self.render(template: 'errors/response') }
|
21
|
+
format.any(:json, :xml) { self.render(template: options[:template]) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def response_errors?
|
26
|
+
@response_errors and not response_errors.empty?
|
27
|
+
end
|
28
|
+
|
29
|
+
def response_errors
|
30
|
+
@response_errors
|
31
|
+
end
|
32
|
+
|
33
|
+
def response_error_codes
|
34
|
+
@response_error_codes
|
35
|
+
end
|
36
|
+
|
37
|
+
module ResponseErrorAttribute
|
38
|
+
attr_accessor :error_codes
|
39
|
+
end
|
40
|
+
|
41
|
+
::ActionController::Base.send :include, self
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module ErrorsRenderer
|
2
|
+
module Implants
|
3
|
+
end
|
4
|
+
end
|
5
|
+
|
6
|
+
if defined? Rails::Railtie
|
7
|
+
require 'errors-renderer/implants/railtie'
|
8
|
+
elsif defined? Rails::Initializer
|
9
|
+
raise "errors-renderer is not compatible with Rails 2.3 or older"
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'errors-renderer/implants/active_model_errors'
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
|
4
|
+
require 'rspec'
|
5
|
+
|
6
|
+
require File.expand_path('../../lib/errors-renderer', __FILE__)
|
7
|
+
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.mock_with :rspec
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: errors-renderer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sebastien Rosa
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
requirement: &2159489980 !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.8.7
|
20
|
+
none: false
|
21
|
+
prerelease: false
|
22
|
+
type: :development
|
23
|
+
name: rake
|
24
|
+
version_requirements: *2159489980
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
requirement: &2159489320 !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '2.0'
|
31
|
+
none: false
|
32
|
+
prerelease: false
|
33
|
+
type: :development
|
34
|
+
name: rspec
|
35
|
+
version_requirements: *2159489320
|
36
|
+
description: ''
|
37
|
+
email:
|
38
|
+
- sebastien@demarque.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.md
|
44
|
+
files:
|
45
|
+
- lib/errors-renderer/engine.rb
|
46
|
+
- lib/errors-renderer/errors.rb
|
47
|
+
- lib/errors-renderer/implants/active_model_errors.rb
|
48
|
+
- lib/errors-renderer/implants/controller_actions.rb
|
49
|
+
- lib/errors-renderer/implants/railtie.rb
|
50
|
+
- lib/errors-renderer/implants.rb
|
51
|
+
- lib/errors-renderer/localizor.rb
|
52
|
+
- lib/errors-renderer.rb
|
53
|
+
- spec/errors-renderer_spec.rb
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
- LICENSE
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- Gemfile
|
59
|
+
homepage: https://github.com/demarque/errors-renderer
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
none: false
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
none: false
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project: errors-renderer
|
80
|
+
rubygems_version: 1.8.10
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: ''
|
84
|
+
test_files: []
|