Created
August 20, 2010 02:08
-
-
Save cheald/539446 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Validatable | |
def errors | |
@errors ||= Validatable::Errors.new(self) | |
end | |
class ValidationBase #:nodoc: | |
def try_translation(key) | |
begin | |
::I18n.translate!(key, :scope => "activerecord") | |
rescue ::I18n::ArgumentError | |
nil | |
end | |
end | |
def message_with_i18n(instance) | |
event = self.class.to_s.demodulize.snake_case | |
model = instance.class.to_s.gsub("::", ".").snake_case | |
try_translation("errors.models.%s.attributes.%s.%s" % [model, @attribute, event]) || | |
try_translation("errors.models.%s.%s" % [model, event]) || | |
try_translation("errors.messages.%s" % event) || | |
message_without_i18n(instance) | |
end | |
alias_method_chain :message, :i18n | |
end | |
class Errors | |
def initialize(owner) | |
@owner = owner | |
@owner_key = owner.class.name.snake_case.gsub("::", ".") | |
end | |
def humanize_with_i18n(lower_case_and_underscored_word) #:nodoc: | |
key = "attributes.#{@owner_key}.#{lower_case_and_underscored_word}" | |
begin | |
::I18n.translate!(key, :scope => "activerecord") | |
rescue ::I18n::ArgumentError | |
humanize_without_i18n(lower_case_and_underscored_word) | |
end | |
end | |
alias_method_chain :humanize, :i18n | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment