class Adminpanel::DumpGenerator

Public Instance Methods

create_json_file() click to toggle source
# File lib/generators/adminpanel/dump/dump_generator.rb, line 13
def create_json_file
  resource = name.demodulize.camelize.singularize
  resource = "Adminpanel::#{resource}".classify.constantize
  file_name = resource.to_s.pluralize.demodulize.downcase + '.json'
  puts "dumping #{resource.display_name.pluralize(I18n.default_locale)} into db/#{file_name}"

  create_file "db/#{file_name}" do
    resource.all.to_a.map{|o| o.attributes}.to_json
  end
  inject_into_seeds(resource, file_name)
end

Private Instance Methods

inject_into_seeds(resource, file_name) click to toggle source
# File lib/generators/adminpanel/dump/dump_generator.rb, line 26
def inject_into_seeds(resource, file_name)
  if options[:'inject-into-seeds']
    append_to_file 'db/seeds.rb' do
      "\nobjects = JSON.parse(open(\"\#{Rails.root}/db/#{file_name}\").read)\n" +
      "objects.each do |element|\n" +
        indent("object = #{resource}.new\n", 2) +
        indent("element.each do |k,v|\n", 2) +
          indent("object[k] = v\n", 4) +
        indent("end\n", 2) +
        indent("object.save\n", 2) +
      "end\n"
    end
  end
end