1
0
Fork 0
mirror of https://github.com/activerecord-hackery/ransack.git synced 2022-11-09 13:47:45 -05:00

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:04 +01:00
parent fa5b400b55
commit f079ab476d
4 changed files with 45 additions and 17 deletions

View file

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