ixport 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +41 -0
- data/app/controllers/ixport/application_controller.rb +6 -1
- data/app/controllers/ixport/imports_controller.rb +4 -1
- data/app/controllers/ixport/mappings_controller.rb +1 -1
- data/app/controllers/ixport/previews_controller.rb +1 -1
- data/app/controllers/ixport/runs_controller.rb +1 -1
- data/app/models/ixport/created_record.rb +31 -0
- data/app/models/ixport/import.rb +13 -2
- data/app/views/ixport/imports/index.html.erb +3 -1
- data/app/views/ixport/imports/show.html.erb +22 -2
- data/lib/generators/ixport/install/install_generator.rb +2 -0
- data/lib/generators/ixport/install/templates/add_source_to_ixport_imports.rb.erb +6 -0
- data/lib/generators/ixport/install/templates/create_ixport_created_records.rb.erb +10 -0
- data/lib/generators/ixport/update/update_generator.rb +8 -0
- data/lib/ixport/registrations.rb +29 -0
- data/lib/ixport/version.rb +1 -1
- data/lib/ixport.rb +5 -0
- data/the_local/agents/ixport-develop.md +132 -20
- data/the_local/agents/ixport-info.md +39 -24
- data/the_local/agents/ixport-install.md +9 -6
- data/the_local/interface.yml +6 -0
- metadata +4 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ff2824039859bc09b43633ce5375cf09c3bcf7a3bad0930d0d7e2e9da6775988
|
|
4
|
+
data.tar.gz: 151224a3a47a6b59f9e50ef13dc6b91e09f77402c67f747a0393049a7b98ea0f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0e7d82be027912bb16e97edbe250a118581da67faae7b25fd9ed3b45eeb973af22c0168715739ce0787537094adf9e180a504d71964c2be2261b4eb4e8fa3dd2
|
|
7
|
+
data.tar.gz: ac2e12649b92c54e3a9db89823e5f193fe6864880a58b92669e7fbe33c6a59b8dbca4529f45dbba8b48bf57cc68c39d3cfc3f7c9a5b6f7ed98840cc29a6ff127
|
data/README.md
CHANGED
|
@@ -208,6 +208,47 @@ A link or upload naming a record the rule refuses answers not found, and so does
|
|
|
208
208
|
|
|
209
209
|
A person the rule refuses does not see that import or export listed, nor their past imports of it, and every one of its pages and downloads answers not found. A registration with no rule is open to every signed-in person.
|
|
210
210
|
|
|
211
|
+
## Imports from a source
|
|
212
|
+
|
|
213
|
+
Data an app brings in from somewhere other than an uploaded CSV file, such as another service's API, can still be recorded as an import. Register the source once:
|
|
214
|
+
|
|
215
|
+
```ruby
|
|
216
|
+
Ixport.register_source :github_move, title: "GitHub move",
|
|
217
|
+
allow: ->(person, _account) { person.staff? }
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
Then open an import from it for a person and an account:
|
|
221
|
+
|
|
222
|
+
```ruby
|
|
223
|
+
import = Ixport.open_import(:github_move, person: current_user, account: current_account)
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
The import has no file, and it is running from the moment it opens. Opening an import from a source that is not registered raises `Ixport::UnknownSource`. The imports page lists sourced imports among the person's CSV imports, under the source's title, and each opens at its result page, which offers no mapping, preview or run. The `allow:` rule works as it does for an import registration.
|
|
227
|
+
|
|
228
|
+
Run `bin/rails generate ixport:update` on an app that installed an earlier ixport, for the migration that adds an import's source.
|
|
229
|
+
|
|
230
|
+
## Records an import created
|
|
231
|
+
|
|
232
|
+
A host records each record it created against an open import:
|
|
233
|
+
|
|
234
|
+
```ruby
|
|
235
|
+
import.record_created(plan)
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Recording the same record against the same import twice keeps it once. The import's result page lists every record recorded against it, grouped by kind, with a count for each kind.
|
|
239
|
+
|
|
240
|
+
Register a kind for each record class to say how its records are shown:
|
|
241
|
+
|
|
242
|
+
```ruby
|
|
243
|
+
Ixport.register_kind Plan, label: "Plan",
|
|
244
|
+
name: ->(plan) { plan.title },
|
|
245
|
+
path: ->(plan) { Rails.application.routes.url_helpers.plan_path(plan) }
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
Each listed record shows the kind's label and the record's name, linked to its page. A record of a class with no kind registered is listed by its class name and id, unlinked. A record deleted since it was recorded is listed by its class name and id, marked deleted.
|
|
249
|
+
|
|
250
|
+
Run `bin/rails generate ixport:update` on an app that installed an earlier ixport, for the migration that adds the records an import created.
|
|
251
|
+
|
|
211
252
|
## Claude Code agents
|
|
212
253
|
|
|
213
254
|
ixport ships its own Claude Code agents through [the_local](https://github.com/DYB-Development/the_local). An app that lists ixport in its Gemfile and runs `bundle exec the_local install` gets them in `.claude/agents/`.
|
|
@@ -16,10 +16,15 @@ module Ixport
|
|
|
16
16
|
|
|
17
17
|
def find_import
|
|
18
18
|
@import = Import.find_by(id: params[:import_id] || params[:id], person: ixport_person, account: ixport_account)
|
|
19
|
-
registration = @import && Ixport.registrations[@import.key.to_sym]
|
|
19
|
+
registration = @import && (@import.source ? Ixport.sources[@import.source.to_sym] : Ixport.registrations[@import.key.to_sym])
|
|
20
20
|
head :not_found unless registration && allowed?(registration, @import.target)
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
+
def find_csv_import
|
|
24
|
+
find_import
|
|
25
|
+
head :not_found if @import&.source && !performed?
|
|
26
|
+
end
|
|
27
|
+
|
|
23
28
|
def ixport_person
|
|
24
29
|
send(Ixport.configuration.current_person_method)
|
|
25
30
|
end
|
|
@@ -8,7 +8,10 @@ module Ixport
|
|
|
8
8
|
|
|
9
9
|
def index
|
|
10
10
|
@registrations = Ixport.registrations.values.select { |registration| allowed?(registration) }
|
|
11
|
-
|
|
11
|
+
sources = Ixport.sources.values.select { |source| allowed?(source) }
|
|
12
|
+
@imports = Import.where(person: ixport_person, account: ixport_account)
|
|
13
|
+
.merge(Import.where(key: @registrations.map { |registration| registration.key.to_s }).or(Import.where(source: sources.map { |source| source.key.to_s })))
|
|
14
|
+
.order(created_at: :desc).with_attached_file.includes(:refusals)
|
|
12
15
|
end
|
|
13
16
|
|
|
14
17
|
def new
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ixport
|
|
4
|
+
class CreatedRecord < ApplicationRecord
|
|
5
|
+
self.table_name = "ixport_created_records"
|
|
6
|
+
|
|
7
|
+
belongs_to :import
|
|
8
|
+
belongs_to :record, polymorphic: true, optional: true
|
|
9
|
+
|
|
10
|
+
def kind_label
|
|
11
|
+
kind ? kind.label : record_type
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def name
|
|
15
|
+
return "#{record_type} #{record_id} (deleted)" unless record
|
|
16
|
+
return "#{record_type} #{record_id}" unless kind
|
|
17
|
+
|
|
18
|
+
kind.name.call(record)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def path
|
|
22
|
+
kind.path.call(record) if kind && record
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def kind
|
|
28
|
+
Ixport.kind_for(record_type)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
data/app/models/ixport/import.rb
CHANGED
|
@@ -12,10 +12,11 @@ module Ixport
|
|
|
12
12
|
|
|
13
13
|
has_one_attached :file
|
|
14
14
|
has_many :refusals, -> { order(:row_number) }, class_name: "Ixport::ImportRefusal", dependent: :destroy
|
|
15
|
+
has_many :created_records, class_name: "Ixport::CreatedRecord", dependent: :destroy
|
|
15
16
|
|
|
16
17
|
STALLED_AFTER = 1.hour
|
|
17
18
|
|
|
18
|
-
validate :incoming_file_is_usable, on: :create
|
|
19
|
+
validate :incoming_file_is_usable, on: :create, unless: :source
|
|
19
20
|
|
|
20
21
|
attr_reader :run_error
|
|
21
22
|
|
|
@@ -25,6 +26,14 @@ module Ixport
|
|
|
25
26
|
end
|
|
26
27
|
end
|
|
27
28
|
|
|
29
|
+
def record_created(record)
|
|
30
|
+
created_records.create_or_find_by!(record: record)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def created_records_by_kind
|
|
34
|
+
created_records.includes(:record).group_by(&:record_type).sort_by(&:first)
|
|
35
|
+
end
|
|
36
|
+
|
|
28
37
|
def finished?
|
|
29
38
|
status == "finished"
|
|
30
39
|
end
|
|
@@ -42,12 +51,14 @@ module Ixport
|
|
|
42
51
|
end
|
|
43
52
|
|
|
44
53
|
def step
|
|
45
|
-
return :result if finished?
|
|
54
|
+
return :result if finished? || source
|
|
46
55
|
|
|
47
56
|
mapping.present? ? :preview : :mapping
|
|
48
57
|
end
|
|
49
58
|
|
|
50
59
|
def title
|
|
60
|
+
return Ixport.source(source).title if source
|
|
61
|
+
|
|
51
62
|
Ixport.registration(key).title
|
|
52
63
|
end
|
|
53
64
|
|
|
@@ -12,7 +12,9 @@
|
|
|
12
12
|
<% @imports.each do |import| %>
|
|
13
13
|
<div data-past-import>
|
|
14
14
|
<%= link_to import_step_path(import), data: { step_link: true } do %><span data-import-title><%= import.title %></span><% end %>
|
|
15
|
-
|
|
15
|
+
<% if import.file.attached? %>
|
|
16
|
+
<span data-file-name><%= import.file.filename.to_s %></span>
|
|
17
|
+
<% end %>
|
|
16
18
|
<span data-status><%= import.status.capitalize %></span>
|
|
17
19
|
<span data-made-on><%= import.created_at.strftime("%b %-d, %Y") %></span>
|
|
18
20
|
<% if import.finished? %>
|
|
@@ -21,10 +21,12 @@
|
|
|
21
21
|
<p>Rows saved before it stopped stay saved.</p>
|
|
22
22
|
<% end %>
|
|
23
23
|
<%= ui_badge(label: tag.span(@import.status.humanize, data: { import_status: true }), variant: @import.failed? ? :danger : :success) %>
|
|
24
|
-
<%= ui_grid(cols: { default: 1, sm: 3 }) do %>
|
|
24
|
+
<%= ui_grid(cols: { default: 1, sm: @import.source ? 2 : 3 }) do %>
|
|
25
25
|
<%= ui_stat_card(label: "Added", value: tag.span(@import.added_count, data: { result_count: "added" })) %>
|
|
26
26
|
<%= ui_stat_card(label: "Updated", value: tag.span(@import.updated_count, data: { result_count: "updated" })) %>
|
|
27
|
-
|
|
27
|
+
<% unless @import.source %>
|
|
28
|
+
<%= ui_stat_card(label: "Rows in file", value: tag.span(@import.row_count, data: { result_count: "total" })) %>
|
|
29
|
+
<% end %>
|
|
28
30
|
<% end %>
|
|
29
31
|
<% if @import.refusals.any? %>
|
|
30
32
|
<%= ui_section(title: "Refused") do %>
|
|
@@ -39,4 +41,22 @@
|
|
|
39
41
|
<% end %>
|
|
40
42
|
<% end %>
|
|
41
43
|
<% end %>
|
|
44
|
+
<% @import.created_records_by_kind.each do |kind, created| %>
|
|
45
|
+
<%= tag.div(data: { created_kind: kind }) do %>
|
|
46
|
+
<%= ui_section(title: tag.span(created.first.kind_label, data: { created_label: true })) do |section| %>
|
|
47
|
+
<% section.with_aside { tag.span(created.size, data: { created_count: true }) } %>
|
|
48
|
+
<ul>
|
|
49
|
+
<% created.each do |created_record| %>
|
|
50
|
+
<%= tag.li(data: { created_record: true }) do %>
|
|
51
|
+
<% if created_record.path %>
|
|
52
|
+
<%= link_to created_record.name, created_record.path %>
|
|
53
|
+
<% else %>
|
|
54
|
+
<%= created_record.name %>
|
|
55
|
+
<% end %>
|
|
56
|
+
<% end %>
|
|
57
|
+
<% end %>
|
|
58
|
+
</ul>
|
|
59
|
+
<% end %>
|
|
60
|
+
<% end %>
|
|
61
|
+
<% end %>
|
|
42
62
|
<% end %>
|
|
@@ -19,6 +19,8 @@ module Ixport
|
|
|
19
19
|
migration_template "add_wallflower_task_to_ixport_imports.rb.erb", "db/migrate/add_wallflower_task_to_ixport_imports.rb"
|
|
20
20
|
migration_template "add_error_message_to_ixport_imports.rb.erb", "db/migrate/add_error_message_to_ixport_imports.rb"
|
|
21
21
|
migration_template "add_target_to_ixport_imports.rb.erb", "db/migrate/add_target_to_ixport_imports.rb"
|
|
22
|
+
migration_template "add_source_to_ixport_imports.rb.erb", "db/migrate/add_source_to_ixport_imports.rb"
|
|
23
|
+
migration_template "create_ixport_created_records.rb.erb", "db/migrate/create_ixport_created_records.rb"
|
|
22
24
|
end
|
|
23
25
|
end
|
|
24
26
|
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
class CreateIxportCreatedRecords < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
|
|
2
|
+
def change
|
|
3
|
+
create_table :ixport_created_records do |t|
|
|
4
|
+
t.references :import, null: false, foreign_key: { to_table: :ixport_imports }
|
|
5
|
+
t.references :record, polymorphic: true, null: false, index: false
|
|
6
|
+
t.timestamps
|
|
7
|
+
end
|
|
8
|
+
add_index :ixport_created_records, %i[import_id record_type record_id], unique: true, name: "index_ixport_created_records_on_import_and_record"
|
|
9
|
+
end
|
|
10
|
+
end
|
|
@@ -27,6 +27,14 @@ module Ixport
|
|
|
27
27
|
def copy_target_migration
|
|
28
28
|
migration_template "add_target_to_ixport_imports.rb.erb", "db/migrate/add_target_to_ixport_imports.rb"
|
|
29
29
|
end
|
|
30
|
+
|
|
31
|
+
def copy_source_migration
|
|
32
|
+
migration_template "add_source_to_ixport_imports.rb.erb", "db/migrate/add_source_to_ixport_imports.rb"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def copy_created_records_migration
|
|
36
|
+
migration_template "create_ixport_created_records.rb.erb", "db/migrate/create_ixport_created_records.rb"
|
|
37
|
+
end
|
|
30
38
|
end
|
|
31
39
|
end
|
|
32
40
|
end
|
data/lib/ixport/registrations.rb
CHANGED
|
@@ -12,9 +12,12 @@ module Ixport
|
|
|
12
12
|
|
|
13
13
|
Registration = Struct.new(:key, :title, :importer, :allow, :refuse_whole_file, keyword_init: true) { include AccessRule }
|
|
14
14
|
ExportRegistration = Struct.new(:key, :title, :exporter, :chosen, :allow, keyword_init: true) { include AccessRule }
|
|
15
|
+
SourceRegistration = Struct.new(:key, :title, :allow, keyword_init: true) { include AccessRule }
|
|
16
|
+
KindRegistration = Struct.new(:label, :name, :path, keyword_init: true)
|
|
15
17
|
|
|
16
18
|
class MissingImporter < StandardError; end
|
|
17
19
|
class MissingExporter < StandardError; end
|
|
20
|
+
class UnknownSource < StandardError; end
|
|
18
21
|
|
|
19
22
|
def self.register_import(key, title:, importer:, allow: nil, refuse_whole_file: false)
|
|
20
23
|
registrations[key.to_sym] = Registration.new(key: key.to_sym, title: title, importer: importer.to_s, allow: allow, refuse_whole_file: refuse_whole_file)
|
|
@@ -53,8 +56,34 @@ module Ixport
|
|
|
53
56
|
@export_registrations ||= {}
|
|
54
57
|
end
|
|
55
58
|
|
|
59
|
+
def self.register_source(key, title:, allow: nil)
|
|
60
|
+
sources[key.to_sym] = SourceRegistration.new(key: key.to_sym, title: title, allow: allow)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def self.source(key)
|
|
64
|
+
sources.fetch(key.to_sym)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def self.sources
|
|
68
|
+
@sources ||= {}
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def self.register_kind(record_class, label:, name:, path:)
|
|
72
|
+
kinds[record_class.to_s] = KindRegistration.new(label: label, name: name, path: path)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def self.kind_for(record_type)
|
|
76
|
+
kinds[record_type.to_s]
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def self.kinds
|
|
80
|
+
@kinds ||= {}
|
|
81
|
+
end
|
|
82
|
+
|
|
56
83
|
def self.reset_registrations!
|
|
57
84
|
@registrations = {}
|
|
58
85
|
@export_registrations = {}
|
|
86
|
+
@sources = {}
|
|
87
|
+
@kinds = {}
|
|
59
88
|
end
|
|
60
89
|
end
|
data/lib/ixport/version.rb
CHANGED
data/lib/ixport.rb
CHANGED
|
@@ -8,4 +8,9 @@ require "ixport/background_runner"
|
|
|
8
8
|
require "ixport/engine"
|
|
9
9
|
|
|
10
10
|
module Ixport
|
|
11
|
+
def self.open_import(source, person:, account:)
|
|
12
|
+
raise UnknownSource, "Ixport has no source registered as #{source}." unless sources.key?(source.to_sym)
|
|
13
|
+
|
|
14
|
+
Import.create!(source: source.to_s, person: person, account: account, status: "running")
|
|
15
|
+
end
|
|
11
16
|
end
|
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: ixport-develop
|
|
3
|
-
description: Use PROACTIVELY for setting up CSV imports and exports in a Rails app that has ixport — registering a mapped import, writing its importer class, declaring the fields a person maps a CSV's columns to, choosing the field or fields rows are matched on, scoping the records an import may add or update to the signed-in person or account, limiting which people may use an import or export with an access rule such as the app's own Pundit policy, linking to an import's upload page, explaining the upload, column-matching, preview and result pages and their messages, explaining the list of a person's past imports on the imports page, explaining what running an import saves and refuses, explaining what a person sees when an import fails partway and which rows it keeps, running imports in a background job through the app's own runner class, explaining what a person sees while an import runs in the background, registering a fixed export or a chosen export, writing its exporter class, declaring its columns including columns taken from a related record, computing a column's value, scoping the records an export may hold, letting an account save named templates of picked columns for an export, explaining the new, edit and delete template pages and what a template's download holds, linking to the exports page, one export's CSV download or one template's download, making the app's own import job fail with Ixport::ImportFailed, and fixing a boot that fails with Ixport::MissingImporter or Ixport::MissingExporter — MUST BE USED instead of hand-writing a CSV upload controller, a column-mapping form, a row-matching loop, an import preview, a loop that saves imported rows, a background job that saves imported rows, a rescue that records why an import stopped, a permission check in front of an import or export page, a CSV download action, a loop that writes records to a CSV, or a model and form for saved column picks.
|
|
3
|
+
description: Use PROACTIVELY for setting up CSV imports and exports in a Rails app that has ixport — registering a mapped import, writing its importer class, declaring the fields a person maps a CSV's columns to, choosing the field or fields rows are matched on, scoping the records an import may add or update to the signed-in person or account, limiting which people may use an import or export with an access rule such as the app's own Pundit policy, linking to an import's upload page, explaining the upload, column-matching, preview and result pages and their messages, explaining the list of a person's past imports on the imports page, explaining what running an import saves and refuses, explaining what a person sees when an import fails partway and which rows it keeps, running imports in a background job through the app's own runner class, explaining what a person sees while an import runs in the background, registering a fixed export or a chosen export, writing its exporter class, declaring its columns including columns taken from a related record, computing a column's value, scoping the records an export may hold, letting an account save named templates of picked columns for an export, explaining the new, edit and delete template pages and what a template's download holds, linking to the exports page, one export's CSV download or one template's download, making the app's own import job fail with Ixport::ImportFailed, registering a source for data the app brings in from somewhere other than an uploaded file, opening an import from a source with Ixport.open_import, explaining what a person sees for a sourced import, handling Ixport::UnknownSource, recording the records the app's own code created against an import with record_created, registering a kind with Ixport.register_kind so those records show a label, a name and a link, explaining the list of created records on an import's result page, linking to an upload page that imports into one chosen record such as a quiz, writing an access rule that checks that chosen record, registering an import that saves nothing from a file with any refused row, and fixing a boot that fails with Ixport::MissingImporter or Ixport::MissingExporter — MUST BE USED instead of hand-writing a CSV upload controller, a column-mapping form, a row-matching loop, an import preview, a loop that saves imported rows, a background job that saves imported rows, a rescue that records why an import stopped, a permission check in front of an import or export page, a CSV download action, a loop that writes records to a CSV, or a model and form for saved column picks, a table of records an import made, or a page listing them.
|
|
4
4
|
tools: Bash, Read, Write, Edit, Grep
|
|
5
5
|
scope: CSV import and export — a mounted engine where a signed-in person sees the mapped imports and the exports the host app registers, each import backed by an importer class and each export by an exporter class the host writes, where an export is either fixed or chosen, and a chosen export lets the account save named templates of picked columns
|
|
6
6
|
---
|
|
7
7
|
|
|
8
|
-
This local adds mapped imports, fixed exports
|
|
8
|
+
This local adds mapped imports, fixed exports, chosen exports and sources to a Rails app that already has ixport installed by following the steps below exactly. It writes one importer or exporter class and one registration line per import or export, one registration line per source, and it invents no method, option, route or file that is not listed here.
|
|
9
9
|
|
|
10
10
|
## What ixport is
|
|
11
11
|
|
|
12
|
-
ixport is a Rails engine that lets a signed-in person import a host app's records from CSV files and download them as CSV files. The app registers each type of CSV a person can import, called a mapped import, and writes an importer class that names the fields the CSV's columns map to, the field or fields rows are matched on, and the records a row may add to or update. A person picks an import, uploads a CSV file for it, matches each of the file's columns to one of the importer's fields, sees a preview of how many rows will add a record, update one or be refused, runs the import to save those rows, and then sees how many rows were added and updated and each refused row. When the run stops on an error, the import is marked failed, keeps the rows it saved before stopping, and its page shows the error. An import runs within the request by default, or, when the app is set up for it, in the background through wallflower or through a runner class the app writes. The imports page also lists the person's own past imports in the current account, each opening at the step it has reached. The app also registers each CSV a person can download, and writes an exporter class that names the columns the file can hold, in order, and the records it holds one row each for. An export is either fixed or chosen. A fixed export always holds every declared column, and a person downloads it from the exports page. A chosen export lets people in the current account save named templates, each a picked list of the exporter's columns in a picked order, and a person downloads a template from the exports page to get a file holding only those columns. Fire this local when a developer wants people to load one of the app's records from a spreadsheet or download them as one, wants people to pick which columns an export holds, wants to link to an import's upload page, the exports page, an export's download or a template's download, asks what the upload, column-matching, preview, result, exports or template pages do, asks what the list of past imports shows, asks what running an import saves, what happens when a run fails, or what an export's or template's file holds, wants imports run in a background job of the app's own, or when the app fails to boot with `Ixport::MissingImporter` or `Ixport::MissingExporter`.
|
|
12
|
+
ixport is a Rails engine that lets a signed-in person import a host app's records from CSV files and download them as CSV files. The app registers each type of CSV a person can import, called a mapped import, and writes an importer class that names the fields the CSV's columns map to, the field or fields rows are matched on, and the records a row may add to or update. A person picks an import, uploads a CSV file for it, matches each of the file's columns to one of the importer's fields, sees a preview of how many rows will add a record, update one or be refused, runs the import to save those rows, and then sees how many rows were added and updated and each refused row. When the run stops on an error, the import is marked failed, keeps the rows it saved before stopping, and its page shows the error. An import runs within the request by default, or, when the app is set up for it, in the background through wallflower or through a runner class the app writes. The imports page also lists the person's own past imports in the current account, each opening at the step it has reached. The app also registers each CSV a person can download, and writes an exporter class that names the columns the file can hold, in order, and the records it holds one row each for. An export is either fixed or chosen. A fixed export always holds every declared column, and a person downloads it from the exports page. A chosen export lets people in the current account save named templates, each a picked list of the exporter's columns in a picked order, and a person downloads a template from the exports page to get a file holding only those columns. Data the app brings in from somewhere other than an uploaded file, such as another service's API, can be recorded as an import too: the app registers a source, and its own code opens an import from that source for a person and an account, which is listed among that person's past imports and has a result page but no file, mapping, preview or run. The app's own code can record each record it created against any import, and the import's result page lists those records grouped by kind, each shown with the label, name and link the app registered for its class. An import can also be opened for one record the app chooses, such as a quiz its questions are added to, and an import can be registered to save no row from a file in which any row is refused. Fire this local when a developer wants an import's result page to list the records their own code created for it, wants those records shown with a label, a name and a link, wants a CSV import to add its rows to one chosen record, wants an import that saves nothing unless every row is valid, or when a developer wants to record data brought in from another service as an import, or when a developer wants people to load one of the app's records from a spreadsheet or download them as one, wants people to pick which columns an export holds, wants to link to an import's upload page, the exports page, an export's download or a template's download, asks what the upload, column-matching, preview, result, exports or template pages do, asks what the list of past imports shows, asks what running an import saves, what happens when a run fails, or what an export's or template's file holds, wants imports run in a background job of the app's own, or when the app fails to boot with `Ixport::MissingImporter` or `Ixport::MissingExporter`.
|
|
13
13
|
|
|
14
14
|
## Interface
|
|
15
15
|
|
|
16
|
-
- `Ixport.register_import` — `Ixport.register_import(key, title:, importer:, allow: nil)` registers one mapped import under a key, with the title a person sees, the name of its importer class,
|
|
16
|
+
- `Ixport.register_import` — `Ixport.register_import(key, title:, importer:, allow: nil, refuse_whole_file: false)` registers one mapped import under a key, with the title a person sees, the name of its importer class, optionally an access rule that decides which people may use it and, when it takes a third argument, which chosen records they may import into, and optionally `refuse_whole_file: true` so a run saves no row from a file in which any row is refused.
|
|
17
|
+
- `Ixport.register_source` — `Ixport.register_source(key, title:, allow: nil)` registers one source under a key, with the title a person sees for its imports, and optionally an access rule that decides which people may see them.
|
|
18
|
+
- `Ixport.open_import` — `Ixport.open_import(source, person:, account:)` saves and returns a new import from the source registered under the key `source`, belonging to `person` and `account`, with no file and the status running.
|
|
19
|
+
- `Ixport.register_kind` — `Ixport.register_kind(record_class, label:, name:, path:)` registers how records of one class are shown in an import's list of created records: the label heading their group, a lambda taking the record and returning its name, and a lambda taking the record and returning the path its name links to.
|
|
20
|
+
- `Ixport::Import#record_created` — `import.record_created(record)` records one saved record the app's own code created against the import, keeping it once however many times it is recorded, so the import's result page lists it.
|
|
21
|
+
- `Ixport::UnknownSource` — the error `Ixport.open_import` raises when no source is registered under the key it was given, with the message "Ixport has no source registered as <key>."
|
|
17
22
|
- `Ixport::Importer` — the base class every importer inherits from.
|
|
18
23
|
- `field` — `field key, label:` is called in an importer's class body and declares one field a CSV column can be mapped to, with the label a person sees.
|
|
19
24
|
- `match_on` — `match_on key` or `match_on key, key, ...` is called in an importer's class body and names the declared field or fields a row is matched to an existing record on, where a row matches a record only when it equals the record on every one of them.
|
|
@@ -25,14 +30,14 @@ ixport is a Rails engine that lets a signed-in person import a host app's record
|
|
|
25
30
|
- `Ixport::ImportFailed` — the error a background run is failed with when its import stopped on an error, carrying the import's error message: wallflower's task raises it, and the app's own job may raise it.
|
|
26
31
|
- `Ixport.run_import` — `Ixport.run_import(import)` or `Ixport.run_import(import, reporter: reporter)` runs one import that a runner was handed, as the signed-in person and in the account it was uploaded with, saving and refusing its rows the same way as a run within the request, marking the import failed rather than raising when the run stops on an error, and optionally reporting each row to `reporter`.
|
|
27
32
|
- `enqueue` — `enqueue(import)` is the class method the app's own runner class defines, which ixport calls with each import a person runs, once the import is marked running, so the runner can arrange for `Ixport.run_import` to run it later.
|
|
28
|
-
- `GET /` — the engine's root page, at the path the engine is mounted under, which lists every registered import by its title, each linking to that import's upload page, and then lists the signed-in person's past imports in the current account, newest first, each linking to the step it has reached.
|
|
29
|
-
- `GET /imports/new` — the upload page for one import, given the import's registered key as the `key` query parameter, which shows the import's title, a CSV file field and an Upload button.
|
|
30
|
-
- `POST /imports` — takes the `key` and `
|
|
31
|
-
- `GET /imports/:import_id/mapping` — the column-matching page, titled "Match columns", which lists each column header from the file's first row beside a select of the importer's field labels and a "Skip" choice.
|
|
32
|
-
- `PATCH /imports/:import_id/mapping` — saves which field each column maps to and redirects to the import's preview page.
|
|
33
|
-
- `GET /imports/:import_id/preview` — the preview page, titled "Preview", which shows how many rows will add a record, update one or be refused, lists each updating row and each refused row by its row number, saves nothing, and offers a "Run import" button, and which redirects to the import's result page once the import has finished or failed, or while it is running in the background.
|
|
34
|
-
- `POST /imports/:import_id/run` — with no background runner, saves every row that adds or updates a record, records every refused row with its reason, marks the import finished, or failed when the run stops on an error, and redirects to the import's result page, and does nothing but redirect when the import has already finished or failed; with a background runner, marks a pending import running, hands it to the runner, and redirects to the wallflower task's page or, for the app's own runner, to the import's result page, and hands nothing to the runner and redirects to the import's result page when the import is already running, finished or failed.
|
|
35
|
-
- `GET /imports/:id` — the result page, titled "Import finished", which shows how many rows the run added and updated and lists each refused row by its row number with its reason, titled "Import running" with the message "This import is running." while a background run is under way, titled "Import failed" with the error the run stopped on and the counts of rows saved before it stopped when the run failed, and which redirects to the import's preview page when the import has neither run nor started running.
|
|
33
|
+
- `GET /` — the engine's root page, at the path the engine is mounted under, which lists every registered import by its title, each linking to that import's upload page, and then lists the signed-in person's past imports in the current account, sourced imports among them, newest first, each linking to the step it has reached.
|
|
34
|
+
- `GET /imports/new` — the upload page for one import, given the import's registered key as the `key` query parameter and optionally a chosen record's Global ID as the `target` query parameter, which shows the import's title, the labels of the fields the import expects, a CSV file field and an Upload button.
|
|
35
|
+
- `POST /imports` — takes the `key`, `file` and optional `target` parameters, stores the uploaded file as an import belonging to the signed-in person and the current account, and to the chosen record when one is named, and redirects to that import's column-matching page.
|
|
36
|
+
- `GET /imports/:import_id/mapping` — the column-matching page, titled "Match columns", which lists each column header from the file's first row beside a select of the importer's field labels and a "Skip" choice, and which gets a not found response for a sourced import.
|
|
37
|
+
- `PATCH /imports/:import_id/mapping` — saves which field each column maps to and redirects to the import's preview page, and gets a not found response for a sourced import.
|
|
38
|
+
- `GET /imports/:import_id/preview` — the preview page, titled "Preview", which shows how many rows will add a record, update one or be refused, lists each updating row and each refused row by its row number, saves nothing, and offers a "Run import" button, and which redirects to the import's result page once the import has finished or failed, or while it is running in the background, and which gets a not found response for a sourced import.
|
|
39
|
+
- `POST /imports/:import_id/run` — with no background runner, saves every row that adds or updates a record, records every refused row with its reason, marks the import finished, or failed when the run stops on an error, and redirects to the import's result page, and does nothing but redirect when the import has already finished or failed; with a background runner, marks a pending import running, hands it to the runner, and redirects to the wallflower task's page or, for the app's own runner, to the import's result page, marks the import pending again and redirects to its preview page with the message "This import did not start. Run it again." when the runner raises, and hands nothing to the runner and redirects to the import's result page when the import is already running, finished or failed; for a sourced import it gets a not found response and runs nothing.
|
|
40
|
+
- `GET /imports/:id` — the result page, titled "Import finished", which shows how many rows the run added and updated, how many data rows the file has unless the import is sourced, and lists each refused row by its row number with its reason, titled "Import running" with the message "This import is running." while a background run or a sourced import is under way, titled "Import stalled" with the message "This import has stopped making progress." once a running import has not changed for an hour, titled "Import failed" with the error the run stopped on and the counts of rows saved before it stopped when the run failed, which in every one of those states lists the records recorded against the import with `record_created`, grouped by kind with a count for each, and which redirects to the import's preview page when the import has neither run nor started running.
|
|
36
41
|
- `Ixport.register_export` — `Ixport.register_export(key, title:, exporter:, chosen: false, allow: nil)` registers one export under a key, with the title a person sees and the name of its exporter class, as a fixed export by default or as a chosen export when `chosen: true`, and optionally an access rule that decides which people may use it.
|
|
37
42
|
- `Ixport::Exporter` — the base class every exporter inherits from.
|
|
38
43
|
- `column` — `column key, label:, through: nil` is called in an exporter's class body and declares one column of the CSV, with the header a person sees, read from the record's `key` attribute, or from the `key` attribute of the related record named by `through:`.
|
|
@@ -48,9 +53,9 @@ ixport is a Rails engine that lets a signed-in person import a host app's record
|
|
|
48
53
|
|
|
49
54
|
## How to use it
|
|
50
55
|
|
|
51
|
-
Steps 1 to 24 add a mapped import, and steps 19 to 23 of those cover running imports in the background. Steps 25 to 37 add an export, fixed or chosen. Steps 38 to 44 cover the templates of a chosen export. Do step 1 first in every case.
|
|
56
|
+
Steps 1 to 24 add a mapped import, and steps 19 to 23 of those cover running imports in the background. Steps 25 to 37 add an export, fixed or chosen. Steps 38 to 44 cover the templates of a chosen export. Steps 45 to 50 add a source and open imports from it. Steps 51 to 55 record the records an import created and register how they are shown. Steps 56 to 58 open a mapped import for one chosen record. Steps 59 to 61 register a mapped import that refuses a whole file. Do step 1 first in every case.
|
|
52
57
|
|
|
53
|
-
1. Check that ixport is installed: `bin/rails routes` lists a path served by ixport. If it does not, stop and hand the setup to the `ixport-install` local before going on. For any import, also check that `db/migrate/` has an `add_error_message_to_ixport_imports` migration and that it has run, because a run that stops on an error writes the column it adds, and without it the run raises an error naming the missing column. If it does not, hand the update to the `ixport-install` local before going on. To run imports in the background, also check that `db/migrate/` has an `add_wallflower_task_to_ixport_imports` migration and that it has run, because pressing "Run import" with any background runner reads the column it adds, including with the app's own runner. If it does not, hand the update to the `ixport-install` local before going on. For a chosen export, also check that `db/migrate/` has a `create_ixport_export_templates` migration and that it has run. If it does not, hand the update to the `ixport-install` local before going on.
|
|
58
|
+
1. Check that ixport is installed: `bin/rails routes` lists a path served by ixport. If it does not, stop and hand the setup to the `ixport-install` local before going on. In every case, also check that `db/migrate/` has an `add_target_to_ixport_imports` migration and an `add_source_to_ixport_imports` migration and that both have run, because the engine's root page reads the `source` column of `ixport_imports`, every upload writes its `target` columns, and opening an import from a source saves an import with a `source` and no `key`, which only the `add_source_to_ixport_imports` migration allows. If either is missing, hand the update to the `ixport-install` local before going on. In every case, also check that `db/migrate/` has a `create_ixport_created_records` migration and that it has run, because every import's result page reads the table it adds, and without it every result page raises an error. If it does not, hand the update to the `ixport-install` local before going on. For any import, also check that `db/migrate/` has an `add_error_message_to_ixport_imports` migration and that it has run, because a run that stops on an error writes the column it adds, and without it the run raises an error naming the missing column. If it does not, hand the update to the `ixport-install` local before going on. To run imports in the background, also check that `db/migrate/` has an `add_wallflower_task_to_ixport_imports` migration and that it has run, because pressing "Run import" with any background runner reads the column it adds, including with the app's own runner. If it does not, hand the update to the `ixport-install` local before going on. For a chosen export, also check that `db/migrate/` has a `create_ixport_export_templates` migration and that it has run. If it does not, hand the update to the `ixport-install` local before going on.
|
|
54
59
|
|
|
55
60
|
### A mapped import
|
|
56
61
|
|
|
@@ -95,6 +100,7 @@ Steps 1 to 24 add a mapped import, and steps 19 to 23 of those cover running imp
|
|
|
95
100
|
- `account` is `nil` when the app has no current account, so the rule must handle that when it can happen.
|
|
96
101
|
- A person the rule refuses does not see the import on the engine's root page, does not see their own past imports of it under "Your imports", and gets a not found response from its upload, column-matching, preview, run and result pages, including for imports they uploaded before the rule refused them.
|
|
97
102
|
- The rule is checked within the person's request only. A background run already handed to a runner still runs to the end.
|
|
103
|
+
- A rule written with two arguments is called with the person and the account. A rule written with three is also called with the import's chosen record, as step 58 describes.
|
|
98
104
|
9. Start the app. If it fails to boot with `Ixport::MissingImporter`, the message names the import key and the class name that does not exist. Fix the class name in the registration, or the class name or file name of the importer, so the two match.
|
|
99
105
|
10. Visit the path the engine is mounted under while signed in. The page shows the heading "Imports" and lists each registered import the signed-in person is allowed to use by its title, in the order the imports were registered. With no import registered, or none the person is allowed to use, it shows "No imports are set up yet." Below that, once the signed-in person has uploaded any file, a "Your imports" section lists their past imports:
|
|
100
106
|
- It lists only imports uploaded by the signed-in person in the current account, newest first, of imports that are still registered and that the person is allowed to use. Imports by other people, or by the same person in another account, are not listed.
|
|
@@ -107,7 +113,7 @@ Steps 1 to 24 add a mapped import, and steps 19 to 23 of those cover running imp
|
|
|
107
113
|
<%= link_to "Import products", ixport.new_import_path(key: :products) %>
|
|
108
114
|
```
|
|
109
115
|
The key must be one that is registered. An unregistered key, or one whose rule refuses the person, gets a not found response. When the import has an `allow:` rule, show the link only to people the same rule allows.
|
|
110
|
-
12.
|
|
116
|
+
12. The upload page lists the label of every field the importer declares under "Fields this import expects", in declared order. A person chooses a CSV file and presses Upload. The upload is refused, and the upload page is shown again with status 422 and one of these messages, when:
|
|
111
117
|
- the file is not UTF-8 text: "This file is not UTF-8 text. Save it as UTF-8 and upload it again."
|
|
112
118
|
- the file is not valid CSV, such as a quote left open: "This file could not be read past line N. Check the quotes on that line."
|
|
113
119
|
- the file has no header row: "This file has no header row."
|
|
@@ -133,7 +139,7 @@ Steps 1 to 24 add a mapped import, and steps 19 to 23 of those cover running imp
|
|
|
133
139
|
- Tell the developer to cover every database constraint on the record with a validation, so a bad row is refused rather than stopping the run.
|
|
134
140
|
- An import runs once. A second run request for the same import, including one sent at the same time as the first, adds and changes nothing and redirects to the result page. A failed import is not run again, so the rows after the one it stopped on can only be loaded by uploading them as a new import.
|
|
135
141
|
18. After the run, the person is on the result page:
|
|
136
|
-
- When the run finished, the page is titled "Import finished". It shows
|
|
142
|
+
- When the run finished, the page is titled "Import finished". It shows three counts, "Added", "Updated" and "Rows in file", the number of data rows in the uploaded file, and, when any row was refused, lists each refused row under "Refused" by its row number with its reason.
|
|
137
143
|
- When the run failed, the page is titled "Import failed". It shows the error's message, the line "Rows saved before it stopped stay saved.", the "Added" and "Updated" counts of rows saved before it stopped, and each row refused before it stopped.
|
|
138
144
|
- The error's message is the exception's own message, such as a database's constraint error, so tell the developer that a person reads it as written.
|
|
139
145
|
- Returning to this page later shows the same page. Visiting it for an import that has neither run nor started running redirects to its preview page. Once an import has finished or failed, its preview page redirects to this page, so it offers no "Run import" button.
|
|
@@ -177,7 +183,8 @@ Whether imports run within the request, through wallflower, or through the app's
|
|
|
177
183
|
raise Ixport::ImportFailed, import.error_message if import.failed?
|
|
178
184
|
end
|
|
179
185
|
```
|
|
180
|
-
- When `enqueue` raises,
|
|
186
|
+
- When `enqueue` raises, the error is reported through Rails' error reporter, the import is marked pending again, and the person is taken back to its preview page with the message "This import did not start. Run it again.", where "Run import" can be pressed again.
|
|
187
|
+
- When the job never runs, the import stays running and its page shows "This import is running.". Once it has not changed for an hour, the page is titled "Import stalled" with the message "This import has stopped making progress.". Tell the developer this, and ask how they want such imports noticed.
|
|
181
188
|
23. When the developer wants to show a run's progress somewhere of their own, pass `reporter:` an object that answers all three of these, and ask the developer where the progress is shown:
|
|
182
189
|
- `set_total(total)` is called once, before the first row, with the number of data rows.
|
|
183
190
|
- `refuse(label:, reason:)` is called for each refused row, with a label such as "Row 4" and the refusal reason.
|
|
@@ -276,10 +283,111 @@ Whether imports run within the request, through wallflower, or through the app's
|
|
|
276
283
|
```
|
|
277
284
|
`new_export_template_path` takes the chosen export's registered key. An unregistered key, the key of a fixed export, or the key of an export whose rule refuses the person, gets a not found response.
|
|
278
285
|
|
|
286
|
+
### A sourced import
|
|
287
|
+
|
|
288
|
+
45. Ask the developer where the data comes from, such as another service's API, and where in the app's own code it is brought in. Do not pick either. ixport does not fetch the data, match it or save any record from it. The app's own code does all of that.
|
|
289
|
+
46. Ask the developer what key and title the source gets. The key is a short symbol that identifies the source, such as `:github_move`. The title is what a person sees for its imports on the engine's root page and their result pages, such as "GitHub move". Add the registration to the end of `config/initializers/ixport.rb`, and create the file if it does not exist:
|
|
290
|
+
```ruby
|
|
291
|
+
Ixport.register_source :github_move, title: "GitHub move"
|
|
292
|
+
```
|
|
293
|
+
Ask the developer whether every signed-in person may see this source's imports, or only some. Do not decide who may. When only some may, pass `allow:` a rule that takes the signed-in person and the current account, the same way as an import's rule in step 8:
|
|
294
|
+
```ruby
|
|
295
|
+
Ixport.register_source :github_move, title: "GitHub move",
|
|
296
|
+
allow: ->(person, account) { GithubMovePolicy.new(person, account).show? }
|
|
297
|
+
```
|
|
298
|
+
- A registration with no `allow:` is open to every signed-in person.
|
|
299
|
+
- A person the rule refuses does not see the source's imports under "Your imports", and gets a not found response from their result pages, including for imports opened for them before the rule refused them.
|
|
300
|
+
- The rule is checked on the engine's pages only. `Ixport.open_import` does not check it.
|
|
301
|
+
- A source has no class behind it, so the app's boot does not check it.
|
|
302
|
+
47. In the app's own code, at the point the data is brought in, open an import from the source for the person and account it belongs to:
|
|
303
|
+
```ruby
|
|
304
|
+
import = Ixport.open_import(:github_move, person: current_user, account: current_account)
|
|
305
|
+
```
|
|
306
|
+
- It saves the import at once, with the source's key, no file, no mapping, and the status running, and returns it.
|
|
307
|
+
- `person:` and `account:` are the person and account whose "Your imports" list shows it. Pass `account: nil` when the app has no current account.
|
|
308
|
+
- It raises `Ixport::UnknownSource`, with the message "Ixport has no source registered as <key>.", when no source is registered under the key. The message names the key, so fix the key in the call or in the registration so the two match.
|
|
309
|
+
48. A sourced import shows on the engine's pages this way:
|
|
310
|
+
- The engine's root page lists it under "Your imports" among the person's other imports, newest first, with the source's title, its status and the date it was opened, and no file name. Like every other entry, it shows "Added", "Updated" and "Refused" counts only once it is finished.
|
|
311
|
+
- Its title links to its result page, whatever its status.
|
|
312
|
+
- While it is running, the result page is titled "Import running" with the message "This import is running." and no counts.
|
|
313
|
+
- Once it has been running for an hour with no change to the import, the result page is titled "Import stalled", with the message "This import has stopped making progress." and the line "Rows saved before it stopped stay saved."
|
|
314
|
+
- A finished sourced import's result page shows the "Added" and "Updated" counts and no count of rows in a file.
|
|
315
|
+
- Its column-matching, preview and run pages get a not found response.
|
|
316
|
+
- Its result page is visible only to the person it was opened for, in the account it was opened in, while the source's `allow:` rule allows them. Anyone else gets a not found response.
|
|
317
|
+
49. ixport gives the app no call for recording a sourced import's added and updated counts, recording refused rows, or marking it finished or failed. An import opened with `Ixport.open_import` stays running, and after an hour its result page shows it as stalled. Tell the developer this before they rely on the import's status, and do not invent such a call.
|
|
318
|
+
50. Each call to `Ixport.open_import` opens a new import. Ask the developer how often their code brings data in, since every call adds one entry to that person's "Your imports".
|
|
319
|
+
|
|
320
|
+
### Records an import created
|
|
321
|
+
|
|
322
|
+
51. Ask the developer which records their own code creates while bringing data in, and where in that code each one is saved. Do not pick either. ixport records nothing on its own: rows added by a CSV import's run are not listed unless the app's code records them too.
|
|
323
|
+
52. Right after each such record is saved, record it against the import:
|
|
324
|
+
```ruby
|
|
325
|
+
import = Ixport.open_import(:github_move, person: current_user, account: current_account)
|
|
326
|
+
plan = Plan.create!(title: "Q3")
|
|
327
|
+
import.record_created(plan)
|
|
328
|
+
```
|
|
329
|
+
- The record must already be saved. An unsaved record has no id, and the call raises a database error.
|
|
330
|
+
- Recording the same record against the same import again keeps one entry and raises nothing, so code that may run twice for the same record needs no check of its own.
|
|
331
|
+
- It works on any import, sourced or uploaded, in any status.
|
|
332
|
+
- It does not change the import's status, its "Added" or "Updated" counts, or the time it last changed. A sourced import still shows as stalled an hour after it opened, however many records are recorded against it.
|
|
333
|
+
53. The import's result page lists the recorded records below everything else on it, whatever the import's status:
|
|
334
|
+
- They are grouped by the record's class, with groups ordered by class name.
|
|
335
|
+
- Each group is headed by its kind's label, or by the class name when no kind is registered, beside the number of records in the group.
|
|
336
|
+
- A record whose class has a kind is listed by the name its kind returns, linked to the path its kind returns, or unlinked when that path is `nil`.
|
|
337
|
+
- A record whose class has no kind is listed by its class name and id, such as "Plan 12", unlinked.
|
|
338
|
+
- A record deleted since it was recorded is listed by its class name and id followed by "(deleted)", such as "Plan 12 (deleted)", unlinked, and the page still opens.
|
|
339
|
+
54. For each class from step 51, ask the developer the label its group is headed by, which attribute or text names one record, and which page of the app a record's name links to. Do not pick any of them. Add the registration to the end of `config/initializers/ixport.rb`, and create the file if it does not exist:
|
|
340
|
+
```ruby
|
|
341
|
+
Ixport.register_kind "Plan", label: "Plans",
|
|
342
|
+
name: ->(plan) { plan.title },
|
|
343
|
+
path: ->(plan) { Rails.application.routes.url_helpers.plan_path(plan) }
|
|
344
|
+
```
|
|
345
|
+
- Pass the class name as a string, so the initializer does not load the class before the app's code is ready. The class itself is also accepted.
|
|
346
|
+
- Register the class the record is stored under, which for a single table inheritance subclass is its base class.
|
|
347
|
+
- `name:` and `path:` are both required. Each takes the record and is called on every visit to the result page, so neither may change anything.
|
|
348
|
+
- The `path:` lambda has no view or request around it, so build the path with `Rails.application.routes.url_helpers`, never a bare route helper. Return `nil` to leave a record unlinked.
|
|
349
|
+
- `label:`, `name:` and `path:` are shown to every person who may open the import's result page, so ask the developer whether every such person may see each record's name and open its page.
|
|
350
|
+
- Registering the same class again replaces its kind.
|
|
351
|
+
55. A kind has no class behind it to check, so the app's boot does not check it. A registration naming a class that does not exist is never used, and that class's records are listed by class name and id.
|
|
352
|
+
|
|
353
|
+
### Importing into a chosen record
|
|
354
|
+
|
|
355
|
+
56. Ask the developer whether the import adds its rows to one record the person picks, such as the questions of one quiz, and from which page of the app the person starts it. Do not decide either. Link to the upload page with that record's Global ID as `target`:
|
|
356
|
+
```erb
|
|
357
|
+
<%= link_to "Import questions", ixport.new_import_path(key: :quiz_questions, target: @quiz.to_global_id) %>
|
|
358
|
+
```
|
|
359
|
+
- The import keeps that record through upload, mapping, preview, run and result.
|
|
360
|
+
- A link naming a record that does not exist gets a not found response.
|
|
361
|
+
- A link with no `target` opens an import with no chosen record.
|
|
362
|
+
57. In the importer, read the chosen record as `target` inside `records`:
|
|
363
|
+
```ruby
|
|
364
|
+
def records
|
|
365
|
+
target.questions
|
|
366
|
+
end
|
|
367
|
+
```
|
|
368
|
+
`target` is `nil` for an import opened with no chosen record, so a `records` that calls a method on `target` fails in that case. Ask the developer whether the import is ever opened without one.
|
|
369
|
+
58. Anyone can edit the link to name any record of any class, so the registration's `allow:` rule decides which records a person may import into. Ask the developer which records each person may import into. Do not decide. Write the rule with a third argument, which is the chosen record, or `nil` when there is none:
|
|
370
|
+
```ruby
|
|
371
|
+
Ixport.register_import :quiz_questions, title: "Quiz questions", importer: "QuizQuestionImporter",
|
|
372
|
+
allow: ->(person, account, quiz) { quiz.nil? || (quiz.is_a?(Quiz) && quiz.account == account) }
|
|
373
|
+
```
|
|
374
|
+
- Check the record's class as well as its owner, since the link can name a record of any class.
|
|
375
|
+
- A link or upload naming a record the rule refuses gets a not found response, and so does every page of an import whose chosen record it refuses.
|
|
376
|
+
- The engine's root page calls the rule with `nil`, so a rule that refuses `nil` hides the import from that page.
|
|
377
|
+
|
|
378
|
+
### Refusing a whole file
|
|
379
|
+
|
|
380
|
+
59. Ask the developer whether a file with any refused row should save nothing, so a partial import never leaves only some of a file's rows added. Do not decide. When it should, add `refuse_whole_file: true` to the registration:
|
|
381
|
+
```ruby
|
|
382
|
+
Ixport.register_import :quiz_questions, title: "Quiz questions", importer: "QuizQuestionImporter", refuse_whole_file: true
|
|
383
|
+
```
|
|
384
|
+
60. While any row is refused, the preview page offers no "Run import" button and shows "This import saves no row while any row is refused. Fix the refused rows in the file and upload it again." A run of such a file, within the request or in the background, saves no row, records each refused row, and counts no row added or updated.
|
|
385
|
+
61. When every row is valid, every row is saved as in step 17. When the run stops on an error, every row it saved and every refusal it recorded is undone, the import is marked failed with the error's message, and it counts no row added or updated.
|
|
386
|
+
|
|
279
387
|
## Conventions
|
|
280
388
|
|
|
281
389
|
- Every registration names an importer or exporter class that exists. The app refuses to boot otherwise, so never register an import or export before its class is written.
|
|
282
|
-
- Each import key
|
|
390
|
+
- Each import key, each export key and each source key is registered once. Registering the same key a second time replaces the first registration.
|
|
283
391
|
- Every importer declares `match_on` with at least one field and defines `records`.
|
|
284
392
|
- Every exporter declares at least one `column` and defines `records`.
|
|
285
393
|
- `records` is always scoped to the `person` or `account` the developer chose, never left to the whole table without the developer saying so.
|
|
@@ -288,7 +396,11 @@ Whether imports run within the request, through wallflower, or through the app's
|
|
|
288
396
|
- When a record's attribute or association is renamed or removed, update the matching `field`, `match_on` and `column` lines in the same change.
|
|
289
397
|
- When an importer or exporter class is renamed, update its `importer:` or `exporter:` string in `config/initializers/ixport.rb` in the same change.
|
|
290
398
|
- When an import's or export's key is changed, update every link to its upload page or download in the same change.
|
|
291
|
-
- Past imports record their import's key. Before changing an import's key or removing its registration, tell the developer that past imports under the old key drop off "Your imports" on the engine's root page, and that
|
|
399
|
+
- Past imports record their import's key, and sourced imports record their source's key. Before changing an import's or a source's key or removing its registration, tell the developer that past imports under the old key drop off "Your imports" on the engine's root page, and that their pages get a not found response, and ask what should happen to those imports.
|
|
400
|
+
- When a source's key is changed, update every `Ixport.open_import` call that names it in the same change.
|
|
401
|
+
- When a class with a registered kind is renamed, update its `Ixport.register_kind` line in the same change. Before renaming or removing any class whose records were recorded with `record_created`, tell the developer that the records already recorded under the old class name keep that name, and that the result page of an import holding them raises an error once no class has that name, and ask what should happen to those records.
|
|
402
|
+
- Call `record_created` only after the record is saved, and never write a table, model or page of the app's own for the records an import created. The engine lists them on the import's result page.
|
|
403
|
+
- An import's chosen record is only ever trusted through its registration's three-argument `allow:` rule. Never look one up from a parameter in the app's own code.
|
|
292
404
|
- Every engine page first runs the before actions of the app controller ixport's pages inherit from, which is set by `ixport-install`, so a person that controller turns away never reaches an import or export. Among the people it lets through, who may use an import or export is decided only by its `allow:` rule. Never write a before action, a policy check or a redirect in front of the engine's pages.
|
|
293
405
|
- When an import or export has an `allow:` rule, every link to it from the app's own pages is shown only to people the same rule allows.
|
|
294
406
|
- Saved templates record a chosen export's key and its picked column keys. Before changing a chosen export's key, removing its registration, or changing it to fixed, ask the developer what happens to the templates already saved for it:
|
|
@@ -299,4 +411,4 @@ Whether imports run within the request, through wallflower, or through the app's
|
|
|
299
411
|
- The app's own runner only starts a job, and the job only calls `Ixport.run_import`. Never write a job that reads the file, matches rows or saves records itself.
|
|
300
412
|
- When the app's own runner class is renamed, tell the developer that the class name `ixport-install` set must be changed in the same change.
|
|
301
413
|
- Never wrap `Ixport.run_import` in a rescue, a transaction or a retry of the app's own. It records its own failure, keeps the rows saved before it, and reports the error.
|
|
302
|
-
- Installing ixport, running the update generator for the export templates table, the background runner's column
|
|
414
|
+
- Installing ixport, running the update generator for the export templates table, the background runner's column, the failed import's error column, the `target` and `source` columns of `ixport_imports`, or the table of the records an import created, subscribing an error service to Rails' error reporter, mounting the engine, the tables an import, its refused rows, the records it created and export templates are stored in, file storage, sign-in, how the current person and account are found, the layout, the inline row limit, which of the app's controllers the engine's pages inherit from, and choosing whether imports run within the request, through wallflower or through the app's own runner class are out of scope for this local and belong to `ixport-install`.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: ixport-info
|
|
3
|
-
description: Use to learn what ixport offers — CSV import and export through a mounted Rails engine, the mapped imports and the fixed and chosen exports a host app registers, the importer and exporter classes behind them, uploading a file, matching its columns, previewing which rows will add, update or be refused, running the import inside the request or in the background through wallflower or the app's own runner, its result page, what a person sees when a file is refused
|
|
3
|
+
description: Use to learn what ixport offers — CSV import and export through a mounted Rails engine, the mapped imports and the fixed and chosen exports a host app registers, the importer and exporter classes behind them, uploading a file, matching its columns, previewing which rows will add, update or be refused, importing into a record the app chooses, imports that save no row while any row is refused, running the import inside the request or in the background through wallflower or the app's own runner, its result page, what a person sees when a file is refused, a run fails or stalls, imports the app opens from a source other than a file, the records an import created listed on its result page by kind, the list of a person's past imports, downloading an export as a CSV file, saving named templates of picked columns for a chosen export, limiting each import, source or export to the people a rule allows, and the vocabulary the install and develop locals assume.
|
|
4
4
|
tools: Read
|
|
5
5
|
scope: CSV import and export — a mounted engine where a signed-in person sees the mapped imports and the exports the host app registers, each import backed by an importer class and each export by an exporter class the host writes, where an export is either fixed or chosen, and a chosen export lets the account save named templates of picked columns
|
|
6
6
|
---
|
|
@@ -11,61 +11,75 @@ This local explains what ixport is and routes you to the local that does the wor
|
|
|
11
11
|
|
|
12
12
|
ixport is a Rails engine for loading a host app's records from CSV files and downloading them back out as CSV files. The host app mounts it at a path of its choosing. A signed-in person who visits that path sees the list of imports the host has set up, and a signed-in person who visits its exports page sees the list of exports, each shown by its title.
|
|
13
13
|
|
|
14
|
-
A person picks an import,
|
|
14
|
+
A person picks an import and reaches its upload page, which lists the fields the import expects by their labels. They upload a CSV file, match each column of the file to one of those fields or skip it, and then see a preview. The preview counts how many rows will add a record, how many will update one, and how many will be refused, and lists the rows that will update and the rows that will be refused with the reason for each. Viewing the preview saves nothing in the host app, and a Back link returns to the mapping so the person can change it.
|
|
15
15
|
|
|
16
|
-
From the preview the person runs the import. The run saves every added and updated record in the host app, records each refused row with its reason, and sends the person to the import's result page
|
|
16
|
+
From the preview the person runs the import. The run saves every added and updated record in the host app, records each refused row with its reason, and sends the person to the import's result page. That page shows the import's status, how many rows were added and updated, how many data rows the file had, and each refused row. An import that has finished is never run again. Opening the preview of a running or finished import sends the person to its result page instead, so no Run button is offered for it, and opening the result page of an import that has not started running sends the person to its preview.
|
|
17
|
+
|
|
18
|
+
The host can register an import as one that refuses the whole file. Such an import saves no row from a file with any refused row: its preview offers no Run button and tells the person to fix the refused rows and upload the file again. When its run stops on an error, every row it saved is undone.
|
|
19
|
+
|
|
20
|
+
The host can also link to an import's upload page naming one of its own records, such as a project, for the rows to be added to. The upload, mapping, preview and run carry that record through, and the importer is given it. A link naming a record that does not exist, or one the import's access rule refuses, is answered not found.
|
|
17
21
|
|
|
18
22
|
A run that stops on an error is marked Failed. Every row saved before the error stays saved, and the result page is headed Import failed, shows the error's message, and shows how many rows were added and updated before it stopped along with each refused row. ixport also reports the error to the host app's error reporting. A failed import is never run again.
|
|
19
23
|
|
|
20
|
-
Where the run happens depends on the host's background runner. With none, the run happens inside the request, as above. With one, every import runs through it whatever its size: running the import marks it Running and hands it to the runner, and its result page says the import is running until the run finishes. An import is handed to the runner only once: asking to run it again while it is running, or after it has finished, sends the person to its result page.
|
|
24
|
+
Where the run happens depends on the host's background runner. With none, the run happens inside the request, as above. With one, every import runs through it whatever its size: running the import marks it Running and hands it to the runner, and its result page says the import is running until the run finishes. An import is handed to the runner only once: asking to run it again while it is running, or after it has finished, sends the person to its result page. When the runner raises while being handed the import, the import goes back to Pending and its preview says it did not start, so the person can run it again. An import still Running an hour after its last change shows on its result page as stalled, and the rows it saved stay saved.
|
|
21
25
|
|
|
22
26
|
- **Wallflower** — the host can choose wallflower as the runner. Running an import then starts a wallflower task and takes the person to the task's page, which shows the run's progress and each refused row with its reason, and links to the import's result page once the run finishes. When the run fails, the task is marked failed with the import's error message and still links to the import's result page.
|
|
23
27
|
- **The app's own runner** — the host can instead name a class of its own. ixport hands each import to that class, and the class runs it later by calling ixport back, after which the result page shows the counts and refused rows as usual. Running the import takes the person to the import's result page.
|
|
24
28
|
|
|
25
29
|
ixport refuses a file on the upload page, before any import record is kept, when it is not UTF-8 text, when a line's quotes stop it being read, naming that line, or when it has no header row. With no background runner, it also refuses a file with more rows than the host's row limit there. Each refusal says what is wrong with the file.
|
|
26
30
|
|
|
27
|
-
|
|
31
|
+
Data an app brings in from somewhere other than an uploaded file, such as another service's API, can still be recorded as an import. The host registers a source with a title, and its own code opens an import from that source for a person and an account. A sourced import has no file, no mapping and no preview, and it is Running from the moment it opens. Its mapping, preview and run pages answer not found, and its result page shows the added and updated counts with no count of rows in a file. This version of ixport gives the host no call for recording a sourced import's counts or marking it finished.
|
|
32
|
+
|
|
33
|
+
The host's code can record each of its own records that an import created, against that import, whether the import came from a file or a source. Recording the same record against the same import twice keeps it once. The import's result page lists every recorded record, whatever the import's status, grouped by kind, with each group headed by the kind's label and showing how many records it holds. The host registers a kind for each of its record classes, giving a label and saying how to name a record and which page to link it to. A record whose class has no kind registered is listed by its class name and id, unlinked, and a record deleted since it was recorded is listed by its class name and id, marked deleted.
|
|
34
|
+
|
|
35
|
+
Below the list of imports, the same page lists the person's own past imports in the current account, newest first, sourced imports among them. Each shows the import's title, or the source's title for a sourced import, the uploaded file's name when it has one, its status, the date it was started, and, once it has finished, how many rows were added, updated and refused. A failed import shows its status as Failed and no counts. Each title opens the import at the step it has reached: the mapping when no mapping is saved, the preview when it has a mapping and has not started running, and the result page once it is running, finished or failed. A sourced import always opens at its result page. Imports started by other people, or by the same person in another account, are not listed, and neither are the person's imports of a type or source their access rule now refuses.
|
|
28
36
|
|
|
29
37
|
An export is either fixed or chosen. A fixed export's title on the exports page links to a CSV download holding every column the exporter declares. A chosen export's title is not a link. Under it the page lists the templates the account has saved for that export, each linking to a download and to its edit page, with a button to make a new template.
|
|
30
38
|
|
|
31
|
-
A template is a name and an ordered pick of the exporter's columns. A person makes one on its own page, entering a name and choosing a column, or None, for each slot. Its download holds only the picked columns, in the order picked, and the file is named after the template. On the edit page a person can change the name and the picked columns, or delete the template. A template belongs to the account, so everyone working in that account sees, downloads, edits and deletes the same templates.
|
|
39
|
+
A template is a name and an ordered pick of the exporter's columns. A person makes one on its own page, entering a name and choosing a column, or None, for each slot. A template cannot be saved without a name. Its download holds only the picked columns, in the order picked, and the file is named after the template. On the edit page a person can change the name and the picked columns, or delete the template. A template belongs to the account, so everyone working in that account sees, downloads, edits and deletes the same templates.
|
|
32
40
|
|
|
33
41
|
Every export file's first row holds the column labels, and each later row holds one of the host's records. The host decides which records an export holds, so an export can be limited to the person's current account. The file is built inside the request and nothing about it is stored.
|
|
34
42
|
|
|
35
|
-
By default every signed-in person can use every import and export. The host can give any
|
|
43
|
+
By default every signed-in person can use every import, source and export. The host can give any of them an access rule, which is handed the signed-in person and the current account and allows or refuses them, and which can ask the app's own permission checks, such as a Pundit policy. An import's rule can also be handed the record the import adds its rows to, and refuse that record. A person the rule refuses does not see that import or export listed, does not see their past imports of it or from that source, and is answered not found on every one of its pages and downloads, including its upload page, an existing import's mapping, preview, run and result pages, and a chosen export's template pages and downloads. The rule is checked again on every request, so a person whose permissions change is let in or kept out from their next page.
|
|
36
44
|
|
|
37
|
-
The engine owns the screens, the access check, and its own tables of import records, refused rows and export templates, with the uploaded file stored through Active Storage. The host owns what each import and export means: which columns it reads or writes, which column or columns identify an existing record, and which of the app's records a row is checked against, saved into, or read from. Reach for ixport when a Rails app needs people to load data from spreadsheets or take it out into one, rather than writing a one-off upload or download page per record type.
|
|
45
|
+
The engine owns the screens, the access check, and its own tables of import records, refused rows, the records an import created and export templates, with the uploaded file stored through Active Storage. The host owns what each import and export means: which columns it reads or writes, which column or columns identify an existing record, and which of the app's records a row is checked against, saved into, or read from. Reach for ixport when a Rails app needs people to load data from spreadsheets or take it out into one, rather than writing a one-off upload or download page per record type.
|
|
38
46
|
|
|
39
|
-
The engine renders its pages with Keystone UI inside a layout the host chooses, and it runs its access check through a method the host already has. Its pages inherit from a controller the host names, which is the host's own application controller unless the host names another, so the filters, helpers and error handling the host puts on that controller apply to ixport's pages as well.
|
|
47
|
+
The engine renders its pages with Keystone UI inside a layout the host chooses, and it runs its access check through a method the host already has. Its pages inherit from a controller the host names, which is the host's own application controller unless the host names another, so the filters, helpers and error handling the host puts on that controller apply to ixport's pages as well. The host's layout can link with the host app's own route helpers around ixport's pages.
|
|
40
48
|
|
|
41
49
|
## Interface
|
|
42
50
|
|
|
43
51
|
ixport declares nothing for this local to document. Its surface is split between the other two locals:
|
|
44
52
|
|
|
45
53
|
- **ixport-install** owns hooking ixport into a host app: adding the gem, running its install generator, running its update generator to bring an existing install's tables up to date, mounting the engine, and the configuration that names the host's sign-in check, current person, current account, layout, row limit, background runner and the controller ixport's pages inherit from.
|
|
46
|
-
- **ixport-develop** owns building against ixport: registering a mapped import and the importer class behind it, registering a fixed or chosen export and the exporter class behind it, giving an import or export an access rule, writing the app's own background runner and running an import from it, the error a wallflower task raises when its import fails, the errors raised when a registration names a class that does not exist, and the engine's pages for imports, exports and export templates.
|
|
54
|
+
- **ixport-develop** owns building against ixport: registering a mapped import and the importer class behind it, making an import refuse the whole file, linking an import to a record its rows are added to, registering a fixed or chosen export and the exporter class behind it, registering a source and opening an import from it, recording the records an import created and registering the kinds that say how they are listed, giving an import, source or export an access rule, writing the app's own background runner and running an import from it, the error a wallflower task raises when its import fails, the errors raised when a registration names a class that does not exist or an import is opened from a source that is not registered, and the engine's pages for imports, exports and export templates.
|
|
47
55
|
|
|
48
56
|
## How to use it
|
|
49
57
|
|
|
50
|
-
- The app does not have ixport yet, the app installed ixport before export templates
|
|
51
|
-
- ixport is already installed and you are adding or changing an import or an export, making an export chosen or fixed, changing how an import's rows are matched, checked or saved, changing which records or columns an export holds, limiting an import or export to some people, writing the app's own runner that runs imports in the background, linking to the engine's pages, or the app fails to boot naming a missing importer or exporter: use **ixport-develop**.
|
|
58
|
+
- The app does not have ixport yet, the app installed ixport before export templates, background runs, chosen records, sources or the records an import created existed and needs their tables, you are choosing whether imports run inside the request, through wallflower or through the app's own runner, or you are changing how ixport reaches the app's sign-in, people, accounts, layout or row limit, or which of the app's controllers its pages inherit from: use **ixport-install**.
|
|
59
|
+
- ixport is already installed and you are adding or changing an import or an export, making an export chosen or fixed, making an import refuse the whole file, importing into a record the app chooses, recording data brought in from another service as an import, recording which records an import created or changing how they are listed, changing how an import's rows are matched, checked or saved, changing which records or columns an export holds, limiting an import, source or export to some people, writing the app's own runner that runs imports in the background, linking to the engine's pages, or the app fails to boot naming a missing importer or exporter: use **ixport-develop**.
|
|
52
60
|
|
|
53
61
|
## Conventions
|
|
54
62
|
|
|
55
63
|
- **Mapped import** — one type of import the host registers, made of a key, a title shown to people, and the name of its importer class.
|
|
64
|
+
- **Whole-file import** — a mapped import the host registers as refusing the whole file, which saves no row while any row is refused and undoes its saved rows when its run stops on an error.
|
|
65
|
+
- **Chosen record** — one of the host's own records named in the link to an import's upload page, which the import's rows are added to and which its importer and access rule are given.
|
|
66
|
+
- **Source** — somewhere other than an uploaded file that the host brings data in from, registered with a key, a title and an optional access rule.
|
|
67
|
+
- **Sourced import** — an import record the host's code opens from a source for a person and an account, with no file, no mapping and no preview, running from the moment it opens.
|
|
68
|
+
- **Created record** — one of the host's own records that the host's code recorded against an import as created by it, kept once per import and listed on that import's result page.
|
|
69
|
+
- **Kind** — what the host registers for one of its record classes to say how its created records are listed: a label heading the group, how to name each record, and the page each record links to.
|
|
56
70
|
- **Fixed export** — one type of export the host registers, made of a key, a title shown to people, and the name of its exporter class, whose download always holds every declared column. An export is fixed unless the host registers it as chosen.
|
|
57
71
|
- **Chosen export** — an export registered the same way and marked as chosen, which has no download of its own and is downloaded only through the account's templates.
|
|
58
72
|
- **Export template** — a named, ordered pick of a chosen export's columns, saved by one person and shared by everyone in their account.
|
|
59
73
|
- **Picked columns** — the columns a template holds, in the order they were chosen. A slot left on None is dropped, a column picked twice is kept once, and a column the exporter no longer declares is left out of the download.
|
|
60
|
-
- **Key** — the short name an import or export is registered and looked up by, held as a symbol. A fixed export's file is named after its key.
|
|
61
|
-
- **Title** — the words a person sees for the import or export on the engine's pages.
|
|
74
|
+
- **Key** — the short name an import, source or export is registered and looked up by, held as a symbol. A fixed export's file is named after its key.
|
|
75
|
+
- **Title** — the words a person sees for the import, source or export on the engine's pages.
|
|
62
76
|
- **Importer** — a class the host app writes that declares the columns the import reads, each with a key and a label, names the column or columns that together identify an existing record, and supplies the set of the app's records the rows are checked against and saved into.
|
|
63
77
|
- **Exporter** — a class the host app writes that declares the columns the export writes, each with a key and a label, and supplies the set of the app's records the file holds.
|
|
64
78
|
- **Export column** — one column of an export's file, headed by its label and filled from the attribute its key names on each record.
|
|
65
79
|
- **Through** — an export column can be read from a record related to each row's record rather than from the record itself, and it is left blank when that related record is missing.
|
|
66
80
|
- **Match column** — a declared import column whose value decides whether a row is an existing record or a new one. An import can have one or several, and a row is an existing record only when it equals that record on every one of them.
|
|
67
81
|
- **Matched value** — a row's value in a match column, compared as the record's own type rather than as text, so 100.0 in a file matches 100 stored on a record.
|
|
68
|
-
- **Import record** — one
|
|
82
|
+
- **Import record** — one import by one person, stored by the engine with either the key of its mapped import, the uploaded file, its chosen record if any and the mapping once it is saved, or the key of its source, and the added and updated counts once it has run.
|
|
69
83
|
- **Headers** — the first row of the uploaded file, read as the names of its columns.
|
|
70
84
|
- **Mapping** — which of the importer's columns each header in the file fills, stored by the header's position in the row. A header left on "Skip" is not stored.
|
|
71
85
|
- **Row limit** — the most data rows an uploaded file may have, not counting the header row. It defaults to 1,000 and the host can change it. It applies only when the host has no background runner, and it does not apply to exports.
|
|
@@ -76,19 +90,20 @@ ixport declares nothing for this local to document. Its surface is split between
|
|
|
76
90
|
- **Update** — a row that equals an existing record on every match column, so it changes that record.
|
|
77
91
|
- **Refused** — a row whose record, new or changed, fails the host model's own validations. Its reason is that model's error messages.
|
|
78
92
|
- **Preview** — the page that shows the add, update and refused counts and lists the update and refused rows by row number. It builds each row's record in memory and saves none of them.
|
|
79
|
-
- **Run** — the step that saves the added and updated records one row at a time and stores each refused row's number and reason. A run that raises an error stops there and keeps the rows it already saved, and an import that has finished or failed is never run a second time.
|
|
80
|
-
- **Status** — an import record is Pending until it is run, Running while a background runner has it, and then Finished, or Failed when the run stopped on an error.
|
|
81
|
-
- **Running** — the state of an import record handed to a background runner and not yet finished. Its result page says it is running and shows no counts, its preview sends the person to that result page, and it is never handed to the runner a second time.
|
|
93
|
+
- **Run** — the step that saves the added and updated records one row at a time and stores each refused row's number and reason. A run that raises an error stops there and keeps the rows it already saved, unless the import refuses the whole file, and an import that has finished or failed is never run a second time.
|
|
94
|
+
- **Status** — an import record is Pending until it is run, Running while a background runner has it or from the moment a sourced import opens, and then Finished, or Failed when the run stopped on an error.
|
|
95
|
+
- **Running** — the state of an import record handed to a background runner, or opened from a source, and not yet finished. Its result page says it is running and shows no counts, its preview sends the person to that result page, and it is never handed to the runner a second time.
|
|
96
|
+
- **Stalled** — a Running import record with no change for an hour, which its result page shows as having stopped making progress.
|
|
82
97
|
- **Finished** — the state of an import record once it has run to the end.
|
|
83
98
|
- **Failed** — the state of an import record whose run stopped on an error. It keeps the error's message and the added and updated counts reached before it stopped, and its result page shows them.
|
|
84
99
|
- **Refused file** — an upload ixport turns away on the upload page because it is not UTF-8, cannot be read past a line, has no header row, or has too many rows. Nothing is stored for it, unlike a refused row, which belongs to an import that ran.
|
|
85
|
-
- **Past imports** — the person's own import records in the current account, listed newest first under the imports on the engine's front page.
|
|
86
|
-
- **Step** — how far an import record has got: mapping when no mapping is saved, preview once one is, and result once it is running, finished or failed.
|
|
87
|
-
- **Result page** — the import's own page, showing the added and updated counts and each refused row as they were stored by the run,
|
|
100
|
+
- **Past imports** — the person's own import records in the current account, file and sourced alike, listed newest first under the imports on the engine's front page.
|
|
101
|
+
- **Step** — how far an import record has got: mapping when no mapping is saved, preview once one is, and result once it is running, finished or failed. A sourced import is always at the result step.
|
|
102
|
+
- **Result page** — the import's own page, showing its status, the added and updated counts, the number of data rows in its file when it has one, and each refused row as they were stored by the run, the error's message when the run failed, and the records recorded as created by the import, grouped by kind, so it shows the same thing each time it is opened.
|
|
88
103
|
- **Person** — whoever is signed in and running the import or downloading the export. ixport reaches them through a method the host names, so the host's user model needs no particular name.
|
|
89
104
|
- **Parent controller** — the host app's controller that every ixport page inherits from, the app's application controller by default. Whatever it adds to every request, such as a filter, a helper or a rescue, runs on ixport's pages too, and the sign-in check, current person and current account methods ixport calls are looked up on it.
|
|
90
105
|
- **Account** — the optional owner the person is working inside, such as a team or company, reached the same way. An app with no accounts runs without one. Both the importer and the exporter are given the person and the account.
|
|
91
|
-
- **Ownership** — an import record belongs to the person and account
|
|
92
|
-
- **Access rule** — an optional check the host gives one import or export when registering it, handed the person and the account and answering whether they may use it. It is checked on top of ownership, so even the person who
|
|
106
|
+
- **Ownership** — an import record belongs to the person and account it was started for, and anyone else who opens it, including the same person from another account, is answered not found. An export template belongs to its account, and anyone opening it from another account is answered not found.
|
|
107
|
+
- **Access rule** — an optional check the host gives one import, source or export when registering it, handed the person and the account, and for an import optionally its chosen record, and answering whether they may use it. It is checked on top of ownership, so even the person who started an import is answered not found for it once the rule refuses them. An import, source or export with no rule is open to every signed-in person.
|
|
93
108
|
- **Boot check** — when the app starts, ixport checks that every registered importer and exporter class exists and refuses to boot when one does not, so a misspelled class name fails at startup rather than when someone runs the import or downloads the export.
|
|
94
|
-
- Imports and exports are registered by the host app, never discovered: an importer or exporter class that is not registered does not appear on the page, asking for an export key
|
|
109
|
+
- Imports, sources and exports are registered by the host app, never discovered: an importer or exporter class that is not registered does not appear on the page, asking for an import, export or template whose key is not registered is answered not found, opening an import from a source that is not registered raises an error naming it, and asking to make a template for an export that is not chosen is answered not found.
|
|
@@ -14,8 +14,8 @@ ixport is a Rails engine with two pages of work for a signed-in person. On the i
|
|
|
14
14
|
## Interface
|
|
15
15
|
|
|
16
16
|
- `gem "ixport"` — the Gemfile line that adds ixport to the app. It also brings in Keystone UI, which draws ixport's pages.
|
|
17
|
-
- `bin/rails generate ixport:install` — copies
|
|
18
|
-
- `bin/rails generate ixport:update` — copies the
|
|
17
|
+
- `bin/rails generate ixport:install` — copies eight migrations into `db/migrate/`: one that creates the `ixport_imports` table, one that creates the `ixport_import_refusals` table, one that creates the `ixport_export_templates` table, one that adds a `wallflower_task_id` column to `ixport_imports`, one that adds an `error_message` column to `ixport_imports`, one that adds a polymorphic `target` reference to `ixport_imports`, one that adds a `source` column to `ixport_imports` and lets its `key` be empty, and one that creates the `ixport_created_records` table.
|
|
18
|
+
- `bin/rails generate ixport:update` — copies the six migrations added since ixport first shipped, the one that creates the `ixport_export_templates` table, the one that adds the `wallflower_task_id` column, the one that adds the `error_message` column, the one that adds the `target` reference, the one that adds the `source` column and the one that creates the `ixport_created_records` table, into an app that installed an earlier ixport.
|
|
19
19
|
- `mount Ixport::Engine` — the line in `config/routes.rb` that serves ixport's pages under a path the app chooses. The imports page is at that path and the exports page is at that path followed by `/exports`.
|
|
20
20
|
- `Ixport.configure` — takes a block that receives the configuration object, where the app sets any of the seven settings below.
|
|
21
21
|
- `config.authentication_method` — the name of the controller method run before every ixport page, imports, exports and export templates alike, as a symbol. It defaults to `:authenticate_user!`.
|
|
@@ -45,12 +45,15 @@ ixport is a Rails engine with two pages of work for a signed-in person. On the i
|
|
|
45
45
|
```bash
|
|
46
46
|
bin/rails generate ixport:install
|
|
47
47
|
```
|
|
48
|
-
It writes
|
|
48
|
+
It writes eight files and touches no other:
|
|
49
49
|
- `db/migrate/<timestamp>_create_ixport_imports.rb` creates the `ixport_imports` table with a required `key` string, a required polymorphic `person` reference, an optional polymorphic `account` reference, a `mapping` JSON column, a required `status` string defaulting to `"pending"`, required `added_count` and `updated_count` integers defaulting to `0`, and timestamps.
|
|
50
50
|
- `db/migrate/<timestamp>_create_ixport_import_refusals.rb` creates the `ixport_import_refusals` table with a required `import` reference with a foreign key to `ixport_imports`, a required `row_number` integer, a required `reason` text, and timestamps.
|
|
51
51
|
- `db/migrate/<timestamp>_create_ixport_export_templates.rb` creates the `ixport_export_templates` table with a required `key` string, a required `name` string, a required `columns` JSON column, a required polymorphic `person` reference, an optional polymorphic `account` reference, and timestamps.
|
|
52
52
|
- `db/migrate/<timestamp>_add_wallflower_task_to_ixport_imports.rb` adds an optional `wallflower_task_id` bigint column to `ixport_imports`, with an index. It is copied whether or not the app uses wallflower.
|
|
53
53
|
- `db/migrate/<timestamp>_add_error_message_to_ixport_imports.rb` adds an optional `error_message` text column to `ixport_imports`, which holds the error an import stopped on when it fails.
|
|
54
|
+
- `db/migrate/<timestamp>_add_target_to_ixport_imports.rb` adds an optional polymorphic `target` reference to `ixport_imports`, which holds the app's own record that an import's rows are added to, when the link to the import's upload page names one.
|
|
55
|
+
- `db/migrate/<timestamp>_add_source_to_ixport_imports.rb` adds an optional `source` string column to `ixport_imports` and lets its `key` column be empty, so an import the app opens from a source can be stored with no import key.
|
|
56
|
+
- `db/migrate/<timestamp>_create_ixport_created_records.rb` creates the `ixport_created_records` table with a required `import` reference with a foreign key to `ixport_imports`, a required polymorphic `record` reference, and timestamps, with a unique index on the import and the record together. It holds the app's own records an import created, which the import's result page lists.
|
|
54
57
|
6. Run the migrations:
|
|
55
58
|
```bash
|
|
56
59
|
bin/rails db:migrate
|
|
@@ -90,16 +93,16 @@ ixport is a Rails engine with two pages of work for a signed-in person. On the i
|
|
|
90
93
|
```
|
|
91
94
|
The values above are examples, and each line is written only when the developer's answer differs from the default. Ask about `config.inline_row_limit` only when imports run inside the request and the developer wants a limit other than 1,000 rows per import file, since it has no effect with a background runner.
|
|
92
95
|
12. Start the app and visit the mounted path while signed in. The page shows the heading "Imports" and the message "No imports are set up yet." It shows no "Your imports" list, since that list appears only once the signed-in person has uploaded an import file. Then visit the mounted path followed by `/exports`. The page shows the heading "Exports" and the message "No exports are set up yet." Visiting either while signed out sends the browser through the app's own sign-in check. The install is done, so skip step 13.
|
|
93
|
-
13. For an app that already has ixport, check whether `db/migrate/` has a `create_ixport_export_templates` migration, an `add_wallflower_task_to_ixport_imports` migration
|
|
96
|
+
13. For an app that already has ixport, check whether `db/migrate/` has every migration the update generator copies: a `create_ixport_export_templates` migration, an `add_wallflower_task_to_ixport_imports` migration, an `add_error_message_to_ixport_imports` migration, an `add_target_to_ixport_imports` migration, an `add_source_to_ixport_imports` migration and a `create_ixport_created_records` migration. If it has all six, the app is up to date and there is nothing to run. If any is missing, run `bundle update ixport`, then run the update generator and the migrations:
|
|
94
97
|
```bash
|
|
95
98
|
bin/rails generate ixport:update
|
|
96
99
|
bin/rails db:migrate
|
|
97
100
|
```
|
|
98
|
-
Until the `error_message` migration has run, an import that fails cannot record its error and the run raises an error naming the missing column. The generator copies whichever of the
|
|
101
|
+
Until the `error_message` migration has run, an import that fails cannot record its error and the run raises an error naming the missing column. Until the `target` and `source` migrations have run, the imports page and every upload raise an error, because both read or write the columns those migrations add. Until the created records migration has run, every import's result page raises an error naming the missing `ixport_created_records` table. The generator copies whichever of the six migrations step 5 describes are missing, reports each one already present as identical, and touches no other file. Then read the app's `config/initializers/ixport.rb`, or the defaults if it has none. Where the export templates migration was just added, put the current account part of step 9 to the developer, since export templates are shared by that account. Where `config.background_runner` is not set, put step 10 to the developer. Where `config.base_controller` is not set, put step 8 to the developer. Visit the mounted path followed by `/exports` while signed in and check that the page still loads. Then visit the mounted path itself and check that the imports page loads, with a "Your imports" list of the person's earlier imports when they have any. An earlier import of a type whose access rule refuses the signed-in person is left off that list. Access rules need no migration and no setting.
|
|
99
102
|
|
|
100
103
|
## Conventions
|
|
101
104
|
|
|
102
|
-
- After installing or updating, check that `bin/rails db:migrate:status` lists the create-ixport-imports, create-ixport-import-refusals, create-ixport-export-templates, add-wallflower-task-to-ixport-imports
|
|
105
|
+
- After installing or updating, check that `bin/rails db:migrate:status` lists the create-ixport-imports, create-ixport-import-refusals, create-ixport-export-templates, add-wallflower-task-to-ixport-imports, add-error-message-to-ixport-imports, add-target-to-ixport-imports, add-source-to-ixport-imports and create-ixport-created-records migrations as up, that `bin/rails routes` lists the mount path, and that both pages load as described in step 12. With `config.background_runner = :wallflower`, also check that `bin/rails routes` lists ixport's mount under the name `ixport` and wallflower's mount under the name `wallflower`.
|
|
103
106
|
- Run the install generator once, and the update generator only on an app installed with an earlier ixport. A second run of either against unchanged copies reports each migration as identical and adds nothing. If a copied migration differs from the one the generator would write, the run stops with an error naming that migration, and copies nothing after it. Do not delete or edit a copied migration once it has been run.
|
|
104
107
|
- An import that stops on an error is marked failed, keeps the rows it saved before stopping, and reports the error through Rails' error reporter, so it reaches whatever error service the app has subscribed there. ixport needs no setting for this, and an app with no subscriber records the error only on the import's page.
|
|
105
108
|
- Set `config.base_controller` only in `config/initializers/ixport.rb`. ixport reads it once, when its controllers load, so a value set anywhere later has no effect. A name that is not a controller in the app raises a `NameError` naming it when ixport's controllers load.
|
data/the_local/interface.yml
CHANGED
|
@@ -16,6 +16,11 @@ install:
|
|
|
16
16
|
|
|
17
17
|
develop:
|
|
18
18
|
- Ixport.register_import
|
|
19
|
+
- Ixport.register_source
|
|
20
|
+
- Ixport.open_import
|
|
21
|
+
- Ixport.register_kind
|
|
22
|
+
- Ixport::Import#record_created
|
|
23
|
+
- Ixport::UnknownSource
|
|
19
24
|
- Ixport::Importer
|
|
20
25
|
- field
|
|
21
26
|
- match_on
|
|
@@ -63,6 +68,7 @@ sources:
|
|
|
63
68
|
- app/helpers/ixport/imports_helper.rb
|
|
64
69
|
- app/models/ixport/application_record.rb
|
|
65
70
|
- app/models/ixport/import.rb
|
|
71
|
+
- app/models/ixport/created_record.rb
|
|
66
72
|
- app/controllers/ixport/mappings_controller.rb
|
|
67
73
|
- app/views/ixport/imports/new.html.erb
|
|
68
74
|
- app/views/ixport/mappings/show.html.erb
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ixport
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tyler Schneider
|
|
@@ -100,6 +100,7 @@ files:
|
|
|
100
100
|
- app/helpers/ixport/app_routes_helper.rb
|
|
101
101
|
- app/helpers/ixport/imports_helper.rb
|
|
102
102
|
- app/models/ixport/application_record.rb
|
|
103
|
+
- app/models/ixport/created_record.rb
|
|
103
104
|
- app/models/ixport/csv_writer.rb
|
|
104
105
|
- app/models/ixport/export_template.rb
|
|
105
106
|
- app/models/ixport/import.rb
|
|
@@ -119,8 +120,10 @@ files:
|
|
|
119
120
|
- config/routes.rb
|
|
120
121
|
- lib/generators/ixport/install/install_generator.rb
|
|
121
122
|
- lib/generators/ixport/install/templates/add_error_message_to_ixport_imports.rb.erb
|
|
123
|
+
- lib/generators/ixport/install/templates/add_source_to_ixport_imports.rb.erb
|
|
122
124
|
- lib/generators/ixport/install/templates/add_target_to_ixport_imports.rb.erb
|
|
123
125
|
- lib/generators/ixport/install/templates/add_wallflower_task_to_ixport_imports.rb.erb
|
|
126
|
+
- lib/generators/ixport/install/templates/create_ixport_created_records.rb.erb
|
|
124
127
|
- lib/generators/ixport/install/templates/create_ixport_export_templates.rb.erb
|
|
125
128
|
- lib/generators/ixport/install/templates/create_ixport_import_refusals.rb.erb
|
|
126
129
|
- lib/generators/ixport/install/templates/create_ixport_imports.rb.erb
|