-
Notifications
You must be signed in to change notification settings - Fork 1
Add support for XLSX with data entry enhancements #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexdunae
wants to merge
26
commits into
culturecode:master
Choose a base branch
from
alexdunae:data-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
86c96d3
Switch to require_relative
alexdunae c0fb717
Use IOString instead of a tempfile when generating XLSX
alexdunae 001a54f
Add as_json from ActiveSupport
alexdunae c87fc87
Ignore *.xlsx
alexdunae b790bc5
Spike of data validation logic
alexdunae 241aa44
Improve after integrating in the app
alexdunae 8524059
Add docs
alexdunae 1850288
Re-organize for clarity
alexdunae f75aa12
Add activesupport import
alexdunae 0a07e35
Always use absolute references for data source lists
alexdunae 00d7bc8
Update test suite
alexdunae 879618d
Refactor to handle reusable validation data sources and named ranges
alexdunae 5c3663f
Add logic for dependent pick lists
alexdunae bd2545a
Add HeaderCell to simplify humanizing names
alexdunae 2832fb1
Docs
alexdunae 383d4f6
Add freeze_panes option
alexdunae 2cd0fe2
Add GeneratesSpreadsheet model concern
alexdunae 2c5a327
Prevent having to write per-row dependent validations 🥳
alexdunae 661d5a6
Tighten
alexdunae fc5d433
Add macros to source control
alexdunae 1f31546
Use kwargs
alexdunae eb5285c
Docs
alexdunae a32e6c4
Docs
alexdunae fcff9f9
Move test script
alexdunae f1fc22a
Add docs to the macro
alexdunae e5e9a39
Remove dead code
alexdunae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -7,3 +7,4 @@ test/dummy/log/*.log | |
| test/dummy/tmp/ | ||
| test/dummy/.sass-cache | ||
| *.gem | ||
| *.xlsx | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
| module SpreadsheetExporter | ||
| ColumnValidation = Struct.new(:ignore_blank, :data_source, :dependent_on, :error_type, keyword_init: true) do | ||
| def initialize(*) | ||
| super | ||
| self.ignore_blank = true if ignore_blank.nil? | ||
| self.error_type ||= VALIDATION_ERROR_TYPES[0] | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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,11 @@ | ||
| require "active_support/concern" | ||
|
|
||
| module SpreadsheetExporter | ||
| module GeneratesSpreadsheet | ||
| extend ActiveSupport::Concern | ||
|
|
||
| def as_spreadsheet(options = {}) | ||
| serializable_hash(options) | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,24 +1,43 @@ | ||
| # TODO: Find out why we can't detect arrays properly and must resort to crappy class.name comparison | ||
| module SpreadsheetExporter | ||
| HeaderCell = Struct.new(:attribute_name, :human_attribute_name) do | ||
| def to_s | ||
| human_attribute_name.presence || attribute_name | ||
| end | ||
| end | ||
|
|
||
| module Spreadsheet | ||
| def self.from_objects(objects, options = {}) | ||
| def self.from_objects(objects, humanize_headers_class: nil, **options) | ||
| headers = [] | ||
| rows = [] | ||
|
|
||
| # Get all the data and accumulate headers from each row (since rows may not have all the same attributes) | ||
| Array(objects).each do |object| | ||
| data = object.respond_to?(:as_csv) ? get_values(object.as_csv(options)) : get_values(object.as_json(options)) | ||
| headers = headers | data.keys | ||
| data = if object.respond_to?(:as_spreadsheet) | ||
| get_values(object.as_spreadsheet(options)) | ||
| elsif object.respond_to?(:as_csv) | ||
| get_values(object.as_csv(options)) | ||
| else | ||
| get_values(object.as_json(options)) | ||
| end | ||
|
|
||
| headers |= data.keys.map { |v| HeaderCell.new(v) } | ||
| rows << data | ||
| end | ||
|
|
||
| # Create the csv, ensuring to place each row's attributes under the appropriate header (since rows may not have all the same attributes) | ||
| [].tap do |spreadsheet| | ||
| spreadsheet << (options[:humanize_headers_class] ? han(options[:humanize_headers_class], headers) : headers) | ||
| if humanize_headers_class | ||
| headers = han(headers, humanize_headers_class: humanize_headers_class, **options) | ||
| end | ||
|
|
||
| spreadsheet << headers | ||
|
|
||
| rows.each do |row| | ||
| sorted_row = [] | ||
| row.each do |header, value| | ||
| sorted_row[headers.index(header)] = value | ||
| col_index = headers.find_index { |h| h.attribute_name == header } | ||
| sorted_row[col_index] = value | ||
| end | ||
|
|
||
| spreadsheet << sorted_row | ||
|
|
@@ -28,14 +47,14 @@ def self.from_objects(objects, options = {}) | |
|
|
||
| # Return an array of human_attribute_name's | ||
| # Used by the CSV Import/Export process to match CSV headers to model attribute names | ||
| def self.han(klass, *attributes) | ||
| options = attributes.extract_options! | ||
| def self.han(headers, humanize_headers_class:, downcase: false, **) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type for this use to be scalar or array but now is always an array. This change didn't break any other code that I could see |
||
| headers.flatten! | ||
|
|
||
| attributes.flatten! | ||
| attributes.collect! {|attribute| klass.human_attribute_name(attribute) } | ||
| attributes.collect!(&:downcase) if options[:downcase] | ||
|
|
||
| return attributes.many? ? attributes : attributes.first | ||
| headers.collect! do |header| | ||
| header.human_attribute_name = humanize_headers_class.human_attribute_name(header.attribute_name) | ||
| header.human_attribute_name.downcase! if downcase | ||
| header | ||
| end | ||
| end | ||
|
|
||
| def self.get_values(node, current_header = nil) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is my main question about code style. It's nice to have these structs for safety, etc... but they're kind of ugly. Would this be better as
?