Module: AbAdmin::Models::Locator

Extended by:
ActiveSupport::Concern
Included in:
Locator
Defined in:
lib/ab_admin/models/locator.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

YAML_LINE_WIDTH =
200

Instance Method Summary collapse

Instance Method Details

#allObject



113
114
115
116
# File 'lib/ab_admin/models/locator.rb', line 113

def all
  @paths.each { |path| @data.deep_merge!(YAML.load_file(path)) }
  @data
end

#initializeObject



108
109
110
111
# File 'lib/ab_admin/models/locator.rb', line 108

def initialize
  @data = {}
  @files = self.class.find_files
end

#prepare_filesObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ab_admin/models/locator.rb', line 118

def prepare_files
  message = nil
  locale_replace_regexp = Regexp.new("(^#{I18n.default_locale}|(?<=\.)#{I18n.default_locale}(?=\.yml))")

  locale_files = @files.find_all{|f| f =~ /\/\w+(\.readonly)?\.yml/ }.map { |path| self.class.prepare_data(path) }
  main_locale_files = locale_files.find_all { |file| file.locale == I18n.default_locale }

  main_locale_files.each do |main_file|
    I18n.available_locales.each do |locale|
      next if locale == I18n.default_locale
      clean_locale_hash = main_file.data.deep_clear_values
      path = File.join(main_file.dir, main_file.filename.sub(locale_replace_regexp, locale.to_s))
      if File.exists?(path)
        file = self.class.prepare_data(path)
        self.class.save(path, {locale.to_s => clean_locale_hash.deep_add(file.data)})
      else
        self.class.save(path, {locale.to_s => clean_locale_hash})
        message = 'Reload application to pick up new locale files'
      end
    end
  end
  {message: message}
end