davekaro-custom_error_message 1.2.1.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +41 -0
- data/LICENSE +20 -0
- data/README.md +39 -0
- data/Rakefile +19 -0
- data/davekaro-custom_error_message.gemspec +19 -0
- data/init.rb +1 -0
- data/lib/custom_error_message.rb +5 -0
- data/lib/rails/extensions/active_model.rb +31 -0
- data/lib/rails/extensions/active_record.rb +25 -0
- data/spec/custom_error_message_spec.rb +54 -0
- data/spec/db/schema.rb +13 -0
- metadata +60 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.1.0)
|
5
|
+
activesupport (= 3.1.0)
|
6
|
+
bcrypt-ruby (~> 3.0.0)
|
7
|
+
builder (~> 3.0.0)
|
8
|
+
i18n (~> 0.6)
|
9
|
+
activerecord (3.1.0)
|
10
|
+
activemodel (= 3.1.0)
|
11
|
+
activesupport (= 3.1.0)
|
12
|
+
arel (~> 2.2.1)
|
13
|
+
tzinfo (~> 0.3.29)
|
14
|
+
activesupport (3.1.0)
|
15
|
+
multi_json (~> 1.0)
|
16
|
+
arel (2.2.1)
|
17
|
+
bcrypt-ruby (3.0.1)
|
18
|
+
builder (3.0.0)
|
19
|
+
diff-lcs (1.1.3)
|
20
|
+
i18n (0.6.0)
|
21
|
+
multi_json (1.0.3)
|
22
|
+
rspec (2.6.0)
|
23
|
+
rspec-core (~> 2.6.0)
|
24
|
+
rspec-expectations (~> 2.6.0)
|
25
|
+
rspec-mocks (~> 2.6.0)
|
26
|
+
rspec-core (2.6.4)
|
27
|
+
rspec-expectations (2.6.0)
|
28
|
+
diff-lcs (~> 1.1.2)
|
29
|
+
rspec-mocks (2.6.0)
|
30
|
+
sqlite3 (1.3.4)
|
31
|
+
sqlite3-ruby (1.3.3)
|
32
|
+
sqlite3 (>= 1.3.3)
|
33
|
+
tzinfo (0.3.29)
|
34
|
+
|
35
|
+
PLATFORMS
|
36
|
+
ruby
|
37
|
+
|
38
|
+
DEPENDENCIES
|
39
|
+
activerecord
|
40
|
+
rspec
|
41
|
+
sqlite3-ruby
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2006-2011 David Easley
|
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,39 @@
|
|
1
|
+
Fork changes
|
2
|
+
============
|
3
|
+
|
4
|
+
I modified the gem to work with Rails 3.1. This probably broke compatibility with Rails 3.0, but should still work fine with Rails 2.x.
|
5
|
+
|
6
|
+
Custom Error Message
|
7
|
+
====================
|
8
|
+
|
9
|
+
This plugin gives you the option to not have your custom validation error message
|
10
|
+
prefixed with the attribute name.
|
11
|
+
|
12
|
+
Rails 3 and Ruby 1.9
|
13
|
+
--------------------
|
14
|
+
|
15
|
+
Custom Error Message is Rails 3 and Ruby 1.9 compatible
|
16
|
+
|
17
|
+
Usage
|
18
|
+
-----
|
19
|
+
|
20
|
+
Sometimes generated error messages don't make sense.
|
21
|
+
|
22
|
+
validates_acceptance_of :accepted_terms, :message => 'Please accept the terms of service'
|
23
|
+
|
24
|
+
This generates the error message:
|
25
|
+
|
26
|
+
Accepted terms Please accept the terms of service
|
27
|
+
|
28
|
+
This plugin uses the carat (^) to omit the name of the attribute from error messages:
|
29
|
+
|
30
|
+
validates_acceptance_of :accepted_terms, :message => '^Please accept the terms of service'
|
31
|
+
|
32
|
+
This now generates:
|
33
|
+
|
34
|
+
Please accept the terms of service
|
35
|
+
|
36
|
+
CREDITS
|
37
|
+
-------
|
38
|
+
|
39
|
+
This plugin was originally written by David Easley (easleydp@gmail.com)
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_record'
|
3
|
+
require 'rubygems/package_task'
|
4
|
+
require 'rubygems/specification'
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
|
7
|
+
desc "Run all examples"
|
8
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
9
|
+
end
|
10
|
+
|
11
|
+
namespace :spec do
|
12
|
+
desc "Run all examples with RCov"
|
13
|
+
RSpec::Core::RakeTask.new(:rcov) do |t|
|
14
|
+
t.rcov = true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
desc 'Default: run unit tests.'
|
19
|
+
task :default => 'spec'
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'davekaro-custom_error_message'
|
5
|
+
s.version = "1.2.1.pre"
|
6
|
+
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["David Easley", "Jeremy Durham", "Dave Kroondyk"]
|
9
|
+
s.email = %q{davekaro@gmail.com}
|
10
|
+
s.homepage = 'http://github.com/davekaro/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/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'custom_error_message'
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
class Errors
|
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,25 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
class Error
|
3
|
+
|
4
|
+
protected
|
5
|
+
|
6
|
+
def generate_full_message(options = {})
|
7
|
+
keys = [
|
8
|
+
:"full_messages.#{@message}",
|
9
|
+
:'full_messages.format'
|
10
|
+
]
|
11
|
+
|
12
|
+
if self.message =~ /^\^/
|
13
|
+
keys.push('{{message}}')
|
14
|
+
|
15
|
+
options.merge!(:default => self.message[1..-1])
|
16
|
+
else
|
17
|
+
keys.push('%{attribute} %{message}')
|
18
|
+
|
19
|
+
options.merge!(:default => keys, :message => self.message)
|
20
|
+
end
|
21
|
+
|
22
|
+
I18n.translate(keys.shift, options)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rspec'
|
3
|
+
require 'active_record'
|
4
|
+
require 'custom_error_message'
|
5
|
+
|
6
|
+
class User < ActiveRecord::Base
|
7
|
+
has_many :user_roles
|
8
|
+
has_many :roles, :through => :user_roles
|
9
|
+
|
10
|
+
validates_presence_of :name
|
11
|
+
validates_presence_of :email, :message => "^Your email is invalid"
|
12
|
+
|
13
|
+
accepts_nested_attributes_for :roles
|
14
|
+
end
|
15
|
+
|
16
|
+
class Role < ActiveRecord::Base
|
17
|
+
has_many :user_roles
|
18
|
+
has_many :users, :through => :user_roles
|
19
|
+
|
20
|
+
validates_presence_of :role, :message => "^You must enter a role"
|
21
|
+
end
|
22
|
+
|
23
|
+
class UserRole < ActiveRecord::Base
|
24
|
+
belongs_to :role
|
25
|
+
belongs_to :user
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "error messages" 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
|
+
describe "with standard messages" do
|
35
|
+
it "should return a standard error message" do
|
36
|
+
@user = User.create
|
37
|
+
@user.errors.full_messages.should include "Name can't be blank"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "with custom messages" do
|
42
|
+
it "should return the full message specified" do
|
43
|
+
@user = User.create
|
44
|
+
@user.errors.full_messages.should include "Your email is invalid"
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "on nested attributes" do
|
48
|
+
it "should return the full message specified" do
|
49
|
+
@user = User.create(:roles_attributes => [{}])
|
50
|
+
@user.errors.full_messages.should include "You must enter a role"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
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 :name, :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
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: davekaro-custom_error_message
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.1.pre
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- David Easley
|
9
|
+
- Jeremy Durham
|
10
|
+
- Dave Kroondyk
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2011-09-28 00:00:00.000000000Z
|
15
|
+
dependencies: []
|
16
|
+
description: This plugin gives you the option to not have your custom validation error
|
17
|
+
message prefixed with the attribute name
|
18
|
+
email: davekaro@gmail.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- LICENSE
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- davekaro-custom_error_message.gemspec
|
30
|
+
- init.rb
|
31
|
+
- lib/custom_error_message.rb
|
32
|
+
- lib/rails/extensions/active_model.rb
|
33
|
+
- lib/rails/extensions/active_record.rb
|
34
|
+
- spec/custom_error_message_spec.rb
|
35
|
+
- spec/db/schema.rb
|
36
|
+
homepage: http://github.com/davekaro/custom-err-msg
|
37
|
+
licenses: []
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>'
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.3.1
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.8.10
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Custom Error Message plugin for Rails
|
60
|
+
test_files: []
|