2013-12-15 13:41:13 -05:00
|
|
|
require 'i18n'
|
|
|
|
|
2014-10-11 16:31:21 -04:00
|
|
|
I18n.load_path += Dir[
|
|
|
|
File.join(File.dirname(__FILE__), 'locale'.freeze, '*.yml'.freeze)
|
|
|
|
]
|
2011-03-30 20:31:39 -04:00
|
|
|
|
|
|
|
module Ransack
|
|
|
|
module Translate
|
|
|
|
def self.word(key, options = {})
|
2014-05-01 09:55:39 -04:00
|
|
|
I18n.translate(:"ransack.#{key}", :default => key.to_s)
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.predicate(key, options = {})
|
2014-05-01 09:55:39 -04:00
|
|
|
I18n.translate(:"ransack.predicates.#{key}", :default => key.to_s)
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.attribute(key, options = {})
|
|
|
|
unless context = options.delete(:context)
|
2011-08-13 16:36:40 -04:00
|
|
|
raise ArgumentError, "A context is required to translate attributes"
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
original_name = key.to_s
|
|
|
|
base_class = context.klass
|
2014-01-07 19:45:42 -05:00
|
|
|
base_ancestors = base_class.ancestors.select {
|
|
|
|
|x| x.respond_to?(:model_name)
|
|
|
|
}
|
2011-06-06 21:32:03 -04:00
|
|
|
predicate = Predicate.detect_from_string(original_name)
|
2014-10-09 17:48:38 -04:00
|
|
|
attributes_str = original_name
|
2014-11-21 14:03:20 -05:00
|
|
|
.sub(/_#{predicate}$/, Constants::EMPTY)
|
2011-03-30 20:31:39 -04:00
|
|
|
attribute_names = attributes_str.split(/_and_|_or_/)
|
|
|
|
combinator = attributes_str.match(/_and_/) ? :and : :or
|
|
|
|
defaults = base_ancestors.map do |klass|
|
2014-09-12 17:34:37 -04:00
|
|
|
"ransack.attributes.#{i18n_key(klass)}.#{original_name}".to_sym
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
|
2014-09-12 17:34:37 -04:00
|
|
|
translated_names = attribute_names.map do |name|
|
|
|
|
attribute_name(context, name, options[:include_associations])
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
|
2014-09-12 17:34:37 -04:00
|
|
|
interpolations = {
|
|
|
|
:attributes => translated_names.join(" #{Translate.word(combinator)} ")
|
|
|
|
}
|
2011-03-30 20:31:39 -04:00
|
|
|
|
|
|
|
if predicate
|
2014-09-12 17:34:37 -04:00
|
|
|
defaults << "%{attributes} %{predicate}".freeze
|
2011-03-30 20:31:39 -04:00
|
|
|
interpolations[:predicate] = Translate.predicate(predicate)
|
|
|
|
else
|
2014-09-12 17:34:37 -04:00
|
|
|
defaults << "%{attributes}".freeze
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
defaults << options.delete(:default) if options[:default]
|
2014-05-01 09:55:39 -04:00
|
|
|
options.reverse_merge! :count => 1, :default => defaults
|
2011-03-30 20:31:39 -04:00
|
|
|
I18n.translate(defaults.shift, options.merge(interpolations))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.association(key, options = {})
|
|
|
|
unless context = options.delete(:context)
|
|
|
|
raise ArgumentError, "A context is required to translate associations"
|
|
|
|
end
|
|
|
|
|
2014-09-12 17:34:37 -04:00
|
|
|
defaults =
|
|
|
|
if key.blank?
|
|
|
|
[:"#{context.klass.i18n_scope}.models.#{i18n_key(context.klass)}"]
|
|
|
|
else
|
|
|
|
[:"ransack.associations.#{i18n_key(context.klass)}.#{key}"]
|
|
|
|
end
|
2011-03-30 20:31:39 -04:00
|
|
|
defaults << context.traverse(key).model_name.human
|
2014-05-01 09:55:39 -04:00
|
|
|
options = { :count => 1, :default => defaults }
|
2011-03-30 20:31:39 -04:00
|
|
|
I18n.translate(defaults.shift, options)
|
|
|
|
end
|
|
|
|
|
2014-09-12 17:34:37 -04:00
|
|
|
private
|
2011-03-30 20:31:39 -04:00
|
|
|
|
|
|
|
def self.attribute_name(context, name, include_associations = nil)
|
2014-09-12 19:04:55 -04:00
|
|
|
@context, @name = context, name
|
2014-09-12 17:34:37 -04:00
|
|
|
@assoc_path = context.association_path(name)
|
2014-11-21 14:03:20 -05:00
|
|
|
@attr_name = @name.sub(/^#{@assoc_path}_/, Constants::EMPTY)
|
2014-09-12 17:34:37 -04:00
|
|
|
associated_class = @context.traverse(@assoc_path) if @assoc_path.present?
|
|
|
|
@include_associated = include_associations && associated_class
|
|
|
|
|
|
|
|
defaults = default_attribute_name << fallback_args
|
2014-05-01 09:55:39 -04:00
|
|
|
options = { :count => 1, :default => defaults }
|
2014-09-12 17:34:37 -04:00
|
|
|
interpolations = build_interpolations(associated_class)
|
|
|
|
|
2011-03-30 20:31:39 -04:00
|
|
|
I18n.translate(defaults.shift, options.merge(interpolations))
|
|
|
|
end
|
2014-01-01 12:53:51 -05:00
|
|
|
|
2014-09-12 17:34:37 -04:00
|
|
|
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),
|
2014-10-13 19:15:35 -04:00
|
|
|
:association_name => association_name
|
2014-09-12 17:34:37 -04:00
|
|
|
}
|
2014-10-13 19:15:35 -04:00
|
|
|
.reject { |_, value| value.nil? }
|
2014-09-12 17:34:37 -04:00
|
|
|
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)
|
2014-09-22 08:38:37 -04:00
|
|
|
key = "#{associated_class.i18n_scope}.attributes.#{
|
|
|
|
i18n_key(associated_class)}.#{@attr_name}"
|
|
|
|
["#{key}.one".to_sym, key.to_sym]
|
2014-09-12 17:34:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.translated_ancestor_attributes
|
|
|
|
@context.klass.ancestors
|
|
|
|
.select { |ancestor| ancestor.respond_to?(:model_name) }
|
|
|
|
.map { |ancestor| translated_attribute(ancestor) }
|
|
|
|
end
|
|
|
|
|
2014-01-01 12:53:51 -05:00
|
|
|
def self.i18n_key(klass)
|
2014-08-01 02:36:12 -04:00
|
|
|
# if ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 0
|
|
|
|
# klass.model_name.i18n_key.to_s.tr('.', '/')
|
|
|
|
# else
|
|
|
|
# klass.model_name.i18n_key.to_s
|
|
|
|
# end
|
|
|
|
raise "not implemented"
|
2014-01-01 12:53:51 -05:00
|
|
|
end
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
2013-03-01 15:29:40 -05:00
|
|
|
end
|