Module: I18nScopes::TranslationUtils

Defined in:
lib/i18n_scopes/translation_utils.rb

Constant Summary collapse

CONTROLLER_SUFFIX =
"_controller"

Class Method Summary collapse

Class Method Details

.class_name(klass, options = {}) ⇒ Object

this method will return a underscored class_name

Note: plural_class and strip_controller_suffix will be used here

Params:

  • klass: the class on which the name should be get

  • options: options like plural_class and strip_controller_suffix



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/i18n_scopes/translation_utils.rb', line 24

def self.class_name(klass, options = {})
  class_name_options = options.merge(options)
  class_name = if class_name_options[:strip_controller_suffix] && klass.respond_to?(:controller_name)
    klass.controller_name
  else
    demodulized = ActiveSupport::Inflector.demodulize(klass.name) 
    ActiveSupport::Inflector.underscore(demodulized)
  end
  if class_name_options[:plural_class]
    ActiveSupport::Inflector.pluralize(class_name)
  else
    ActiveSupport::Inflector.singularize(class_name)
  end
end

.method_nameObject

returns the method name of the original caller



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/i18n_scopes/translation_utils.rb', line 40

def self.method_name
  found_scoped_t_at = nil
  caller.each_with_index do |call, index|
    if call.include?("scoped_translation")
      found_scoped_t_at = index
      break
    end
  end
  if found_scoped_t_at && call = caller[found_scoped_t_at + 1]
    if /^(.+?):(\d+)(?::in `(.*)')?/ =~ call
      return Regexp.last_match[3]
    end
  end
  raise "Failed to get method_name"
end

.modules(klass) ⇒ Object

this method will return all modules in a array, each module will be underscored

Params:

  • klass: the klass of which the modules should be used



11
12
13
14
15
# File 'lib/i18n_scopes/translation_utils.rb', line 11

def self.modules(klass)
  modules = ActiveSupport::Inflector.deconstantize(klass.name)
  modules = ActiveSupport::Inflector.underscore(modules)
  modules.split("::")
end