Class: SimpleHelpers::Support

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_helpers/support.rb

Class Method Summary collapse

Class Method Details

.certified_array!(splat_arg) ⇒ Object



3
4
5
6
# File 'lib/simple_helpers/support.rb', line 3

def self.certified_array!(splat_arg)
  array = splat_arg.first.is_a?(Array) ? splat_arg.first : Array(splat_arg)
  array.collect{|item| item.to_s}
end

.create_method(controller, name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/simple_helpers/support.rb', line 57

def self.create_method(controller, name)
  controller.class_eval do
    define_method(name) do |*args|
      if args.empty?
        send "#{name}_get"
      else
        send "#{name}_set", args.first
      end
    end

    define_method("#{name}_set") do |*args|
      instance_variable_set "@#{name}", args.first
    end

    define_method("#{name}_get") do |*args|
      "%{prefix}%{prefix_separator}%{content}%{suffix_separator}%{suffix}" %
        SimpleHelpers::Support.simple_helper_parts(controller, name)
    end
  end
end

.get_i18n_content(controller, key, options) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/simple_helpers/support.rb', line 47

def self.get_i18n_content(controller, key, options)
  scopes  = SimpleHelpers::Support.scopes(controller, key)

  result = SimpleHelpers::Support.translate(scopes[:first], options)
  if result.empty?
    result = SimpleHelpers::Support.translate(scopes[:second], options)
  end
  result
end

.parser(controller, context) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/simple_helpers/support.rb', line 27

def self.parser(controller, context)
  entries(context).each do |key|
    begin
      value = simple_helper_value(controller, key)
    ensure
      context.sub!(key, value)
    end
  end
  context
end

.scopes(controller, method_name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/simple_helpers/support.rb', line 8

def self.scopes(controller, method_name)
  controller_name = controller.class.name.underscore
  controller_name.gsub!(/\//, "_")
  controller_name.gsub!(/_controller$/, "")

  action_name = controller.action_name
  action_name = SimpleHelpers::Helpers::ACTION_ALIASES.fetch(action_name, action_name)
  if controller.class.constants.include? :SIMPLE_HELPER_ALIASES
    simple_helper_aliases = controller.class.const_get :SIMPLE_HELPER_ALIASES
    action_name = simple_helper_aliases.fetch(action_name, action_name) if simple_helper_aliases.is_a? Hash
  end
  group = method_name

  {
    :first => "#{group}.#{controller_name}.#{action_name.to_s}",
    :second => "#{group}.simple_helper_default"
  }
end

.translate(scope, options) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/simple_helpers/support.rb', line 38

def self.translate(scope, options)
  begin
    _options = options || {}
    I18n.translate!(scope, _options.merge({raise: true}))
  rescue I18n::MissingTranslationData
    ""
  end
end