diff options
author | José Valim <jose.valim@gmail.com> | 2009-10-20 22:20:01 -0200 |
---|---|---|
committer | Carl Lerche <carllerche@mac.com> | 2009-10-20 17:52:32 -0700 |
commit | e714b499cc1f7ebc84f8d0e96607b79e60f2828d (patch) | |
tree | a9725eb2101a56db366603ca849a3ed260b710d6 /activemodel/lib | |
parent | 4f6d6f7031a88b647814fc0154e6b69b636dc912 (diff) | |
download | rails-e714b499cc1f7ebc84f8d0e96607b79e60f2828d.tar.gz rails-e714b499cc1f7ebc84f8d0e96607b79e60f2828d.tar.bz2 rails-e714b499cc1f7ebc84f8d0e96607b79e60f2828d.zip |
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 <carllerche@mac.com>
Diffstat (limited to 'activemodel/lib')
-rw-r--r-- | activemodel/lib/active_model.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 19 | ||||
-rw-r--r-- | activemodel/lib/active_model/naming.rb | 9 | ||||
-rw-r--r-- | activemodel/lib/active_model/translation.rb | 59 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations.rb | 1 | ||||
-rw-r--r-- | activemodel/lib/active_model/validator.rb | 68 |
6 files changed, 144 insertions, 14 deletions
diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb index 67f529262d..505e16c195 100644 --- a/activemodel/lib/active_model.rb +++ b/activemodel/lib/active_model.rb @@ -39,8 +39,10 @@ module ActiveModel autoload :Serialization, 'active_model/serialization' autoload :StateMachine, 'active_model/state_machine' autoload :TestCase, 'active_model/test_case' + autoload :Translation, 'active_model/translation' autoload :Validations, 'active_model/validations' autoload :ValidationsRepairHelper, 'active_model/validations_repair_helper' + autoload :Validator, 'active_model/validator' autoload :VERSION, 'active_model/version' module Serializers diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 7a48960f89..e8bb62953d 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -93,7 +93,7 @@ module ActiveModel # 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 = {}) + def full_messages full_messages = [] each do |attribute, messages| @@ -103,8 +103,10 @@ module ActiveModel if attribute == :base messages.each {|m| full_messages << m } else - attr_name = attribute.to_s.humanize - prefix = attr_name + I18n.t('activemodel.errors.format.separator', :default => ' ') + attr_name = @base.class.human_attribute_name(attribute) + options = { :default => ' ', :scope => @base.class.i18n_scope } + prefix = attr_name + I18n.t(:"errors.format.separator", options) + messages.each do |m| full_messages << "#{prefix}#{m}" end @@ -135,10 +137,7 @@ module ActiveModel def generate_message(attribute, message = :invalid, options = {}) message, options[:default] = options[:default], message if options[:default].is_a?(Symbol) - klass_ancestors = [@base.class] - klass_ancestors += @base.class.ancestors.reject {|x| x.is_a?(Module)} - - defaults = klass_ancestors.map do |klass| + defaults = @base.class.lookup_ancestors.map do |klass| [ :"models.#{klass.name.underscore}.attributes.#{attribute}.#{message}", :"models.#{klass.name.underscore}.#{message}" ] end @@ -150,10 +149,10 @@ module ActiveModel value = @base.send(:read_attribute_for_validation, attribute) options = { :default => defaults, - :model => @base.class.name.humanize, - :attribute => attribute.to_s.humanize, + :model => @base.class.model_name.human, + :attribute => @base.class.human_attribute_name(attribute), :value => value, - :scope => [:activemodel, :errors] + :scope => [@base.class.i18n_scope, :errors] }.merge(options) I18n.translate(key, options) diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index b8c2a367b4..675d62b9a6 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -5,12 +5,13 @@ module ActiveModel attr_reader :singular, :plural, :element, :collection, :partial_path, :human alias_method :cache_key, :collection - def initialize(name) - super + def initialize(klass, name) + super(name) + @klass = klass @singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze @plural = ActiveSupport::Inflector.pluralize(@singular).freeze @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze - @human = @element.gsub(/_/, " ") + @human = ActiveSupport::Inflector.humanize(@element).freeze @collection = ActiveSupport::Inflector.tableize(self).freeze @partial_path = "#{@collection}/#{@element}".freeze end @@ -20,7 +21,7 @@ module ActiveModel # Returns an ActiveModel::Name object for module. It can be # used to retrieve all kinds of naming-related information. def model_name - @_model_name ||= ActiveModel::Name.new(name) + @_model_name ||= ActiveModel::Name.new(self, name) end end end diff --git a/activemodel/lib/active_model/translation.rb b/activemodel/lib/active_model/translation.rb new file mode 100644 index 0000000000..dc11198c66 --- /dev/null +++ b/activemodel/lib/active_model/translation.rb @@ -0,0 +1,59 @@ +module ActiveModel + module Translation + include ActiveModel::Naming + + # Returns the i18n_scope for the class. Overwrite if you want custom lookup. + def i18n_scope + :activemodel + end + + # When localizing a string, goes through the lookup returned by this method. + # Used in ActiveModel::Name#human, ActiveModel::Errors#full_messages and + # ActiveModel::Translation#human_attribute_name. + def lookup_ancestors + self.ancestors.select { |x| x.respond_to?(:model_name) } + end + + # Transforms attributes names into a more human format, such as "First name" instead of "first_name". + # + # Example: + # + # Person.human_attribute_name("first_name") # => "First name" + # + # Specify +options+ with additional translating options. + def human_attribute_name(attribute, options = {}) + defaults = lookup_ancestors.map do |klass| + :"#{klass.model_name.underscore}.#{attribute}" + end + + defaults << options.delete(:default) if options[:default] + defaults << attribute.to_s.humanize + + options.reverse_merge! :scope => [self.i18n_scope, :attributes], :count => 1, :default => defaults + I18n.translate(defaults.shift, options) + end + + # Model.human_name is deprecated. Use Model.model_name.human instead. + def human_name(*args) + ActiveSupport::Deprecation.warn("human_name has been deprecated, please use model_name.human instead", caller[0,1]) + model_name.human(*args) + end + end + + class Name < String + # Transform the model name into a more humane format, using I18n. By default, + # it will underscore then humanize the class name (BlogPost.human_name #=> "Blog post"). + # Specify +options+ with additional translating options. + def human(options={}) + defaults = @klass.lookup_ancestors.map do |klass| + klass.model_name.underscore.to_sym + end + + defaults << options.delete(:default) if options[:default] + defaults << @human + + options.reverse_merge! :scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults + I18n.translate(defaults.shift, options) + end + end +end diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 0f178a07c8..064ec98f3a 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -7,6 +7,7 @@ module ActiveModel include ActiveSupport::Callbacks included do + extend ActiveModel::Translation define_callbacks :validate, :scope => :name end diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb new file mode 100644 index 0000000000..09de72b757 --- /dev/null +++ b/activemodel/lib/active_model/validator.rb @@ -0,0 +1,68 @@ +module ActiveModel #:nodoc: + + # A simple base class that can be used along with ActiveModel::Base.validates_with + # + # class Person < ActiveModel::Base + # validates_with MyValidator + # end + # + # class MyValidator < ActiveModel::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 ActiveModel::Validator will have access to <tt>record</tt>, + # which is an instance of the record being validated, and must implement a method called <tt>validate</tt>. + # + # class Person < ActiveModel::Base + # validates_with MyValidator + # end + # + # class MyValidator < ActiveModel::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 <tt>record<tt>'s errors directly + # from within the validators message + # + # class MyValidator < ActiveModel::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 < ActiveModel::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 |