Class: RailsAdminImport::Formats::JSONImporter
- Inherits:
-
FileImporter
- Object
- FileImporter
- RailsAdminImport::Formats::JSONImporter
- Defined in:
- lib/rails_admin_import/formats/json_importer.rb
Instance Attribute Summary
Attributes inherited from FileImporter
#error, #filename, #import_model
Instance Method Summary collapse
-
#each_record ⇒ Object
A method that yields a hash of attributes for each record to import.
- #root_key ⇒ Object
Methods inherited from FileImporter
Constructor Details
This class inherits a constructor from RailsAdminImport::Formats::FileImporter
Instance Method Details
#each_record ⇒ Object
A method that yields a hash of attributes for each record to import
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# 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 ⇒ Object
27 28 29 |
# File 'lib/rails_admin_import/formats/json_importer.rb', line 27 def root_key import_model.model.model_name.element.pluralize end |