class ToLocale

Public Class Methods

add_model(model, locale='tr') click to toggle source
# File lib/model_to_locale/to_locale.rb, line 18
def self.add_model(model, locale='tr')
  begin
    data = YAML::load_file(Rails.root.join('config/locales', "models.#{locale}.yml"))
    data["#{locale}"]['activerecord']['models']["#{model.downcase}"] = model.capitalize
    data = add_attributes(data, model, locale)
    write_to_file(data, locale)
  rescue
    create(locale)
    puts "models.#{locale}.yml file not found. Bu it's created now."
  end
end
create(locale='tr') click to toggle source
# File lib/model_to_locale/to_locale.rb, line 2
def self.create(locale='tr')
  models = ActiveRecord::Base.connection.tables.map{|model| model.capitalize.singularize.camelize }
  data = Hash.new
  data["#{locale}"] = Hash.new
  data["#{locale}"]['activerecord'] = Hash.new
  data["#{locale}"]['activerecord']['models'] = Hash.new
  data["#{locale}"]['activerecord']['attributes'] = Hash.new

  models.each do |model|
    data["#{locale}"]['activerecord']['models']["#{model.downcase}"] = model.capitalize
    data = add_attributes(data, model, locale)
  end

  write_to_file(data, locale)
end

Private Class Methods

add_attributes(data, model, locale) click to toggle source
# File lib/model_to_locale/to_locale.rb, line 37
def self.add_attributes(data, model, locale)
  begin
    columns = model.classify.constantize.column_names
    data["#{locale}"]['activerecord']['attributes']["#{model.downcase}"] = Hash.new
    columns.each do |column|
      data["#{locale}"]['activerecord']['attributes']["#{model.downcase}"]["#{column}"] = column.capitalize
    end
  rescue
    puts "Not found #{model} model."
  end
  data
end
write_to_file(data, locale='tr') click to toggle source
# File lib/model_to_locale/to_locale.rb, line 31
def self.write_to_file(data, locale='tr')
  File.open(Rails.root.join('config/locales', "models.#{locale}.yml"), 'w') do |fo|
    fo.puts data.to_yaml
  end
end