forked from Terrastories/terrastories
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The Import concern allows any controller to have import action that invokes the import method of its model. Additionally we have a similar concern in the routes as well. The Importable concern allows any model to support import by providing the importer class to be used. It expects the importer instance to be initialized with some data and respond to import.
- Loading branch information
Showing
4 changed files
with
41 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Import | ||
extend ActiveSupport::Concern | ||
|
||
def import | ||
if params[:file].nil? | ||
flash[:error] = "No file was attached!" | ||
else | ||
begin | ||
controller_name.classify.constantize.import(params[:file].read) | ||
flash[:notice] = "Points were imported successfully!" | ||
rescue ImportError => e | ||
raise e | ||
flash[:error] = e.message | ||
end | ||
end | ||
|
||
redirect_to action: :index | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
class ImportError < StandardError; end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Importable | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def importer(importer_klass) | ||
@importer_klass = importer_klass | ||
end | ||
|
||
def import(data) | ||
@importer_klass.new(data).import | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters