Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Namespacesd Models and model_name.human/human_attribute_for #39

Closed
wants to merge 1 commit into from

2 participants

@starpeak

I'm just localizing Humpyard and as it has namespaced models like Humpyard::Elements::TextElement it would be in the I18n yml:

activerecord:
  models:
    humpyard/elements/text_element: 'Text Element'
  attributes:
    humpyard/elements/text_element:
      content: 'Content'
      display_from: 'Display from'

This ist not very nice to read and I would have to redefine common fields like "display_from", which exists for all Humpyard::Elements:: models.
So I wrote a patch where the namespaced model translation could be in a namespaced yml/format:

activerecord:
  models:
    humpyard:
      elements:
        text_element: 'Text element'
  attributes:
    humpyard:
      elements:
        display_from: 'Display from'
        text_element:
          content: 'Content'

Note the fallback to 'activerecord.attributes.humpyard.elements.display_from for the common field. This is of course a simple example - when you have many Humpyard::Pages::* and Humpyard::Elements::* this syntax makes much more sense than it may look like. ;-)

CU
StarPeak

@starpeak starpeak Lookup I18n human_name and human_attribute_name for namespaced models…
… in namespaced I18n translations for ActiveModels; Fallback to namespace attributes on human_attribute_name for ActiveModels;
4066ac0
@starpeak

Ok - should have had a deeper look into the tickets :-)

The patch is much more improved than mine - but misses the fallback to module's attribute_name lookup.
As I found out there are often the same attributes inside a module I think it would be a good idea to implement it in that patch.
If you would like I would implement that into the patch from the ticket 5572.

@josevalim
Owner

Please do provide another patch in that ticket. You can consider that your patch will be applied after the one already attached there.

@starpeak

Ok - I than would expand the given patch later the day as I have to go to a customer now. I think it will be in lighthouse in about 10 hours.
The code is almost there (I think I could take the loop from this commit using the i18n_key function) and I would even write a test for it this time. :-)

@josevalim
Owner

Awesome! And writing a test is your only option! ;)

@starpeak

I took the option to write tests and pushed changes here as well as as a patch on lighthouse in the corresponding bug.

Also the tests from the patch were not running as PersonModule::Person.new does not take argument_hash - so I fixed that, too.

And github is telling me, that it only lasted 8 hours to publish the patch, not 10 as estimated. ;-)

@starpeak

Hi

Did you see the patch in https://rails.lighthouseapp.com/projects/8994/tickets/5572 ?
I just ask because you didn't respond and I assume that you just lost track on the ticket :)

Sven

@josevalim
Owner

Yes, I am aware of the changes in the ticket! I just need to find sometime to discuss it with other core members as I am not sure if we want to make the errors messages lookup slower than it already is today.

@starpeak

For me it is more important to have the fallback on attribute names than on error messages - I just did the error messages part to complete the fallback cases.
Error messages are either the standard values for the type, could easily be set to non default I18n-Stings in validations (e.g. the :cannot_be_before_display_from error in the PublishRangeValidator in Humpyard) or are model specific in the most cases.

@nhocki nhocki referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@vijaydev vijaydev referenced this pull request from a commit
@nhocki nhocki Alert about the new Bundler require for asset gems
If you are coming from a Rails 3.0 application, you won't have
the correct Bundler require statement.

This will cause the gems under the `assets` group not to be available
in the development and production environment.

I think this is related to the issue #39 in rails-sass
rails/sass-rails#39
0b151a4
@andhapp andhapp referenced this pull request from a commit in andhapp/rails
@nhocki nhocki Alert about the new Bundler require for asset gems
If you are coming from a Rails 3.0 application, you won't have
the correct Bundler require statement.

This will cause the gems under the `assets` group not to be available
in the development and production environment.

I think this is related to the issue #39 in rails-sass
rails/sass-rails#39
94ed1e2
@kalys kalys referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@kalys kalys referenced this pull request from a commit in kalys/rails
@kalys kalys &#39 dates back to SGML when &#x27 was introduced in HTML 4.0 fcca118
@kalys kalys referenced this pull request from a commit in kalys/rails
@kalys kalys &#39 dates back to SGML when &#x27 was introduced in HTML 4.0 6b2a24c
@kennyj kennyj referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@ttosch ttosch referenced this pull request from a commit
@nhocki nhocki Alert about the new Bundler require for asset gems
If you are coming from a Rails 3.0 application, you won't have
the correct Bundler require statement.

This will cause the gems under the `assets` group not to be available
in the development and production environment.

I think this is related to the issue #39 in rails-sass
rails/sass-rails#39
41aef11
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 20, 2010
  1. @starpeak

    Lookup I18n human_name and human_attribute_name for namespaced models…

    starpeak authored
    … in namespaced I18n translations for ActiveModels; Fallback to namespace attributes on human_attribute_name for ActiveModels;
This page is out of date. Refresh to see the latest.
View
2  activemodel/lib/active_model/naming.rb
@@ -31,7 +31,7 @@ def human(options={})
@klass.respond_to?(:i18n_scope)
defaults = @klass.lookup_ancestors.map do |klass|
- klass.model_name.underscore.to_sym
+ klass.model_name.underscore.gsub('/','.').to_sym
end
defaults << options.delete(:default) if options[:default]
View
9 activemodel/lib/active_model/translation.rb
@@ -43,8 +43,13 @@ def lookup_ancestors
#
# Specify +options+ with additional translating options.
def human_attribute_name(attribute, options = {})
- defaults = lookup_ancestors.map do |klass|
- :"#{self.i18n_scope}.attributes.#{klass.model_name.underscore}.#{attribute}"
+ defaults = []
+ lookup_ancestors.each do |klass|
+ name = klass.model_name.underscore.split('/')
+ while name.size > 0
+ defaults << :"#{self.i18n_scope}.attributes.#{name * '.'}.#{attribute}"
+ name.pop
+ end
end
defaults << :"attributes.#{attribute}"
Something went wrong with that request. Please try again.