From f079ab476d3f21e5fb0cfc7ca1ff9ffe77073c8f Mon Sep 17 00:00:00 2001 From: jonatack Date: Wed, 1 Jan 2014 18:53:04 +0100 Subject: [PATCH] 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 --- lib/ransack/translate.rb | 32 ++++++++++++++++---------------- spec/ransack/translate_spec.rb | 16 ++++++++++++++++ spec/support/en.yml | 8 +++++++- spec/support/schema.rb | 6 ++++++ 4 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 spec/ransack/translate_spec.rb diff --git a/lib/ransack/translate.rb b/lib/ransack/translate.rb index 569bdc8..24abc2d 100644 --- a/lib/ransack/translate.rb +++ b/lib/ransack/translate.rb @@ -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 diff --git a/spec/ransack/translate_spec.rb b/spec/ransack/translate_spec.rb new file mode 100644 index 0000000..0a4d181 --- /dev/null +++ b/spec/ransack/translate_spec.rb @@ -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 diff --git a/spec/support/en.yml b/spec/support/en.yml index 8d39c74..57de6af 100644 --- a/spec/support/en.yml +++ b/spec/support/en.yml @@ -4,4 +4,10 @@ en: ransack: attributes: person: - name: Full Name \ No newline at end of file + name: Full Name + activerecord: + attributes: + namespace/article: + title: AR Namespaced Title + namespace_article: + title: Old Ransack Namespaced Title diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 46e82a2..edd4449 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -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