Skip to content

Commit

Permalink
Better workaround for ActiveRecord's flawed Base.human_name:
Browse files Browse the repository at this point in the history
* affecting commit buttons
* in 0.9.6 and earlier we called @object.class.human_name, but this resulted in things like "Userpost" instead of "User post"
* in 0.9.7 we called @object.class.name.underscore.humanize
* 0.9.6 worked for people with their own i18n for model names (human_name falls back to "Userpost" if there's no i18n)
* 0.9.7 worked for those without i18n ("User post" instead of "Userpost")
* the crappy fallback in human_name is the issue we're routing around (@object.class.name.humanize)
* so we now call @object.class.name.underscore.humanize only if it looks like human_name has fallen back to the crappy @object.class.name.humanize
  • Loading branch information
justinfrench committed Mar 1, 2010
1 parent 511509c commit 6ca9d44
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/formtastic.rb
Expand Up @@ -314,7 +314,15 @@ def commit_button(*args)

if @object && @object.respond_to?(:new_record?)
key = @object.new_record? ? :create : :update
object_name = @object.class.name.underscore.humanize # TODO Rails Bug: should be able to call @object.class.human_name, see http://github.com/justinfrench/formtastic/issues/#issue/153 revisit for Rails 3

# Deal with some complications with ActiveRecord::Base.human_name and two name models (eg UserPost)
# ActiveRecord::Base.human_name falls back to ActiveRecord::Base.name.humanize ("Userpost")
# if there's no i18n, which is pretty crappy. In this circumstance we want to detect this
# fall back (human_name == name.humanize) and do our own thing name.underscore.humanize ("User Post")
object_human_name = @object.class.human_name # default is UserPost => "Userpost", but i18n may do better ("User post")
crappy_human_name = @object.class.name.humanize # UserPost => "Userpost"
decent_human_name = @object.class.name.underscore.humanize # UserPost => "User post"
object_name = (object_human_name == crappy_human_name) ? decent_human_name : object_human_name
else
key = :submit
object_name = @object_name.to_s.send(@@label_str_method)
Expand Down
57 changes: 53 additions & 4 deletions spec/commit_button_spec.rb
Expand Up @@ -121,9 +121,6 @@
end

describe 'label' do
before do
::Post.stub!(:human_name).and_return('Post')
end

# No object
describe 'when used without object' do
Expand Down Expand Up @@ -351,7 +348,7 @@
describe 'when the model is two words' do
before do
output_buffer = ''
class ::UserPost; def id; end; end
class ::UserPost; def id; end; def self.human_name; "Userpost"; end; end # Rails does crappy human_name
@new_user_post = ::UserPost.new

@new_user_post.stub!(:new_record?).and_return(true)
Expand All @@ -366,4 +363,56 @@ class ::UserPost; def id; end; end

end

# describe 'with i18n' do
#
# before do
# ::I18n.backend.store_translations :en, :activerecord => {
# :models => {
# :post => {
# :one => 'Article',
# :many => 'Articles',
# :other => "{{count}} Articles" },
# :big_post => {
# :one => 'Feature Article',
# :many => 'Feature Articles',
# :other => "{{count}} Feature Articles" } } }
# #@new_post.class.stub!(:human_name => I18n.t("activerecord.models.post.one"))
# end
#
# after do
# ::I18n.backend.store_translations :en, :models => nil
# end
#
# it 'should translate a single word model' do
# output_buffer.replace ''
# semantic_form_for(@new_post) do |builder|
# concat(builder.commit_button())
# end
#
# output_buffer.should have_tag('input[@value="Create Article"]')
# end
#
# it 'should translate a multiple word model' do
#
# end
#
#
# #before do
# # output_buffer = ''
# # class ::UserPost; def id; end; end
# # @new_user_post = ::UserPost.new
# #
# # @new_user_post.stub!(:new_record?).and_return(true)
# # semantic_form_for(@new_user_post, :url => '') do |builder|
# # concat(builder.commit_button())
# # end
# #end
# #
# #it "should render the string as the value of the button" do
# # output_buffer.should have_tag('li input[@value="Create User post"]')
# #end
#
# end
#

end

0 comments on commit 6ca9d44

Please sign in to comment.