How to use full_messages method of ClassMethods Package

Best Active_mocker_ruby code snippet using ClassMethods.full_messages

resource.rb

Source:resource.rb Github

copy

Full Screen

...233 # Wraps an error message into a full_message format.234 #235 # The default full_message format for any locale is <tt>"{{attribute}} {{message}}"</tt>.236 # One can specify locale specific default full_message format by storing it as a237 # translation for the key <tt>:"activerecord.errors.full_messages.format"</tt>.238 #239 # Additionally one can specify a validation specific error message format by240 # storing a translation for <tt>:"activerecord.errors.full_messages.[message_key]"</tt>.241 # E.g. the full_message format for any validation that uses :blank as a message242 # key (such as validates_presence_of) can be stored to <tt>:"activerecord.errors.full_messages.blank".</tt>243 #244 # Because the message key used by a validation can be overwritten on the245 # <tt>validates_*</tt> class macro level one can customize the full_message format for246 # any particular validation:247 #248 # # app/models/article.rb249 # class Article < ActiveRecord::Base250 # validates_presence_of :title, :message => :"title.blank"251 # end252 #253 # # config/locales/en.yml254 # en:255 # activerecord:256 # errors:257 # full_messages:258 # title:259 # blank: This title is screwed!260 def generate_full_message(message, options = {})261 options.reverse_merge! :message => self.message,262 :model => @base.class.human_name,263 :attribute => @base.class.human_attribute_name(attribute.to_s),264 :value => value265 key = :"full_messages.#{@message}"266 defaults = [:'full_messages.format', '{{attribute}} {{message}}']267 I18n.t(key, options.merge(:default => defaults, :scope => [:activerecord, :errors]))268 end269end270# Active Record validation is reported to and from this object, which is used by Base#save to271# determine whether the object is in a valid state to be saved. See usage example in Validations.272class Errors273 include Enumerable274 class << self275 def default_error_messages276 ActiveSupport::Deprecation.warn("ActiveRecord::Errors.default_error_messages has been deprecated. Please use I18n.translate('activerecord.errors.messages').")277 I18n.translate 'activerecord.errors.messages'278 end279 end280 def initialize(base) # :nodoc:281 @base, @errors = base, {}282 end283 # Adds an error to the base object instead of any particular attribute. This is used284 # to report errors that don't tie to any specific attribute, but rather to the object285 # as a whole. These error messages don't get prepended with any field name when iterating286 # with +each_full+, so they should be complete sentences.287 def add_to_base(msg)288 add(:base, msg)289 end290 # Adds an error message (+messsage+) to the +attribute+, which will be returned on a call to <tt>on(attribute)</tt>291 # for the same attribute and ensure that this error object returns false when asked if <tt>empty?</tt>. More than one292 # error can be added to the same +attribute+ in which case an array will be returned on a call to <tt>on(attribute)</tt>.293 # If no +messsage+ is supplied, :invalid is assumed.294 # If +message+ is a Symbol, it will be translated, using the appropriate scope (see translate_error).295 # def add(attribute, message = nil, options = {})296 # message ||= :invalid297 # message = generate_message(attribute, message, options)) if message.is_a?(Symbol)298 # @errors[attribute.to_s] ||= []299 # @errors[attribute.to_s] << message300 # end301 def add(error_or_attr, message = nil, options = {})302 error, attribute = error_or_attr.is_a?(Error) ? [error_or_attr, error_or_attr.attribute] : [nil, error_or_attr]303 options[:message] = options.delete(:default) if options.has_key?(:default)304 @errors[attribute.to_s] ||= []305 @errors[attribute.to_s] << (error || Error.new(@base, attribute, message, options))306 end307 # Will add an error message to each of the attributes in +attributes+ that is empty.308 def add_on_empty(attributes, custom_message = nil)309 for attr in [attributes].flatten310 value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]311 is_empty = value.respond_to?(:empty?) ? value.empty? : false312 add(attr, :empty, :default => custom_message) unless !value.nil? && !is_empty313 end314 end315 # Will add an error message to each of the attributes in +attributes+ that is blank (using Object#blank?).316 def add_on_blank(attributes, custom_message = nil)317 for attr in [attributes].flatten318 value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]319 add(attr, :blank, :default => custom_message) if value.blank?320 end321 end322 # Returns true if the specified +attribute+ has errors associated with it.323 #324 # class Company < ActiveRecord::Base325 # validates_presence_of :name, :address, :email326 # validates_length_of :name, :in => 5..30327 # end328 #329 # company = Company.create(:address => '123 First St.')330 # company.errors.invalid?(:name) # => true331 # company.errors.invalid?(:address) # => false332 def invalid?(attribute)333 !@errors[attribute.to_s].nil?334 end335 # Returns +nil+, if no errors are associated with the specified +attribute+.336 # Returns the error message, if one error is associated with the specified +attribute+.337 # Returns an array of error messages, if more than one error is associated with the specified +attribute+.338 #339 # class Company < ActiveRecord::Base340 # validates_presence_of :name, :address, :email341 # validates_length_of :name, :in => 5..30342 # end343 #344 # company = Company.create(:address => '123 First St.')345 # company.errors.on(:name) # => ["is too short (minimum is 5 characters)", "can't be blank"]346 # company.errors.on(:email) # => "can't be blank"347 # company.errors.on(:address) # => nil348 def on(attribute)349 attribute = attribute.to_s350 return nil unless @errors.has_key?(attribute)351 errors = @errors[attribute].map(&:to_s)352 errors.size == 1 ? errors.first : errors353 end354 alias :[] :on355 # Returns errors assigned to the base object through +add_to_base+ according to the normal rules of <tt>on(attribute)</tt>.356 def on_base357 on(:base)358 end359 # Yields each attribute and associated message per error added.360 #361 # class Company < ActiveRecord::Base362 # validates_presence_of :name, :address, :email363 # validates_length_of :name, :in => 5..30364 # end365 #366 # company = Company.create(:address => '123 First St.')367 # company.errors.each{|attr,msg| puts "#{attr} - #{msg}" }368 # # => name - is too short (minimum is 5 characters)369 # # name - can't be blank370 # # address - can't be blank371 def each372 @errors.each_key { |attr| @errors[attr].each { |error| yield attr, error.message } }373 end374 def each_error375 @errors.each_key { |attr| @errors[attr].each { |error| yield attr, error } }376 end377 # Yields each full error message added. So <tt>Person.errors.add("first_name", "can't be empty")</tt> will be returned378 # through iteration as "First name can't be empty".379 #380 # class Company < ActiveRecord::Base381 # validates_presence_of :name, :address, :email382 # validates_length_of :name, :in => 5..30383 # end384 #385 # company = Company.create(:address => '123 First St.')386 # company.errors.each_full{|msg| puts msg }387 # # => Name is too short (minimum is 5 characters)388 # # Name can't be blank389 # # Address can't be blank390 def each_full391 full_messages.each { |msg| yield msg }392 end393 # Returns all the full error messages in an array.394 #395 # class Company < ActiveRecord::Base396 # validates_presence_of :name, :address, :email397 # validates_length_of :name, :in => 5..30398 # end399 #400 # company = Company.create(:address => '123 First St.')401 # company.errors.full_messages # =>402 # ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"]403 def full_messages(options = {})404 @errors.values.inject([]) do |full_messages, errors|405 full_messages + errors.map { |error| error.full_message }406 end407 end408 # Returns true if no errors have been added.409 def empty?410 @errors.empty?411 end412 # Removes all errors that have been added.413 def clear414 @errors = {}415 end416 # Returns the total number of errors added. Two errors added to the same attribute will be counted as such.417 def size418 @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size }419 end420 alias_method :count, :size421 alias_method :length, :size422 # Returns an XML representation of this error object.423 #424 # class Company < ActiveRecord::Base425 # validates_presence_of :name, :address, :email426 # validates_length_of :name, :in => 5..30427 # end428 #429 # company = Company.create(:address => '123 First St.')430 # company.errors.to_xml431 # # => <?xml version="1.0" encoding="UTF-8"?>432 # # <errors>433 # # <error>Name is too short (minimum is 5 characters)</error>434 # # <error>Name can't be blank</error>435 # # <error>Address can't be blank</error>436 # # </errors>437 def to_xml(options={})438 options[:root] ||= "errors"439 options[:indent] ||= 2440 options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])441 options[:builder].instruct! unless options.delete(:skip_instruct)442 options[:builder].errors do |e|443 full_messages.each { |msg| e.error(msg) }444 end445 end446 def generate_message(attribute, message = :invalid, options = {})447 ActiveSupport::Deprecation.warn("ActiveRecord::Errors#generate_message has been deprecated. Please use ActiveRecord::Error#generate_message.")448 Error.new(@base, attribute, message, options).to_s449 end450end...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Active_mocker_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful