Module: SoMeta::Helper

Defined in:
lib/so_meta/helper.rb

Instance Method Summary collapse

Instance Method Details

#so_meta(name, scope = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/so_meta/helper.rb', line 3

def so_meta(name, scope = nil)
  interpolation_data = instance_variable_get("@so_meta_#{name}_interpolation") || {}

  request_key = "so_meta.#{controller_path.gsub(/\//, "_")}.#{action_name}"

  if scope.present?
    begin
      return I18n.translate!("#{request_key}.#{scope}.#{name}", **interpolation_data)
    rescue I18n::MissingTranslationData
      nil
    end
  end

  t("#{request_key}.#{name}", **interpolation_data.merge(default: t("so_meta.defaults.#{name}")))
end

#so_meta_interpolation(name, hash) ⇒ Object



19
20
21
22
23
24
# File 'lib/so_meta/helper.rb', line 19

def so_meta_interpolation(name, hash)
  variable_name = "@so_meta_#{name}_interpolation"
  interpolation = instance_variable_get(variable_name) || {}
  hash.transform_values! { |v| html_escape(v) }
  instance_variable_set(variable_name, interpolation.merge(hash))
end