13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/ransack/translate.rb', line 13
def self.attribute(key, options = {})
unless context = options.delete(:context)
raise ArgumentError, "A context is required to translate attributes"
end
original_name = key.to_s
base_class = context.klass
base_ancestors = base_class.ancestors.select { |x| x.respond_to?(:model_name) }
predicate = Predicate.detect_from_string(original_name)
attributes_str = original_name.sub(/_#{predicate}$/, '')
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.underscore}.#{original_name}"
end
translated_names = attribute_names.map do |attr|
attribute_name(context, attr, options[:include_associations])
end
interpolations = {}
interpolations[:attributes] = translated_names.join(" #{Translate.word(combinator)} ")
if predicate
defaults << "%{attributes} %{predicate}"
interpolations[:predicate] = Translate.predicate(predicate)
else
defaults << "%{attributes}"
end
defaults << options.delete(:default) if options[:default]
options.reverse_merge! :count => 1, :default => defaults
I18n.translate(defaults.shift, options.merge(interpolations))
end
|