markcatley-advanced_errors 1.0.2 → 1.0.080810
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.
- data/lib/advanced_errors/full_messages.rb +24 -0
- data/rails/init.rb +1 -0
- data/test/full_message_test.rb +36 -6
- data/test/test_helper.rb +62 -0
- metadata +4 -2
- data/init.rb +0 -1
@@ -0,0 +1,24 @@
|
|
1
|
+
module Nexx
|
2
|
+
module AdvancedErrors
|
3
|
+
module FullMessages
|
4
|
+
def full_messages_with_ignore_attribute
|
5
|
+
error = ActiveRecord::Errors.new @base
|
6
|
+
self.each do |attribute, message|
|
7
|
+
unless message.match(/^\^/)
|
8
|
+
error.add attribute, message
|
9
|
+
else
|
10
|
+
error.add_to_base message[1..message.length] #Adding to base will mean that the attribute will not be displayed
|
11
|
+
end
|
12
|
+
end
|
13
|
+
error.full_messages_without_ignore_attribute
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.included(base)
|
17
|
+
base.class_eval do
|
18
|
+
alias_method :full_messages_without_ignore_attribute, :full_messages
|
19
|
+
alias_method :full_messages, :full_messages_with_ignore_attribute
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'advanced_errors'
|
data/test/full_message_test.rb
CHANGED
@@ -2,15 +2,45 @@ require 'test/unit'
|
|
2
2
|
require File.dirname(__FILE__) + File::SEPARATOR + 'test_helper'
|
3
3
|
|
4
4
|
class AdvancedErrorsTest < Test::Unit::TestCase
|
5
|
+
include ErrorHelper
|
6
|
+
|
5
7
|
def test_should_render_errors
|
6
|
-
|
8
|
+
@user = User.new({:login => 'blah'})
|
9
|
+
assert !@user.valid?, 'User should be invalid'
|
10
|
+
assert @user.errors.size == 1, 'There should only be one error.'
|
11
|
+
assert !@user.errors.on(:password).blank?, 'They should be a failed validations on :password'
|
12
|
+
assert @user.errors.full_messages.first == "Password can't be blank", "The message is wrong."
|
13
|
+
expected_error_message = %Q{
|
14
|
+
<div class="errorExplanation" id="errorExplanation">
|
15
|
+
<h2>1 error prohibited this user from being saved</h2>
|
16
|
+
<p>There were problems with the following fields:</p>
|
17
|
+
<ul>
|
18
|
+
<li>Password can't be blank</li>
|
19
|
+
</ul>
|
20
|
+
</div>
|
21
|
+
}.normalize_html
|
22
|
+
actual_error_message = error_messages_for(:user).normalize_html
|
23
|
+
assert expected_error_message == actual_error_message, 'Expected and actual messages should be the same'
|
24
|
+
@user = nil
|
7
25
|
end
|
8
26
|
|
9
27
|
def should_not_display_attribute_if_caret_is_first_charactor
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
28
|
+
@user = User.new({:password => 'blah'})
|
29
|
+
assert !@user.valid?, 'User should be invalid'
|
30
|
+
assert @user.errors.size == 1, 'There should only be one error.'
|
31
|
+
assert !@user.errors.on(:login).blank?, 'They should be a failed validations on :login'
|
32
|
+
assert @user.errors.full_messages.first == 'You really should have a login.', "The message is wrong."
|
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
|
15
45
|
end
|
16
46
|
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_record'
|
4
|
+
require 'action_controller'
|
5
|
+
require 'action_view'
|
6
|
+
require File.dirname(__FILE__) + '/../rails/init'
|
7
|
+
|
8
|
+
ActiveRecord::Base.configurations = {'sqlite3' => {:adapter => 'sqlite3', :database => ':memory:'}}
|
9
|
+
ActiveRecord::Base.establish_connection('sqlite3')
|
10
|
+
|
11
|
+
ActiveRecord::Schema.define(:version => 0) do
|
12
|
+
create_table :users do |t|
|
13
|
+
t.boolean :admin, :default => false
|
14
|
+
t.string :login, :default => ''
|
15
|
+
t.string :password, :default => ''
|
16
|
+
end
|
17
|
+
|
18
|
+
create_table :accounts do |t|
|
19
|
+
t.string :subdomain, :default => ''
|
20
|
+
t.string :title, :default => ''
|
21
|
+
end
|
22
|
+
|
23
|
+
create_table :emails do |t|
|
24
|
+
t.references :user_id
|
25
|
+
t.string :email_address
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class User < ActiveRecord::Base
|
30
|
+
has_many :emails
|
31
|
+
validates_presence_of :login, :message => "^You really should have a login."
|
32
|
+
validates_presence_of :password
|
33
|
+
attr_accessible :login, :password
|
34
|
+
end
|
35
|
+
|
36
|
+
class Email < ActiveRecord::Base
|
37
|
+
belongs_to :user
|
38
|
+
validates_presence_of :user, :email_address
|
39
|
+
attr_accessible :email_address
|
40
|
+
end
|
41
|
+
|
42
|
+
class Account < ActiveRecord::Base; end
|
43
|
+
|
44
|
+
class String
|
45
|
+
def colapse_whitespace
|
46
|
+
gsub(/\s+/, ' ')
|
47
|
+
end
|
48
|
+
|
49
|
+
def remove_unnessisary_spaces_from_html
|
50
|
+
gsub(/>\s+</, '><')
|
51
|
+
end
|
52
|
+
|
53
|
+
def normalize_html
|
54
|
+
colapse_whitespace.remove_unnessisary_spaces_from_html.strip
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
module ErrorHelper
|
59
|
+
include ActionView::Helpers::TagHelper
|
60
|
+
include ActionView::Helpers::TextHelper
|
61
|
+
include ActionView::Helpers::ActiveRecordHelper
|
62
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: markcatley-advanced_errors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.080810
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Catley
|
@@ -33,8 +33,10 @@ files:
|
|
33
33
|
- README
|
34
34
|
- MIT-LICENSE
|
35
35
|
- Rakefile
|
36
|
-
- init.rb
|
36
|
+
- rails/init.rb
|
37
37
|
- lib/advanced_errors.rb
|
38
|
+
- lib/advanced_errors/full_messages.rb
|
39
|
+
- test/test_helper.rb
|
38
40
|
- test/full_message_test.rb
|
39
41
|
has_rdoc: false
|
40
42
|
homepage: http://github.com/markcatley/advanced_errors
|
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rails/init.rb
|