Refactor Ransack::Translate, esp. the attribute_name
class method, hopefully for better readability and maintainability. All these class methods make me wonder if `attribute_name` shouldn’t be in a separate class with instance methods instead.
This commit is contained in:
parent
70d2d23181
commit
8752f75d79
|
@ -27,23 +27,22 @@ module Ransack
|
|||
attribute_names = attributes_str.split(/_and_|_or_/)
|
||||
combinator = attributes_str.match(/_and_/) ? :and : :or
|
||||
defaults = base_ancestors.map do |klass|
|
||||
:"ransack.attributes.#{i18n_key(klass)}.#{original_name}"
|
||||
"ransack.attributes.#{i18n_key(klass)}.#{original_name}".to_sym
|
||||
end
|
||||
|
||||
translated_names = attribute_names.map do |attr|
|
||||
attribute_name(context, attr, options[:include_associations])
|
||||
translated_names = attribute_names.map do |name|
|
||||
attribute_name(context, name, options[:include_associations])
|
||||
end
|
||||
|
||||
interpolations = {}
|
||||
interpolations[:attributes] = translated_names.join(
|
||||
" #{Translate.word(combinator)} "
|
||||
)
|
||||
interpolations = {
|
||||
:attributes => translated_names.join(" #{Translate.word(combinator)} ")
|
||||
}
|
||||
|
||||
if predicate
|
||||
defaults << "%{attributes} %{predicate}"
|
||||
defaults << "%{attributes} %{predicate}".freeze
|
||||
interpolations[:predicate] = Translate.predicate(predicate)
|
||||
else
|
||||
defaults << "%{attributes}"
|
||||
defaults << "%{attributes}".freeze
|
||||
end
|
||||
|
||||
defaults << options.delete(:default) if options[:default]
|
||||
|
@ -56,55 +55,96 @@ module Ransack
|
|||
raise ArgumentError, "A context is required to translate associations"
|
||||
end
|
||||
|
||||
defaults = key.blank? ?
|
||||
[:"#{context.klass.i18n_scope}.models.#{i18n_key(context.klass)}"] :
|
||||
[:"ransack.associations.#{i18n_key(context.klass)}.#{key}"]
|
||||
defaults =
|
||||
if key.blank?
|
||||
[:"#{context.klass.i18n_scope}.models.#{i18n_key(context.klass)}"]
|
||||
else
|
||||
[:"ransack.associations.#{i18n_key(context.klass)}.#{key}"]
|
||||
end
|
||||
defaults << context.traverse(key).model_name.human
|
||||
options = { :count => 1, :default => defaults }
|
||||
I18n.translate(defaults.shift, options)
|
||||
end
|
||||
|
||||
private
|
||||
private
|
||||
|
||||
def self.attribute_name(context, name, include_associations = nil)
|
||||
assoc_path = context.association_path(name)
|
||||
associated_class = context.traverse(assoc_path) if assoc_path.present?
|
||||
attr_name = name.sub(/^#{assoc_path}_/, '')
|
||||
interpolations = {}
|
||||
interpolations[:attr_fallback_name] = I18n.translate(
|
||||
:"ransack.attributes.#{
|
||||
i18n_key(associated_class || context.klass)
|
||||
}.#{attr_name}",
|
||||
:default => [
|
||||
(
|
||||
if associated_class
|
||||
:"#{associated_class.i18n_scope}.attributes.#{
|
||||
i18n_key(associated_class)}.#{attr_name}"
|
||||
else
|
||||
context.klass.ancestors.select do |k|
|
||||
k.respond_to?(:model_name)
|
||||
end.collect do |k|
|
||||
:"#{k.i18n_scope}.attributes.#{i18n_key(k)}.#{attr_name}"
|
||||
end
|
||||
end
|
||||
),
|
||||
:".attributes.#{attr_name}",
|
||||
attr_name.humanize
|
||||
].flatten
|
||||
)
|
||||
defaults = [:"ransack.attributes.#{i18n_key(context.klass)}.#{name}"]
|
||||
if include_associations && associated_class
|
||||
defaults << '%{association_name} %{attr_fallback_name}'
|
||||
interpolations[:association_name] = association(
|
||||
assoc_path, :context => context
|
||||
)
|
||||
else
|
||||
defaults << '%{attr_fallback_name}'
|
||||
end
|
||||
@context, @name, @include_associations = context, name
|
||||
@assoc_path = context.association_path(name)
|
||||
@attr_name = @name.sub(/^#{@assoc_path}_/, '')
|
||||
associated_class = @context.traverse(@assoc_path) if @assoc_path.present?
|
||||
@include_associated = include_associations && associated_class
|
||||
|
||||
defaults = default_attribute_name << fallback_args
|
||||
options = { :count => 1, :default => defaults }
|
||||
interpolations = build_interpolations(associated_class)
|
||||
|
||||
I18n.translate(defaults.shift, options.merge(interpolations))
|
||||
end
|
||||
|
||||
def self.default_attribute_name
|
||||
["ransack.attributes.#{i18n_key(@context.klass)}.#{@name}".to_sym]
|
||||
end
|
||||
|
||||
def self.fallback_args
|
||||
if @include_associated
|
||||
'%{association_name} %{attr_fallback_name}'.freeze
|
||||
else
|
||||
'%{attr_fallback_name}'.freeze
|
||||
end
|
||||
end
|
||||
|
||||
def self.build_interpolations(associated_class)
|
||||
{
|
||||
:attr_fallback_name => attr_fallback_name(associated_class),
|
||||
:association_name => association_name
|
||||
}
|
||||
.compact
|
||||
end
|
||||
|
||||
def self.attr_fallback_name(associated_class)
|
||||
I18n.t(
|
||||
:"ransack.attributes.#{fallback_class(associated_class)}.#{@attr_name}",
|
||||
:default => default_interpolation(associated_class)
|
||||
)
|
||||
end
|
||||
|
||||
def self.fallback_class(associated_class)
|
||||
i18n_key(associated_class || @context.klass)
|
||||
end
|
||||
|
||||
def self.association_name
|
||||
association(@assoc_path, :context => @context) if @include_associated
|
||||
end
|
||||
|
||||
def self.default_interpolation(associated_class)
|
||||
[
|
||||
associated_attribute(associated_class),
|
||||
".attributes.#{@attr_name}".to_sym,
|
||||
@attr_name.humanize
|
||||
]
|
||||
.flatten
|
||||
end
|
||||
|
||||
def self.associated_attribute(associated_class)
|
||||
if associated_class
|
||||
translated_attribute(associated_class)
|
||||
else
|
||||
translated_ancestor_attributes
|
||||
end
|
||||
end
|
||||
|
||||
def self.translated_attribute(associated_class)
|
||||
"#{associated_class.i18n_scope}.attributes.#{
|
||||
i18n_key(associated_class)}.#{@attr_name}".to_sym
|
||||
end
|
||||
|
||||
def self.translated_ancestor_attributes
|
||||
@context.klass.ancestors
|
||||
.select { |ancestor| ancestor.respond_to?(:model_name) }
|
||||
.map { |ancestor| translated_attribute(ancestor) }
|
||||
end
|
||||
|
||||
def self.i18n_key(klass)
|
||||
if ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 0
|
||||
klass.model_name.i18n_key.to_s.tr('.', '/')
|
||||
|
|
Loading…
Reference in New Issue