Skip to content

Commit

Permalink
Brought error handling up to speed with respect to I18n. Fixed a bug …
Browse files Browse the repository at this point in the history
…introduced by Rails 2.3.4 at the same time.
  • Loading branch information
Steven Luscher authored and danielharan committed Oct 4, 2009
1 parent 4c0d8c8 commit 11930d3
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
29 changes: 26 additions & 3 deletions lib/active_presenter/base.rb
Expand Up @@ -36,12 +36,35 @@ def self.presents(*types)
end
end

def self.human_attribute_name(attribute_name)
def self.human_attribute_name(attribute_key_name, options = {})
presentable_type = presented.keys.detect do |type|
attribute_name.to_s.starts_with?("#{type}_")
attribute_key_name.to_s.starts_with?("#{type}_") || attribute_key_name.to_s == type.to_s
end
attribute_key_name_without_class = attribute_key_name.to_s.gsub("#{presentable_type}_", "")

attribute_name.to_s.gsub("#{presentable_type}_", "").humanize
if presented[presentable_type] and attribute_key_name_without_class != presentable_type.to_s
presented[presentable_type].human_attribute_name(attribute_key_name_without_class, options)
else
I18n.translate(presentable_type, options.merge(:default => presentable_type.to_s.humanize, :scope => [:activerecord, :models]))
end
end

# Since ActivePresenter does not descend from ActiveRecord, we need to
# mimic some ActiveRecord behavior in order for the ActiveRecord::Errors
# object we're using to work properly.
#
# This problem was introduced with Rails 2.3.4.
# Fix courtesy http://gist.github.com/191263
def self.self_and_descendants_from_active_record # :nodoc:
[self]
end

def self.human_name(options = {}) # :nodoc:
defaults = self_and_descendants_from_active_record.map do |klass|
:"#{klass.name.underscore}"
end
defaults << self.name.humanize
I18n.translate(defaults.shift, {:scope => [:activerecord, :models], :count => 1, :default => defaults}.merge(options))
end

# Accepts arguments in two forms. For example, if you had a SignupPresenter that presented User, and Account, you could specify arguments in the following two forms:
Expand Down
34 changes: 33 additions & 1 deletion test/base_test.rb
Expand Up @@ -69,12 +69,44 @@
s.errors.on(:user_login)
end

expect ['User Password can not be blank'] do
expect "can't be blank" do
s = SignupPresenter.new
s.valid?
s.errors.on(:user_login)
end

expect ["User Password can't be blank"] do
s = SignupPresenter.new(:user_login => 'login')
s.valid?
s.errors.full_messages
end

expect 'c4N n07 83 8L4nK' do
old_locale = I18n.locale
I18n.locale = '1337'

s = SignupPresenter.new(:user_login => nil)
s.valid?
message = s.errors.on(:user_login)

I18n.locale = old_locale

message
end

expect ['U53R pa22w0rD c4N n07 83 8L4nK'] do
old_locale = I18n.locale
I18n.locale = '1337'

s = SignupPresenter.new(:user_login => 'login')
s.valid?
message = s.errors.full_messages

I18n.locale = old_locale

message
end

expect ActiveRecord::Base.to.receive(:transaction) do
s = SignupPresenter.new
s.save
Expand Down
21 changes: 20 additions & 1 deletion test/test_helper.rb
Expand Up @@ -8,6 +8,21 @@
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.logger.level = Logger::WARN

I18n.backend.store_translations '1337',
:activerecord => {
:models => {
:user => 'U53R'
},
:attributes => {
:user => {:password => 'pa22w0rD'}
},
:errors => {
:messages => {
:blank => 'c4N n07 83 8L4nK'
}
}
}

ActiveRecord::Schema.define(:version => 0) do
create_table :users do |t|
t.boolean :admin, :default => false
Expand Down Expand Up @@ -37,7 +52,11 @@ class User < ActiveRecord::Base
attr_accessor :password_confirmation

def presence_of_password
errors.add_to_base('Password can not be blank') if password.blank?
if password.blank?
attribute_name = I18n.t(:password, {:default => "Password", :scope => [:activerecord, :attributes, :user]})
error_message = I18n.t(:blank, {:default => "can't be blank", :scope => [:activerecord, :errors, :messages]})
errors.add_to_base("#{attribute_name} #{error_message}")
end
end
end
class Account < ActiveRecord::Base; end
Expand Down

0 comments on commit 11930d3

Please sign in to comment.