class RailsAdminImport::Formats::JSONImporter

Public Instance Methods

each_record() { |symbolize_keys| ... } click to toggle source

A method that yields a hash of attributes for each record to import

# File lib/rails_admin_import/formats/json_importer.rb, line 8
def each_record
  File.open(filename) do |file|
    data = JSON.load(file)

    if data.is_a? Hash
      # Load array from root key
      data = data[root_key]
    end

    if !data.is_a? Array
      raise ArgumentError, I18n.t("admin.import.invalid_json", root_key: root_key)
    end

    data.each do |record|
      yield record.symbolize_keys
    end
  end
end
root_key() click to toggle source
# File lib/rails_admin_import/formats/json_importer.rb, line 27
def root_key
  import_model.model.model_name.element.pluralize
end