Skip to content

groupings to replace children #64

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 94 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,28 +295,6 @@ the `:project` key is removed from the errors, so: `row_model.errors.keys # => [

`represents_many` is also available, except it returns `[]` when any of the dependencies are `blank?`.

### Children

Child `RowModel` relationships can also be defined:

```ruby
class UserImportRowModel
include CsvRowModel::Model
include CsvRowModel::Import

column :id, type: Integer
column :name
column :email

# uses ProjectImportRowModel#valid? to detect the child row
has_many :projects, ProjectImportRowModel
end

import_file = CsvRowModel::Import::File.new(file_path, UserImportRowModel)
row_model = import_file.next
row_model.projects # => [<ProjectImportRowModel>, ...]
```

## Import Validations

Use [`ActiveModel::Validations`](http://api.rubyonrails.org/classes/ActiveModel/Validations.html) the `RowModel`'s [Layers](#layers).
Expand Down Expand Up @@ -414,7 +392,100 @@ class ImportFile < CsvRowModel::Import::File
end
```

## Dynamic columns
## Groupings
Additional options that can be combined.

### Grouped Columns
Grouped columns have `Array` attribute values and where each element is amongst separate columns.

```ruby
class GroupedColumnModel
include CsvRowModel::Model
column :tags, grouped_columns: 3
end
```

represents this table:

| tag1 | tag2 | tag3 |
| ---- |----- | ---- |
| a | b | c |
| d | e | |


For export, implement the column as an array:

```ruby
class GroupedColumnExportModel < GroupedColumnModel
include CsvRowModel::Export

def tags
['a', 'b', 'c', 'd', 'e']
end
end
```

For import, the attribute is automatically an array:

```ruby
class GroupedColumnImportModel < GroupedColumnModel
include CsvRowModel::Import
end

row_model = CsvRowModel::Import::File.new(file_path, GroupedColumnImportModel).next
row_model.attributes # => { tags: ['a', 'b', 'c', 'd', 'e'] }
row_model.tags # => ['a', 'b', 'c', 'd', 'e']
```

### Group Rows
This option allows grouping of rows into the same attribute. Row is only grouped if cells of other columns are empty.

```ruby
class GroupRowsModel
include CsvRowModel::Model

column :names, group_rows: true
column :standard
column :tags, group_rows: true
end
```

represents this table:

| names | standard | tags |
| ----- |------------ | -----|
| abbey | | a |
| | | b |
| bob | not_empty | c |
| chris | not_empty | d |
| dave | | e |

For export, implement the column as an array:

```ruby
class GroupRowsExportModel < GroupRowsModel
include CsvRowModel::Export

def names; ['abbey']
def standard; end
def tags; ['a', 'b'] end
end
```

For import, the attribute is automatically an array:

```ruby
class GroupRowsImportModel < GroupRowsModel
include CsvRowModel::Import
end

file = CsvRowModel::Import::File.new(file_path, GroupedColumnImportModel)
file.next.attributes # => { names: ['abbey'], standard: '', tags: ['a', b'] }
file.next.attributes # => { names: ['bob'], standard: 'not_empty', tags: ['c'] } # does not group 'chris' because the `standard` cell is not empty
file.next.attributes # => { names: ['chris', 'dave'], standard: 'not_empty', tags: ['d', 'e'] }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really weird case..... I don't see any case like that and our CSVs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

library :p (we're trying to replace children)

```

## Dynamic Columns
Dynamic columns are columns that can expand to many columns. Currently, we can only one dynamic column after all other standard columns.
The following:

Expand Down