Module: AbAdmin::Models::Locator::ClassMethods

Defined in:
lib/ab_admin/models/locator.rb

Instance Method Summary collapse

Instance Method Details

#find_filesObject


14
15
16
# File 'lib/ab_admin/models/locator.rb', line 14

def find_files
  Dir[Rails.root.join('config', 'locales', '*.yml')]
end

#flat_hash(hash, k = []) ⇒ Object


30
31
32
33
# File 'lib/ab_admin/models/locator.rb', line 30

def flat_hash(hash, k = [])
  return {k => hash} unless hash.is_a?(Hash)
  hash.inject({}) { |h, v| h.merge! flat_hash(v[-1], k + [v[0]]) }
end

#prepare_data(path) ⇒ Object


23
24
25
26
27
28
# File 'lib/ab_admin/models/locator.rb', line 23

def prepare_data(path)
  data = YAML.load_file(path)
  locale = data.keys.first
  OpenStruct.new({locale: locale.to_sym, data: data[locale], flat_data: flat_hash(data[locale]),
                  filename: File.basename(path), path: path, dir: File.dirname(path)})
end

#save(path, data) ⇒ Object


18
19
20
21
# File 'lib/ab_admin/models/locator.rb', line 18

def save(path, data)
  data.deep_transform_values! { |v| AbAdmin.normalize_html(v) }
  File.write path, data.deep_stringify_keys.to_yaml.sub(/\A---\s+/, '').gsub(/:\s+$/, ':').gsub(/^(\s+)(yes|no):/, '\1"\2":')
end