daily pastebin goal
8%
SHARE
TWEET

Untitled

a guest Jun 18th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # config/initializers/errors_i18n.rb
  2. ActiveModel::Errors.module_eval do
  3.  
  4.   def full_messages
  5.     full_messages = []
  6.  
  7.     each do |attribute, messages|
  8.       messages = Array.wrap(messages)
  9.       next if messages.empty?
  10.  
  11.       if attribute == :base
  12.         messages.each {|m| full_messages << m }
  13.       else
  14.         # attr_name = attribute.to_s.gsub('.', '_').humanize
  15.         attr_name = convert_name(attribute.to_s)
  16.         attr_name ||= attribute.to_s.gsub('.', '_').humanize        
  17.         attr_name = @base.class.human_attribute_name(attribute, :default => attr_name)
  18.         options = { :default => "%{attribute} %{message}", :attribute => attr_name }
  19.  
  20.         messages.each do |m|
  21.           full_messages << I18n.t(:"errors.format", options.merge(:message => m))
  22.         end
  23.       end
  24.     end
  25.  
  26.     full_messages
  27.   end
  28.  
  29.   def to_text
  30.     output_string = ""
  31.     full_messages.each{|attr,msg| output_string << "#{attr} - #{msg}\n" }
  32.     output_string
  33.   end
  34.  
  35.  
  36.   def convert_name(name)
  37.     default = nil
  38.     if name =~ /(.+)\.(.+)/
  39.       base_name = $1
  40.       attribute = $2
  41.       # base = ActiveSupport::Dependencies.constantize(base_name.camelize)
  42.       base = ActiveSupport::Dependencies.constantize(base_name.camelize.singularize)
  43.       if (base.respond_to? :model_name)
  44.         default = "#{base.model_name.human} #{base.human_attribute_name(attribute.to_sym, :default => attribute.humanize)}"
  45.       end
  46.     end
  47.     default
  48.   end  
  49.  
  50. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top