From e714b499cc1f7ebc84f8d0e96607b79e60f2828d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 20 Oct 2009 22:20:01 -0200 Subject: Move validator, human_name and human_attribute_name to ActiveModel, remove deprecated error messages and add i18n_scope and lookup_ancestors. Signed-off-by: Carl Lerche --- activerecord/lib/active_record.rb | 1 - activerecord/lib/active_record/base.rb | 32 ++-------- activerecord/lib/active_record/validations.rb | 89 --------------------------- activerecord/lib/active_record/validator.rb | 68 -------------------- 4 files changed, 5 insertions(+), 185 deletions(-) delete mode 100644 activerecord/lib/active_record/validator.rb (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 88becfb482..8195e78826 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -71,7 +71,6 @@ module ActiveRecord autoload :Timestamp, 'active_record/timestamp' autoload :Transactions, 'active_record/transactions' autoload :Types, 'active_record/types' - autoload :Validator, 'active_record/validator' autoload :Validations, 'active_record/validations' module AttributeMethods diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 4274df54cc..4e6090458a 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1386,7 +1386,8 @@ module ActiveRecord #:nodoc: subclasses.each { |klass| klass.reset_inheritable_attributes; klass.reset_column_information } end - def self_and_descendants_from_active_record#nodoc: + # Set the lookup ancestors for ActiveModel. + def lookup_ancestors #:nodoc: klass = self classes = [klass] while klass != klass.base_class @@ -1400,32 +1401,9 @@ module ActiveRecord #:nodoc: [self] end - # Transforms attribute key names into a more humane format, such as "First name" instead of "first_name". Example: - # Person.human_attribute_name("first_name") # => "First name" - # This used to be deprecated in favor of humanize, but is now preferred, because it automatically uses the I18n - # module now. - # Specify +options+ with additional translating options. - def human_attribute_name(attribute_key_name, options = {}) - defaults = self_and_descendants_from_active_record.map do |klass| - :"#{klass.name.underscore}.#{attribute_key_name}" - end - defaults << options[:default] if options[:default] - defaults.flatten! - defaults << attribute_key_name.to_s.humanize - options[:count] ||= 1 - I18n.translate(defaults.shift, options.merge(:default => defaults, :scope => [:activerecord, :attributes])) - end - - # Transform the modelname into a more humane format, using I18n. - # By default, it will underscore then humanize the class name (BlogPost.human_name #=> "Blog post"). - # Default scope of the translation is activerecord.models - # Specify +options+ with additional translating options. - def human_name(options = {}) - defaults = self_and_descendants_from_active_record.map do |klass| - :"#{klass.name.underscore}" - end - defaults << self.name.underscore.humanize - I18n.translate(defaults.shift, {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options)) + # Set the i18n scope to overwrite ActiveModel. + def i18n_scope #:nodoc: + :activerecord end # True if this isn't a concrete subclass needing a STI type condition. diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index e61b253192..0365cb592f 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -17,90 +17,6 @@ module ActiveRecord end end - class Errors < ActiveModel::Errors - class << self - def default_error_messages - message = "Errors.default_error_messages has been deprecated. Please use I18n.translate('activerecord.errors.messages')." - ActiveSupport::Deprecation.warn(message) - - I18n.translate 'activerecord.errors.messages' - end - end - - # Returns all the full error messages in an array. - # - # class Company < ActiveRecord::Base - # validates_presence_of :name, :address, :email - # validates_length_of :name, :in => 5..30 - # end - # - # company = Company.create(:address => '123 First St.') - # company.errors.full_messages # => - # ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"] - def full_messages(options = {}) - 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 = @base.class.human_attribute_name(attribute.to_s) - prefix = attr_name + I18n.t('activerecord.errors.format.separator', :default => ' ') - messages.each do |m| - full_messages << "#{prefix}#{m}" - end - end - end - - full_messages - end - - # Translates an error message in it's default scope (activerecord.errrors.messages). - # Error messages are first looked up in models.MODEL.attributes.ATTRIBUTE.MESSAGE, if it's not there, - # it's looked up in models.MODEL.MESSAGE and if that is not there it returns the translation of the - # default message (e.g. activerecord.errors.messages.MESSAGE). The translated model name, - # translated attribute name and the value are available for interpolation. - # - # When using inheritance in your models, it will check all the inherited models too, but only if the model itself - # hasn't been found. Say you have class Admin < User; end and you wanted the translation for the :blank - # error +message+ for the title +attribute+, it looks for these translations: - # - #
    - #
  1. activerecord.errors.models.admin.attributes.title.blank
  2. - #
  3. activerecord.errors.models.admin.blank
  4. - #
  5. activerecord.errors.models.user.attributes.title.blank
  6. - #
  7. activerecord.errors.models.user.blank
  8. - #
  9. activerecord.errors.messages.blank
  10. - #
  11. any default you provided through the +options+ hash (in the activerecord.errors scope)
  12. - #
- def generate_message(attribute, message = :invalid, options = {}) - message, options[:default] = options[:default], message if options[:default].is_a?(Symbol) - - defaults = @base.class.self_and_descendants_from_active_record.map do |klass| - [ :"models.#{klass.name.underscore}.attributes.#{attribute}.#{message}", - :"models.#{klass.name.underscore}.#{message}" ] - end - - defaults << options.delete(:default) - defaults = defaults.compact.flatten << :"messages.#{message}" - - key = defaults.shift - value = @base.respond_to?(attribute) ? @base.send(attribute) : nil - - options = { :default => defaults, - :model => @base.class.human_name, - :attribute => @base.class.human_attribute_name(attribute.to_s), - :value => value, - :scope => [:activerecord, :errors] - }.merge(options) - - I18n.translate(key, options) - end - end - module Validations extend ActiveSupport::Concern @@ -165,11 +81,6 @@ module ActiveRecord errors.empty? end - - # Returns the Errors object that holds all information about attribute error messages. - def errors - @errors ||= Errors.new(self) - end end end end diff --git a/activerecord/lib/active_record/validator.rb b/activerecord/lib/active_record/validator.rb deleted file mode 100644 index 83a33f4dcd..0000000000 --- a/activerecord/lib/active_record/validator.rb +++ /dev/null @@ -1,68 +0,0 @@ -module ActiveRecord #:nodoc: - - # A simple base class that can be used along with ActiveRecord::Base.validates_with - # - # class Person < ActiveRecord::Base - # validates_with MyValidator - # end - # - # class MyValidator < ActiveRecord::Validator - # def validate - # if some_complex_logic - # record.errors[:base] = "This record is invalid" - # end - # end - # - # private - # def some_complex_logic - # # ... - # end - # end - # - # Any class that inherits from ActiveRecord::Validator will have access to record, - # which is an instance of the record being validated, and must implement a method called validate. - # - # class Person < ActiveRecord::Base - # validates_with MyValidator - # end - # - # class MyValidator < ActiveRecord::Validator - # def validate - # record # => The person instance being validated - # options # => Any non-standard options passed to validates_with - # end - # end - # - # To cause a validation error, you must add to the record's errors directly - # from within the validators message - # - # class MyValidator < ActiveRecord::Validator - # def validate - # record.errors[:base] << "This is some custom error message" - # record.errors[:first_name] << "This is some complex validation" - # # etc... - # end - # end - # - # To add behavior to the initialize method, use the following signature: - # - # class MyValidator < ActiveRecord::Validator - # def initialize(record, options) - # super - # @my_custom_field = options[:field_name] || :first_name - # end - # end - # - class Validator - attr_reader :record, :options - - def initialize(record, options) - @record = record - @options = options - end - - def validate - raise "You must override this method" - end - end -end -- cgit v1.2.3