custom_error_message 1.1.0.pre → 1.1.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- data/{MIT-LICENSE → LICENSE} +0 -0
- data/Rakefile +20 -0
- data/custom_error_message.gemspec +19 -0
- data/lib/custom_error_message.rb +5 -57
- data/lib/rails/extensions/active_model.rb +31 -0
- data/lib/rails/extensions/active_record.rb +15 -0
- data/spec/custom_error_message_spec.rb +45 -0
- data/spec/db/schema.rb +13 -0
- metadata +24 -18
data/{MIT-LICENSE → LICENSE}
RENAMED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_record'
|
3
|
+
require 'rake/gempackagetask'
|
4
|
+
require 'rubygems/specification'
|
5
|
+
require 'rake/rdoctask'
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
|
8
|
+
desc "Run all examples"
|
9
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
10
|
+
end
|
11
|
+
|
12
|
+
namespace :spec do
|
13
|
+
desc "Run all examples with RCov"
|
14
|
+
RSpec::Core::RakeTask.new(:rcov) do |t|
|
15
|
+
t.rcov = true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
desc 'Default: run unit tests.'
|
20
|
+
task :default => 'spec'
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'custom_error_message'
|
5
|
+
s.version = "1.1.0.pre2"
|
6
|
+
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["David Easley", "Jeremy Durham"]
|
9
|
+
s.email = %q{jeremydurham@gmail.com}
|
10
|
+
s.homepage = 'http://github.com/jeremydurham/custom-err-msg'
|
11
|
+
s.summary = 'Custom Error Message plugin for Rails'
|
12
|
+
s.description = 'This plugin gives you the option to not have your custom validation error message prefixed with the attribute name'
|
13
|
+
|
14
|
+
s.rubygems_version = '>= 1.3.5'
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
end
|
data/lib/custom_error_message.rb
CHANGED
@@ -1,57 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
@errors.each_key do |attr|
|
7
|
-
@errors[attr].each do |msg|
|
8
|
-
next if msg.nil?
|
9
|
-
msg = msg.respond_to?(:message) ? msg.message : msg
|
10
|
-
if attr == "base"
|
11
|
-
full_messages << msg
|
12
|
-
elsif msg =~ /^\^/
|
13
|
-
full_messages << msg[1..-1]
|
14
|
-
elsif msg.is_a? Proc
|
15
|
-
full_messages << msg.call(@base)
|
16
|
-
else
|
17
|
-
full_messages << @base.class.human_attribute_name(attr) + " " + msg
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
full_messages
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
module ActiveModel
|
28
|
-
class Errors < ActiveSupport::OrderedHash
|
29
|
-
def full_messages
|
30
|
-
full_messages = []
|
31
|
-
|
32
|
-
each do |attribute, messages|
|
33
|
-
messages = Array.wrap(messages)
|
34
|
-
next if messages.empty?
|
35
|
-
|
36
|
-
if attribute == :base
|
37
|
-
messages.each {|m| full_messages << m }
|
38
|
-
else
|
39
|
-
attr_name = attribute.to_s.gsub('.', '_').humanize
|
40
|
-
attr_name = @base.class.human_attribute_name(attribute, :default => attr_name)
|
41
|
-
options = { :default => "%{attribute} %{message}", :attribute => attr_name }
|
42
|
-
|
43
|
-
|
44
|
-
messages.each do |m|
|
45
|
-
if m =~ /^\^/
|
46
|
-
full_messages << I18n.t(:"errors.format.full_message", options.merge(:message => m[1..-1], :default => "%{message}"))
|
47
|
-
else
|
48
|
-
full_messages << I18n.t(:"errors.format", options.merge(:message => m))
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
full_messages
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
1
|
+
if defined?(ActiveModel)
|
2
|
+
require 'rails/extensions/active_model'
|
3
|
+
else
|
4
|
+
require 'rails/extensions/active_record'
|
5
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
class Errors < ActiveSupport::OrderedHash
|
3
|
+
def full_messages
|
4
|
+
full_messages = []
|
5
|
+
|
6
|
+
each do |attribute, messages|
|
7
|
+
messages = Array.wrap(messages)
|
8
|
+
next if messages.empty?
|
9
|
+
|
10
|
+
if attribute == :base
|
11
|
+
messages.each {|m| full_messages << m }
|
12
|
+
else
|
13
|
+
attr_name = attribute.to_s.gsub('.', '_').humanize
|
14
|
+
attr_name = @base.class.human_attribute_name(attribute, :default => attr_name)
|
15
|
+
options = { :default => "%{attribute} %{message}", :attribute => attr_name }
|
16
|
+
|
17
|
+
|
18
|
+
messages.each do |m|
|
19
|
+
if m =~ /^\^/
|
20
|
+
full_messages << I18n.t(:"errors.format.full_message", options.merge(:message => m[1..-1], :default => "%{message}"))
|
21
|
+
else
|
22
|
+
full_messages << I18n.t(:"errors.format", options.merge(:message => m))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
full_messages
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
class Error
|
3
|
+
|
4
|
+
protected
|
5
|
+
|
6
|
+
def generate_full_message(options = {})
|
7
|
+
if self.message =~ /^\^/
|
8
|
+
keys = ["{{message}}"]
|
9
|
+
options.merge!(:default => self.message[1..-1])
|
10
|
+
end
|
11
|
+
|
12
|
+
I18n.translate(keys.shift, options)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rspec'
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
class User < ActiveRecord::Base
|
6
|
+
has_many :user_roles
|
7
|
+
has_many :roles, :through => :user_roles
|
8
|
+
|
9
|
+
validates_presence_of :email, :message => "^Your email is invalid"
|
10
|
+
|
11
|
+
accepts_nested_attributes_for :roles
|
12
|
+
end
|
13
|
+
|
14
|
+
class Role < ActiveRecord::Base
|
15
|
+
has_many :user_roles
|
16
|
+
has_many :users, :through => :user_roles
|
17
|
+
|
18
|
+
validates_presence_of :role, :message => "^You must enter a role"
|
19
|
+
end
|
20
|
+
|
21
|
+
class UserRole < ActiveRecord::Base
|
22
|
+
belongs_to :role
|
23
|
+
belongs_to :user
|
24
|
+
end
|
25
|
+
|
26
|
+
require 'custom_error_message'
|
27
|
+
|
28
|
+
describe "validating attributes" do
|
29
|
+
before do
|
30
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
31
|
+
load File.join(File.dirname(__FILE__), 'db', 'schema.rb')
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should return the message specified without a prefix" do
|
35
|
+
@user = User.create
|
36
|
+
@user.errors.full_messages.should include "Your email is invalid"
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "validating nested attributes" do
|
40
|
+
it "should return the message specified without a prefix" do
|
41
|
+
@user = User.create(:roles_attributes => [{}])
|
42
|
+
@user.errors.full_messages.should include "You must enter a role"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/spec/db/schema.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
create_table :users, :force => true do |t|
|
3
|
+
t.string :email, :password, :roles
|
4
|
+
end
|
5
|
+
|
6
|
+
create_table :roles, :force => true do |t|
|
7
|
+
t.string :role, :name
|
8
|
+
end
|
9
|
+
|
10
|
+
create_table :user_roles, :force => true do |t|
|
11
|
+
t.belongs_to :user, :role
|
12
|
+
end
|
13
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: custom_error_message
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: -1876988167
|
5
5
|
prerelease: true
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 1
|
9
9
|
- 0
|
10
|
-
-
|
11
|
-
version: 1.1.0.
|
10
|
+
- pre2
|
11
|
+
version: 1.1.0.pre2
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- David Easley
|
@@ -17,26 +17,29 @@ autorequire:
|
|
17
17
|
bindir: bin
|
18
18
|
cert_chain: []
|
19
19
|
|
20
|
-
date:
|
20
|
+
date: 2010-12-17 00:00:00 -05:00
|
21
21
|
default_executable:
|
22
22
|
dependencies: []
|
23
23
|
|
24
|
-
description:
|
25
|
-
This plugin gives you the option to not have your custom validation error message
|
26
|
-
prefixed with the attribute name
|
24
|
+
description: This plugin gives you the option to not have your custom validation error message prefixed with the attribute name
|
27
25
|
email: jeremydurham@gmail.com
|
28
26
|
executables: []
|
29
27
|
|
30
28
|
extensions: []
|
31
29
|
|
32
|
-
extra_rdoc_files:
|
33
|
-
|
34
|
-
- MIT-LICENSE
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
35
32
|
files:
|
33
|
+
- LICENSE
|
34
|
+
- README
|
35
|
+
- Rakefile
|
36
|
+
- custom_error_message.gemspec
|
36
37
|
- init.rb
|
37
38
|
- lib/custom_error_message.rb
|
38
|
-
-
|
39
|
-
-
|
39
|
+
- lib/rails/extensions/active_model.rb
|
40
|
+
- lib/rails/extensions/active_record.rb
|
41
|
+
- spec/custom_error_message_spec.rb
|
42
|
+
- spec/db/schema.rb
|
40
43
|
has_rdoc: true
|
41
44
|
homepage: http://github.com/jeremydurham/custom-err-msg
|
42
45
|
licenses: []
|
@@ -58,12 +61,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
61
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
62
|
none: false
|
60
63
|
requirements:
|
61
|
-
- - "
|
64
|
+
- - ">"
|
62
65
|
- !ruby/object:Gem::Version
|
63
|
-
hash:
|
66
|
+
hash: 25
|
64
67
|
segments:
|
65
|
-
-
|
66
|
-
|
68
|
+
- 1
|
69
|
+
- 3
|
70
|
+
- 1
|
71
|
+
version: 1.3.1
|
67
72
|
requirements: []
|
68
73
|
|
69
74
|
rubyforge_project:
|
@@ -71,5 +76,6 @@ rubygems_version: 1.3.7
|
|
71
76
|
signing_key:
|
72
77
|
specification_version: 3
|
73
78
|
summary: Custom Error Message plugin for Rails
|
74
|
-
test_files:
|
75
|
-
|
79
|
+
test_files:
|
80
|
+
- spec/custom_error_message_spec.rb
|
81
|
+
- spec/db/schema.rb
|