Allows you to easily overwrite the translation of object_name in resource_controller by adding a method called translated_object_name. #419

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
3 participants
Contributor

citrus commented Jun 7, 2011

By adding the translated_object_name method, users and extension developers can easily avoid errors that would otherwise be caused by resource_controller looking up the object_name in a translation file.

For example, If I store my models attributes in my en.yml like so: (and I often do)

en: 
  post:
    title: Title
    posted_at: Posted At
    body: Body
    ...

Then resource controller would try to make the flash message out of a hash in any of the actions:

def update
  invoke_callbacks(:update, :before)
  if @object.update_attributes(params[object_name])
    invoke_callbacks(:update, :after)

    # object_name in this case is 'post'
    resource_desc = I18n.t(object_name)

    resource_desc += " \"#{@object.name}\"" if @object.respond_to?(:name)
    flash[:notice] = I18n.t(:successfully_updated, :resource => resource_desc)
    respond_with(@object) do |format|
      format.html { redirect_to location_after_save }
      format.js   { render :layout => false }
    end
  else
    invoke_callbacks(:update, :fails)
    respond_with(@object)
  end
end

This raises a NoMethod error:

NoMethodError in Admin::Blog::PostsController#update

undefined method `+' for #Hash:0x000001067aac40

After this patch, I could easily avoid this error by adding this method and the appropriate translation to my en.yml:

def translated_object_name
  I18n.t("post.model_name")
end

Thanks!

Owner

schof commented Jun 8, 2011

@romul I'll let you review this one since you use i18n more than me

Contributor

citrus commented Jun 8, 2011

thanks for checking this out @schof

just fyi, I spoke about this with @BDQ earlier on IRC..

Member

romul commented Jun 8, 2011

@citrus: First of all you have I18n error in your en.yml. Correct option should be

en: 
  activerecord:
    attributes:
      post:
        title: Title
        posted_at: Posted At
        body: Body

Although in any case I'll improve flash message generation tomorrow, but in more general manner. Thanks for pointing this issue.

Contributor

citrus commented Jun 8, 2011

sure @romul .. whatever makes you happy...

Contributor

citrus commented Jun 8, 2011

Maybe we should look beyond 'my errors' and examine the underlying issue here... there is no way to customize the translation for the flash message, regardless of how your en.yml is set up...

Member

romul commented Jun 8, 2011

Yup, I plan to handle precisely the underlying issue.

@romul romul closed this in 4619b41 Jun 9, 2011

Contributor

citrus commented Jun 9, 2011

cool thanks @romul

Contributor

citrus commented Jun 9, 2011

I'm a bit confused though.. What if we don't want to use object.class.model_name.human as our resource descriptor? Say I have a model called BlogComment or something.. but want the flash message just to say Comment... and maybe something else in a different language?

Basically your solution fixes the error but doesn't allow for any customization. Can we please add a translated_object_name method?

def translated_object_name
  object.class.model_name.human
end
Member

romul commented Jun 10, 2011

Basically your solution fixes the error but doesn't allow for any customization.

You can customize model name via locales:

en:
  activerecord:
    models:
      blog_comment:
        one: Comment

Besides, you can even override model_name method (it should returns instance of ActiveModel::Name) in your model to handle extraordinary cases.
Besides, if you want fully custom notices for some controllers, you can override whole flash_message_for method.

romul added a commit to romul/spree that referenced this pull request Jun 29, 2011

Improved generation of flash notices in admin panel and extracted to …
…separate method to allow easily override it for different locales.


fixes #419

jordan-brough added a commit to jordan-brough/spree that referenced this pull request Jul 15, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment