Fixes issue #273 - translations for namespaced models.

ActiveRecord uses i18n_key methods which return
"namespace/model_name"("namespace.model_name" in rails 3.0.x) for
Namespace::ModelName. Ransack should used the same pattern.

Adapted from original commit by github.com/Bonias
This commit is contained in:
jonatack 2014-01-01 18:53:51 +01:00
parent a87d145eab
commit 8abd7fdd80
4 changed files with 45 additions and 17 deletions

View File

@ -26,7 +26,7 @@ module Ransack
attribute_names = attributes_str.split(/_and_|_or_/)
combinator = attributes_str.match(/_and_/) ? :and : :or
defaults = base_ancestors.map do |klass|
:"ransack.attributes.#{klass.model_name.singular}.#{original_name}"
:"ransack.attributes.#{i18n_key(klass)}.#{original_name}"
end
translated_names = attribute_names.map do |attr|
@ -54,12 +54,11 @@ module Ransack
raise ArgumentError, "A context is required to translate associations"
end
defaults = key.blank? ? [:"#{context.klass.i18n_scope}.models.#{
context.klass.model_name.singular}"] : [
:"ransack.associations.#{context.klass.model_name.singular
}.#{key}"]
defaults = key.blank? ?
[:"#{context.klass.i18n_scope}.models.#{i18n_key(context.klass)}"] :
[:"ransack.associations.#{i18n_key(context.klass)}.#{key}"]
defaults << context.traverse(key).model_name.human
options = {count: 1, default: defaults}
options = { count: 1, default: defaults }
I18n.translate(defaults.shift, options)
end
@ -71,22 +70,19 @@ module Ransack
attr_name = name.sub(/^#{assoc_path}_/, '')
interpolations = {}
interpolations[:attr_fallback_name] = I18n.translate(
(associated_class ?
:"ransack.attributes.#{associated_class.model_name.singular}.#{attr_name}" :
:"ransack.attributes.#{context.klass.model_name.singular}.#{attr_name}"
),
:"ransack.attributes.#{
i18n_key(associated_class || context.klass)
}.#{attr_name}",
default: [
(associated_class ?
:"#{associated_class.i18n_scope}.attributes.#{associated_class.model_name.singular}.#{attr_name}" :
:"#{context.klass.i18n_scope}.attributes.#{context.klass.model_name.singular}.#{attr_name}"
:"#{associated_class.i18n_scope}.attributes.#{i18n_key(associated_class)}.#{attr_name}" :
:"#{context.klass.i18n_scope}.attributes.#{i18n_key(context.klass)}.#{attr_name}"
),
:".attributes.#{attr_name}",
attr_name.humanize
]
)
defaults = [
:"ransack.attributes.#{context.klass.model_name.singular}.#{name}"
]
defaults = [:"ransack.attributes.#{i18n_key(context.klass)}.#{name}"]
if include_associations && associated_class
defaults << '%{association_name} %{attr_fallback_name}'
interpolations[:association_name] = association(
@ -96,8 +92,12 @@ module Ransack
else
defaults << '%{attr_fallback_name}'
end
options = {count: 1, default: defaults}
options = { count: 1, default: defaults }
I18n.translate(defaults.shift, options.merge(interpolations))
end
def self.i18n_key(klass)
klass.model_name.i18n_key
end
end
end

View File

@ -0,0 +1,16 @@
require 'spec_helper'
module Ransack
describe Translate do
describe '.attribute' do
it 'translate namespaced attribute like AR does' do
ar_translation = ::Namespace::Article.human_attribute_name(:title)
ransack_translation = Ransack::Translate.attribute(
:title, :context => ::Namespace::Article.search.context
)
ransack_translation.should eq ar_translation
end
end
end
end

View File

@ -4,4 +4,10 @@ en:
ransack:
attributes:
person:
name: Full Name
name: Full Name
activerecord:
attributes:
namespace/article:
title: AR Namespaced Title
namespace_article:
title: Old Ransack Namespaced Title

View File

@ -66,6 +66,12 @@ class Article < ActiveRecord::Base
has_many :notes, as: :notable
end
module Namespace
class Article < ::Article
end
end
class Comment < ActiveRecord::Base
belongs_to :article
belongs_to :person