advanced_errors 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +14 -0
- data/Rakefile +22 -0
- data/lib/advanced_errors.rb +6 -0
- data/lib/advanced_errors/full_messages.rb +26 -0
- data/rails/init.rb +1 -0
- data/test/full_message_test.rb +46 -0
- data/test/test_helper.rb +65 -0
- metadata +82 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 [name of plugin creator]
|
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
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
AdvancedErrors
|
2
|
+
==============
|
3
|
+
|
4
|
+
Extentions to ActiveRecord's error handling features which I find useful.
|
5
|
+
|
6
|
+
|
7
|
+
Features
|
8
|
+
========
|
9
|
+
|
10
|
+
Disabling the rendering of the attribute name for errors by the error_messages_for helper.
|
11
|
+
Simply place a caret as the first character in the message.
|
12
|
+
|
13
|
+
|
14
|
+
Copyright (c) 2008 Mark Catley, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the advanced_errors plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the advanced_errors plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'AdvancedErrors'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Nexx
|
2
|
+
module AdvancedErrors
|
3
|
+
module FullMessages
|
4
|
+
REGEX = /^\^/
|
5
|
+
|
6
|
+
def full_messages_with_ignore_attribute
|
7
|
+
error = ActiveRecord::Errors.new @base
|
8
|
+
self.each do |attribute, message|
|
9
|
+
unless message.match(REGEX)
|
10
|
+
error.add attribute, message
|
11
|
+
else
|
12
|
+
error.add_to_base message.sub REGEX, ''
|
13
|
+
end
|
14
|
+
end
|
15
|
+
error.full_messages_without_ignore_attribute
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.included(base)
|
19
|
+
base.class_eval do
|
20
|
+
alias_method :full_messages_without_ignore_attribute, :full_messages
|
21
|
+
alias_method :full_messages, :full_messages_with_ignore_attribute
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'advanced_errors'
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.dirname(__FILE__) + File::SEPARATOR + 'test_helper'
|
2
|
+
|
3
|
+
class AdvancedErrorsTest < Test::Unit::TestCase
|
4
|
+
include ErrorHelper
|
5
|
+
|
6
|
+
def test_should_render_errors
|
7
|
+
@user = User.new({:login => 'blah'})
|
8
|
+
assert !@user.valid?, 'User should be invalid'
|
9
|
+
assert @user.errors.size == 1, 'There should only be one error.'
|
10
|
+
assert !@user.errors.on(:password).blank?, 'They should be a failed validations on :password'
|
11
|
+
assert @user.errors.full_messages.first == "Password can't be blank", "The message is wrong."
|
12
|
+
expected_error_message = %Q{
|
13
|
+
<div class="errorExplanation" id="errorExplanation">
|
14
|
+
<h2>1 error prohibited this user from being saved</h2>
|
15
|
+
<p>There were problems with the following fields:</p>
|
16
|
+
<ul>
|
17
|
+
<li>Password can't be blank</li>
|
18
|
+
</ul>
|
19
|
+
</div>
|
20
|
+
}.normalize_html
|
21
|
+
actual_error_message = error_messages_for(:user).normalize_html
|
22
|
+
assert expected_error_message == actual_error_message, 'Expected and actual messages should be the same'
|
23
|
+
@user = nil
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_should_not_display_attribute_if_caret_is_first_charactor
|
27
|
+
@user = User.new({:password => 'blah'})
|
28
|
+
assert !@user.valid?, 'User should be invalid'
|
29
|
+
assert_equal 1, @user.errors.size
|
30
|
+
assert !@user.errors.on(:login).blank?, 'They should be a failed validations on :login'
|
31
|
+
puts @user.errors.method(:full_messages).inspect
|
32
|
+
assert_equal 'You really should have a login.', @user.errors.full_messages.first
|
33
|
+
expected_error_message = %Q{
|
34
|
+
<div class="errorExplanation" id="errorExplanation">
|
35
|
+
<h2>1 error prohibited this user from being saved</h2>
|
36
|
+
<p>There were problems with the following fields:</p>
|
37
|
+
<ul>
|
38
|
+
<li>You really should have a login.</li>
|
39
|
+
</ul>
|
40
|
+
</div>
|
41
|
+
}.normalize_html
|
42
|
+
actual_error_message = error_messages_for(:user).normalize_html
|
43
|
+
assert expected_error_message == actual_error_message, 'Expected and actual messages should be the same'
|
44
|
+
@user = nil
|
45
|
+
end
|
46
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_record'
|
5
|
+
require 'action_controller'
|
6
|
+
require 'action_view'
|
7
|
+
require File.expand_path('../../rails/init', __FILE__)
|
8
|
+
|
9
|
+
ActiveRecord::Base.configurations = {'sqlite3' => {:adapter => 'sqlite3', :database => ':memory:'}}
|
10
|
+
ActiveRecord::Base.establish_connection('sqlite3')
|
11
|
+
|
12
|
+
ActiveRecord::Schema.define(:version => 0) do
|
13
|
+
create_table :users do |t|
|
14
|
+
t.boolean :admin, :default => false
|
15
|
+
t.string :login, :default => ''
|
16
|
+
t.string :password, :default => ''
|
17
|
+
end
|
18
|
+
|
19
|
+
create_table :accounts do |t|
|
20
|
+
t.string :subdomain, :default => ''
|
21
|
+
t.string :title, :default => ''
|
22
|
+
end
|
23
|
+
|
24
|
+
create_table :emails do |t|
|
25
|
+
t.references :user_id
|
26
|
+
t.string :email_address
|
27
|
+
t.string :preferred_type
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class User < ActiveRecord::Base
|
32
|
+
has_many :emails
|
33
|
+
validates_presence_of :login, :message => "^You really should have a login."
|
34
|
+
validates_presence_of :password
|
35
|
+
attr_accessible :login, :password
|
36
|
+
end
|
37
|
+
|
38
|
+
class Email < ActiveRecord::Base
|
39
|
+
belongs_to :user
|
40
|
+
validates_presence_of :user, :email_address
|
41
|
+
validates_inclusion_of :preferred_type, :in => ['html', 'plain text']
|
42
|
+
attr_accessible :email_address
|
43
|
+
end
|
44
|
+
|
45
|
+
class Account < ActiveRecord::Base; end
|
46
|
+
|
47
|
+
class String
|
48
|
+
def colapse_whitespace
|
49
|
+
gsub(/\s+/, ' ')
|
50
|
+
end
|
51
|
+
|
52
|
+
def remove_unnessisary_spaces_from_html
|
53
|
+
gsub(/>\s+</, '><')
|
54
|
+
end
|
55
|
+
|
56
|
+
def normalize_html
|
57
|
+
colapse_whitespace.remove_unnessisary_spaces_from_html.strip
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
module ErrorHelper
|
62
|
+
include ActionView::Helpers::TagHelper
|
63
|
+
include ActionView::Helpers::TextHelper
|
64
|
+
include ActionView::Helpers::ActiveRecordHelper
|
65
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: advanced_errors
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 1.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Mark Catley
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-03-04 00:00:00 +13:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activerecord
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
32
|
+
description: |-
|
33
|
+
Disabling the rendering of the attribute name for errors by the error_messages_for helper.
|
34
|
+
Simply place a caret as the first character in the message.
|
35
|
+
email: mark@nexx.co.nz
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files: []
|
41
|
+
|
42
|
+
files:
|
43
|
+
- README
|
44
|
+
- MIT-LICENSE
|
45
|
+
- Rakefile
|
46
|
+
- rails/init.rb
|
47
|
+
- lib/advanced_errors.rb
|
48
|
+
- lib/advanced_errors/full_messages.rb
|
49
|
+
- test/test_helper.rb
|
50
|
+
- test/full_message_test.rb
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/markcatley/advanced_errors
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.3.6
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: "[Rails] Extentions to ActiveRecord's error handling features which I find useful."
|
81
|
+
test_files: []
|
82
|
+
|