-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP 2: Better more complete feature MVP
UI polish and testing next.
- Loading branch information
Showing
13 changed files
with
208 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require "csv" | ||
|
||
class DocumentDataDictionary < ApplicationRecord | ||
include ActiveModel::Validations | ||
|
||
# Callbacks (keep at top) | ||
after_save :parse_csv_file | ||
|
||
# Associations | ||
has_one_attached :csv_file | ||
belongs_to :document, foreign_key: :friendlier_id, primary_key: :friendlier_id | ||
has_many :document_data_dictionary_entries, dependent: :destroy | ||
|
||
# Validations | ||
validates :name, presence: true | ||
validates :csv_file, attached: true, content_type: {in: "text/csv", message: "is not a CSV file"} | ||
|
||
validates_with DocumentDataDictionary::CsvHeaderValidator | ||
|
||
def parse_csv_file | ||
if csv_file.attached? | ||
csv_data = CSV.parse(csv_file.download, headers: true) | ||
csv_data.each do |row| | ||
self.document_data_dictionary_entries.create!(row.to_h) | ||
end | ||
end | ||
end | ||
end | ||
|
27 changes: 27 additions & 0 deletions
27
app/models/document_data_dictionary/csv_header_validator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require "csv" | ||
|
||
# CSV Header Validation | ||
class DocumentDataDictionary | ||
# CsvHeaderValidator | ||
class CsvHeaderValidator < ActiveModel::Validator | ||
def validate(record) | ||
valid_csv_header = true | ||
unless valid_csv_headers?(record&.csv_file) | ||
valid_csv_header = false | ||
record.errors.add(:csv_file, | ||
"Missing the required CSV header. friendlier_id, field_name, field_type, values, definition, definition_source, and parent_field_name are required.") | ||
end | ||
|
||
valid_csv_header | ||
end | ||
|
||
def valid_csv_headers?(csv_file) | ||
headers = CSV.parse(csv_file.download)[0] | ||
(["friendlier_id", "field_name", "field_type", "values", "definition", "definition_source", "parent_field_name"] - headers).empty? | ||
rescue ArgumentError, ActiveStorage::FileNotFoundError | ||
false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class DocumentDataDictionaryEntry < ApplicationRecord | ||
# Associations | ||
belongs_to :document_data_dictionary | ||
|
||
# Validations | ||
validates :friendlier_id, :field_name, :field_type, :values, presence: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,80 @@ | ||
<% content_for :title, "New document data dictionary" %> | ||
<div class="row mb-2"> | ||
<div class="col"> | ||
<%= link_to "Back to document data dictionaries", admin_document_document_data_dictionaries_path(@document) %> | ||
|
||
<h1>New document data dictionary</h1> | ||
<h1 style="width:100%;"> | ||
<%= @document.title %> · New Data Dictionary | ||
</h1> | ||
</div> | ||
</div> | ||
|
||
<%= render "form", document_data_dictionary: @document_data_dictionary %> | ||
<div class="row"> | ||
<div class="col-5"> | ||
<%= render "form", document_data_dictionary: @document_data_dictionary %> | ||
|
||
<br> | ||
<%= link_to "Back to document data dictionaries", admin_document_document_data_dictionaries_path(@document) %> | ||
</div> | ||
|
||
<div> | ||
<%= link_to "Back to document data dictionaries", admin_document_document_data_dictionaries_path(@document) %> | ||
</div> | ||
<div class="col-6"> | ||
<h4>Example Data Dictionary CSV File</h4> | ||
<table class="table table-bordered"> | ||
<thead> | ||
<tr> | ||
<th>friendlier_id*</th> | ||
<th>field_name*</th> | ||
<th>field_type*</th> | ||
<th>values*</th> | ||
<th>definition</th> | ||
<th>definition_source</th> | ||
<th>parent_field_name</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>999-0010a</td> | ||
<td>Institution</td> | ||
<td>string</td> | ||
<td>University of Minnesota</td> | ||
<td>The name of the institution</td> | ||
<td>https://www.umn.edu</td> | ||
<td>999-0010UMN</td> | ||
</tr> | ||
<tr> | ||
<td>999-0010a</td> | ||
<td>Institution</td> | ||
<td>string</td> | ||
<td>University of Minnesota Duluth</td> | ||
<td>The name of the institution</td> | ||
<td>https://www.d.umn.edu</td> | ||
<td>999-0010UMN</td> | ||
</tr> | ||
<tr> | ||
<td>...</td> | ||
<td>...</td> | ||
<td>...</td> | ||
<td>...</td> | ||
<td>...</td> | ||
<td>...</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
|
||
<h5>CSV File Column Definitions</h5> | ||
<dl> | ||
<dt>friendlier_id</dt> | ||
<dd>The associated document's friendlier id</dd> | ||
<dt>field_name</dt> | ||
<dd>The name of the field</dd> | ||
<dt>field_type</dt> | ||
<dd>The type of the field</dd> | ||
<dt>values</dt> | ||
<dd>The values of the field</dd> | ||
<dt>definition</dt> | ||
<dd>The definition of the field</dd> | ||
<dt>definition_source</dt> | ||
<dd>The source of the definition</dd> | ||
<dt>parent_field_name</dt> | ||
<dd>The parent field's name</dd> | ||
</dl> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
db/migrate/20241218174455_create_document_data_dictionary_entries.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class CreateDocumentDataDictionaryEntries < ActiveRecord::Migration[7.2] | ||
def change | ||
create_table :document_data_dictionary_entries do |t| | ||
t.references :document_data_dictionary, null: false, foreign_key: true | ||
t.string :friendlier_id | ||
t.string :field_name | ||
t.string :field_type | ||
t.string :values | ||
t.string :definition | ||
t.string :definition_source | ||
t.string :parent_field_name | ||
t.integer :position | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
friendlier_id,field_name,field_type,values,definition,definition_source,parent_field_name | ||
test,AREA,Real,6,Area of polygon,Software generated, | ||
test,PERIMETER,Real,6,Perimeter of polygon,Software generated, | ||
test,NAME_U,String,10,County names in upper case letters,USGS 7.5-minute quadrangles, | ||
test,NAME_L,String,10,County names in upper and lower case letters,IGS standard, | ||
test,NCAPC,Integer,2,Numeric county alphabetic prefix code,Indiana Bureau of Motor Vehicles, |