Module: Ransack::Translate

Defined in:
lib/ransack/translate.rb,
lib/ransack/adapters/mongoid/ransack/translate.rb,
lib/ransack/adapters/active_record/ransack/translate.rb

Class Method Summary collapse

Class Method Details

.association(key, options = {}) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ransack/translate.rb', line 55

def self.association(key, options = {})
  unless context = options.delete(:context)
    raise ArgumentError, "A context is required to translate associations"
  end

  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

.attribute(key, options = {}) ⇒ Object


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
47
48
49
50
51
52
53
# File 'lib/ransack/translate.rb', line 17

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}$/, Constants::EMPTY)
  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}".to_sym
  end

  translated_names = attribute_names.map do |name|
    attribute_name(context, name, options[:include_associations])
  end

  interpolations = {
    :attributes => translated_names.join(" #{Translate.word(combinator)} ")
  }

  if predicate
    defaults << "%{attributes} %{predicate}".freeze
    interpolations[:predicate] = Translate.predicate(predicate)
  else
    defaults << "%{attributes}".freeze
  end

  defaults << options.delete(:default) if options[:default]
  options.reverse_merge! :count => 1, :default => defaults
  I18n.translate(defaults.shift, options.merge(interpolations))
end

.i18n_key(klass) ⇒ Object


151
152
153
154
155
156
157
158
# File 'lib/ransack/translate.rb', line 151

def self.i18n_key(klass)
  # 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"
end

.predicate(key, options = {}) ⇒ Object


13
14
15
# File 'lib/ransack/translate.rb', line 13

def self.predicate(key, options = {})
  I18n.translate(:"ransack.predicates.#{key}", :default => key.to_s)
end

.word(key, options = {}) ⇒ Object


9
10
11
# File 'lib/ransack/translate.rb', line 9

def self.word(key, options = {})
  I18n.translate(:"ransack.#{key}", :default => key.to_s)
end