custom_error_message
Advanced tools
| # -*- encoding: utf-8 -*- | ||
| Gem::Specification.new do |s| | ||
| s.name = 'custom_error_message' | ||
| s.version = "1.1.0.pre2" | ||
| s.platform = Gem::Platform::RUBY | ||
| s.authors = ["David Easley", "Jeremy Durham"] | ||
| s.email = %q{jeremydurham@gmail.com} | ||
| s.homepage = 'http://github.com/jeremydurham/custom-err-msg' | ||
| s.summary = 'Custom Error Message plugin for Rails' | ||
| s.description = 'This plugin gives you the option to not have your custom validation error message prefixed with the attribute name' | ||
| s.rubygems_version = '>= 1.3.5' | ||
| s.files = `git ls-files`.split("\n") | ||
| s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") | ||
| s.require_paths = ["lib"] | ||
| end |
| module ActiveModel | ||
| class Errors < ActiveSupport::OrderedHash | ||
| def full_messages | ||
| full_messages = [] | ||
| each do |attribute, messages| | ||
| messages = Array.wrap(messages) | ||
| next if messages.empty? | ||
| if attribute == :base | ||
| messages.each {|m| full_messages << m } | ||
| else | ||
| attr_name = attribute.to_s.gsub('.', '_').humanize | ||
| attr_name = @base.class.human_attribute_name(attribute, :default => attr_name) | ||
| options = { :default => "%{attribute} %{message}", :attribute => attr_name } | ||
| messages.each do |m| | ||
| if m =~ /^\^/ | ||
| full_messages << I18n.t(:"errors.format.full_message", options.merge(:message => m[1..-1], :default => "%{message}")) | ||
| else | ||
| full_messages << I18n.t(:"errors.format", options.merge(:message => m)) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| full_messages | ||
| end | ||
| end | ||
| end |
| module ActiveRecord | ||
| class Error | ||
| protected | ||
| def generate_full_message(options = {}) | ||
| if self.message =~ /^\^/ | ||
| keys = ["{{message}}"] | ||
| options.merge!(:default => self.message[1..-1]) | ||
| end | ||
| I18n.translate(keys.shift, options) | ||
| end | ||
| end | ||
| end |
+20
| Copyright (c) 2006 David Easley | ||
| Permission is hereby granted, free of charge, to any person obtaining | ||
| a copy of this software and associated documentation files (the | ||
| "Software"), to deal in the Software without restriction, including | ||
| without limitation the rights to use, copy, modify, merge, publish, | ||
| distribute, sublicense, and/or sell copies of the Software, and to | ||
| permit persons to whom the Software is furnished to do so, subject to | ||
| the following conditions: | ||
| The above copyright notice and this permission notice shall be | ||
| included in all copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
| LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
| OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
| WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
+20
| require 'rubygems' | ||
| require 'active_record' | ||
| require 'rake/gempackagetask' | ||
| require 'rubygems/specification' | ||
| require 'rake/rdoctask' | ||
| require 'rspec/core/rake_task' | ||
| desc "Run all examples" | ||
| RSpec::Core::RakeTask.new(:spec) do |t| | ||
| end | ||
| namespace :spec do | ||
| desc "Run all examples with RCov" | ||
| RSpec::Core::RakeTask.new(:rcov) do |t| | ||
| t.rcov = true | ||
| end | ||
| end | ||
| desc 'Default: run unit tests.' | ||
| task :default => 'spec' |
| require 'rubygems' | ||
| require 'rspec' | ||
| require 'active_record' | ||
| class User < ActiveRecord::Base | ||
| has_many :user_roles | ||
| has_many :roles, :through => :user_roles | ||
| validates_presence_of :email, :message => "^Your email is invalid" | ||
| accepts_nested_attributes_for :roles | ||
| end | ||
| class Role < ActiveRecord::Base | ||
| has_many :user_roles | ||
| has_many :users, :through => :user_roles | ||
| validates_presence_of :role, :message => "^You must enter a role" | ||
| end | ||
| class UserRole < ActiveRecord::Base | ||
| belongs_to :role | ||
| belongs_to :user | ||
| end | ||
| require 'custom_error_message' | ||
| describe "validating attributes" do | ||
| before do | ||
| ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:") | ||
| load File.join(File.dirname(__FILE__), 'db', 'schema.rb') | ||
| end | ||
| it "should return the message specified without a prefix" do | ||
| @user = User.create | ||
| @user.errors.full_messages.should include "Your email is invalid" | ||
| end | ||
| describe "validating nested attributes" do | ||
| it "should return the message specified without a prefix" do | ||
| @user = User.create(:roles_attributes => [{}]) | ||
| @user.errors.full_messages.should include "You must enter a role" | ||
| end | ||
| end | ||
| end |
| ActiveRecord::Schema.define(:version => 0) do | ||
| create_table :users, :force => true do |t| | ||
| t.string :email, :password, :roles | ||
| end | ||
| create_table :roles, :force => true do |t| | ||
| t.string :role, :name | ||
| end | ||
| create_table :user_roles, :force => true do |t| | ||
| t.belongs_to :user, :role | ||
| end | ||
| end |
@@ -1,57 +0,5 @@ | ||
| module ActiveRecord | ||
| class Errors | ||
| def full_messages | ||
| full_messages = [] | ||
| @errors.each_key do |attr| | ||
| @errors[attr].each do |msg| | ||
| next if msg.nil? | ||
| msg = msg.respond_to?(:message) ? msg.message : msg | ||
| if attr == "base" | ||
| full_messages << msg | ||
| elsif msg =~ /^\^/ | ||
| full_messages << msg[1..-1] | ||
| elsif msg.is_a? Proc | ||
| full_messages << msg.call(@base) | ||
| else | ||
| full_messages << @base.class.human_attribute_name(attr) + " " + msg | ||
| end | ||
| end | ||
| end | ||
| full_messages | ||
| end | ||
| end | ||
| end | ||
| module ActiveModel | ||
| class Errors < ActiveSupport::OrderedHash | ||
| def full_messages | ||
| full_messages = [] | ||
| each do |attribute, messages| | ||
| messages = Array.wrap(messages) | ||
| next if messages.empty? | ||
| if attribute == :base | ||
| messages.each {|m| full_messages << m } | ||
| else | ||
| attr_name = attribute.to_s.gsub('.', '_').humanize | ||
| attr_name = @base.class.human_attribute_name(attribute, :default => attr_name) | ||
| options = { :default => "%{attribute} %{message}", :attribute => attr_name } | ||
| messages.each do |m| | ||
| if m =~ /^\^/ | ||
| full_messages << I18n.t(:"errors.format.full_message", options.merge(:message => m[1..-1], :default => "%{message}")) | ||
| else | ||
| full_messages << I18n.t(:"errors.format", options.merge(:message => m)) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| full_messages | ||
| end | ||
| end | ||
| end | ||
| if defined?(ActiveModel) | ||
| require 'rails/extensions/active_model' | ||
| else | ||
| require 'rails/extensions/active_record' | ||
| end |
Sorry, the diff of this file is not supported yet