mintdigital-fancy_errors 1.0.0

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/README.rdoc ADDED
@@ -0,0 +1,54 @@
1
+ = Fancy Errors
2
+
3
+ == Description
4
+
5
+ Define the whole error message for an attribute by prefixing it with '^':
6
+
7
+ validates_acceptance_of :terms, :message => "^Terms must be accepted."
8
+ @user.errors.full_messages #=> ['Terms must be accepted']
9
+
10
+ Order your error messages:
11
+
12
+ validates_presence_of :title, :content
13
+ @post.errors.full_messages(:order => [:title, :content])
14
+ #=> ["Title can't be blank", "Content can't be blank"]
15
+
16
+ Get the full messages for a specific attribute:
17
+
18
+ validates_presence_of :title, :message => "^Can has title?"
19
+ validates_uniqueness_of :title, :message => 'must be unique'
20
+ @post.errors.full_messages_on(:title)
21
+ #=> ['Can has title?', 'Title must be unique']
22
+
23
+ == Installation
24
+
25
+ sudo gem install mintdigital-fancy_errors
26
+
27
+ == Usage
28
+
29
+ config.gem 'mintdigital-fancy_errors', :lib => 'fancy_errors'
30
+
31
+ == License
32
+
33
+ Copyright (c) 2009 Dean Strelau, Mint Digital
34
+
35
+ Permission is hereby granted, free of charge, to any person
36
+ obtaining a copy of this software and associated documentation
37
+ files (the "Software"), to deal in the Software without
38
+ restriction, including without limitation the rights to use,
39
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
40
+ copies of the Software, and to permit persons to whom the
41
+ Software is furnished to do so, subject to the following
42
+ conditions:
43
+
44
+ The above copyright notice and this permission notice shall be
45
+ included in all copies or substantial portions of the Software.
46
+
47
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
49
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
51
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
52
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
53
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
54
+ OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require 'rake/gempackagetask'
5
+ spec = Gem::Specification.new do |s|
6
+ s.name = 'fancy_errors'
7
+ s.version = '1.0.0'
8
+ s.has_rdoc = false
9
+ s.extra_rdoc_files = %w(README.rdoc)
10
+ s.rdoc_options = %w(--main README.rdoc)
11
+ s.summary = "Make your ActiveRecord::Errors fancy"
12
+ s.author = 'Dean Strelau'
13
+ s.email = 'dean@mintdigital.com'
14
+ s.homepage = 'http://mintdigital.github.com/fancy_errors'
15
+ s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib,test,rails}/**/*")
16
+
17
+ s.add_development_dependency('jeremymcanally-context', '> 0.5.0')
18
+ s.add_development_dependency('mhennemeyer-matchy', '> 0.3.0')
19
+ end
20
+
21
+ Rake::GemPackageTask.new(spec) do |pkg|
22
+ pkg.gem_spec = spec
23
+ end
24
+
25
+ require 'rake/testtask'
26
+ Rake::TestTask.new(:test) do |test|
27
+ test.libs << 'lib' << 'test'
28
+ test.pattern = 'test/**/*_test.rb'
29
+ test.verbose = false
30
+ end
31
+
32
+ begin
33
+ require 'rcov/rcovtask'
34
+ Rcov::RcovTask.new do |t|
35
+ t.libs << "test"
36
+ t.test_files = FileList['test/*_test.rb']
37
+ t.verbose = true
38
+ end
39
+ rescue
40
+ task :rcov do
41
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
42
+ end
43
+ end
44
+
45
+ desc 'Generate the gemspec to serve this Gem from Github'
46
+ task :gemspec do
47
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
48
+ File.open(file, 'w') {|f| f << spec.to_ruby }
49
+ puts "Created gemspec: #{file}"
50
+ end
51
+
52
+
53
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ class FancyErrors < ActiveRecord::Errors
2
+ def full_messages(options = {})
3
+ if options[:order] && order = options[:order].map {|a| a.to_s }
4
+ # add attrs specified in order
5
+ ordered_attrs = order & @errors.keys
6
+ ordered_attrs += @errors.keys - order
7
+ end
8
+
9
+ (ordered_attrs||@errors.keys).inject([]) do |full_messages, a|
10
+ full_messages.concat(full_messages_on(a))
11
+ end
12
+ end
13
+
14
+ def full_messages_on(attribute)
15
+ (@errors[attribute.to_s] || []).inject([]) do |messages, msg|
16
+ if attribute.to_s == 'base'
17
+ messages << msg
18
+ elsif msg =~ /^\^/
19
+ messages << msg[1..-1]
20
+ else
21
+ attr_name = @base.class.human_attribute_name(attribute)
22
+ messages << attr_name + I18n.t('activerecord.errors.format.separator', :default => ' ') + msg
23
+ end
24
+ end
25
+ end
26
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,7 @@
1
+ module ::ActiveRecord
2
+ module Validations
3
+ def errors
4
+ @errors ||= FancyErrors.new(self)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,77 @@
1
+ require 'helper'
2
+
3
+ class FancyErrorsTest < Test::Unit::TestCase
4
+ def setup
5
+ @errors = FancyErrors.new(Base.new)
6
+ end
7
+
8
+ context "#full_messages" do
9
+ it "returns just the message when it starts with ^ " do
10
+ @errors.add(:title, '^That title sucks')
11
+ @errors.full_messages.should == ['That title sucks']
12
+ end
13
+
14
+ it "does not change messages that don't start with ^" do
15
+ @errors.add(:title, "can't be blank")
16
+ @errors.full_messages.should == ["Title can't be blank"]
17
+ end
18
+
19
+ it "can mix and match" do
20
+ @errors.add_to_base('You fail')
21
+ @errors.add(:title, '^That title sucks')
22
+ @errors.add(:title, "can't be blank")
23
+ ['You fail', "That title sucks", "Title can't be blank"].each do |err|
24
+ @errors.full_messages.should include(err)
25
+ end
26
+ end
27
+
28
+ it "returns ordered errors for object when given an order key" do
29
+ [:title, :snippet, :content].each do |attribute|
30
+ @errors.add(attribute, "can't be blank")
31
+ end
32
+ @errors.full_messages(:order => [:snippet, :title, :content]).should == [
33
+ "Snippet can't be blank",
34
+ "Title can't be blank",
35
+ "Content can't be blank"
36
+ ]
37
+ end
38
+
39
+ it "excludes ordering attributes that don't have errors" do
40
+ @errors.add(:title, "can't be blank")
41
+ @errors.full_messages(:order => [:snippet, :title, :content]).
42
+ should == ["Title can't be blank"]
43
+ end
44
+ end
45
+
46
+ context "#full_messages_on" do
47
+ it "returns empty if no error on that attribute" do
48
+ @errors.full_messages_on(:title).should == []
49
+ end
50
+
51
+ it "returns just the message when it starts with ^ " do
52
+ @errors.add(:title, '^That title sucks')
53
+ @errors.full_messages_on(:title).should == ['That title sucks']
54
+ end
55
+
56
+ it "does not change messages that don't start with ^" do
57
+ @errors.add(:title, "can't be blank")
58
+ @errors.full_messages_on(:title).should == ["Title can't be blank"]
59
+ end
60
+
61
+ it "works with errors on base" do
62
+ @errors.add_to_base('You fail')
63
+ @errors.full_messages_on(:base).should == ['You fail']
64
+ end
65
+ end
66
+
67
+ it "allows full, ordered messages" do
68
+ @errors.add(:snippet, "must be less than 160 characters")
69
+ @errors.add(:content, "^You must have content")
70
+ @errors.add(:title, "can't be blank")
71
+ @errors.full_messages(:order => [:title, :snippet, :content]).should == [
72
+ "Title can't be blank",
73
+ "Snippet must be less than 160 characters",
74
+ "You must have content"
75
+ ]
76
+ end
77
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'context'
4
+ require 'matchy'
5
+
6
+ begin
7
+ require 'redgreen'
8
+ rescue LoadError
9
+ end
10
+
11
+ # Define this error so Validations can be loaded without Base
12
+ class ActiveRecordError < StandardError; end
13
+ require 'i18n'
14
+ require 'active_record/validations'
15
+
16
+ require File.join(File.dirname(__FILE__), '../lib/fancy_errors')
17
+
18
+ # Mock out an AR::Base class
19
+ class Base
20
+ attr_accessor :title, :snippet, :content
21
+ class << self
22
+ def human_attribute_name(a); a.to_s.capitalize; end
23
+ end
24
+ end
25
+
26
+ module Kernel
27
+ def silence_warnings
28
+ old_verbose, $VERBOSE = $VERBOSE, nil
29
+ yield
30
+ ensure
31
+ $VERBOSE = old_verbose
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mintdigital-fancy_errors
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dean Strelau
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-02 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: jeremymcanally-context
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.5.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: mhennemeyer-matchy
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.0
34
+ version:
35
+ description:
36
+ email: dean@mintdigital.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ files:
44
+ - README.rdoc
45
+ - Rakefile
46
+ - lib/fancy_errors.rb
47
+ - test/fancy_errors_test.rb
48
+ - test/helper.rb
49
+ - rails/init.rb
50
+ has_rdoc: false
51
+ homepage: http://mintdigital.github.com/fancy_errors
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --main
55
+ - README.rdoc
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.2.0
74
+ signing_key:
75
+ specification_version: 2
76
+ summary: Make your ActiveRecord::Errors fancy
77
+ test_files: []
78
+