luckyframework / luckyframework/avram

Make it easier to set default associations in a Factory

Open
#385 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Crystal
Stars
183
Forks
67
PR merge metrics
No merged PRs in 30d

Description

Depends on #331

```crystal
class ArticleBox < Box
def initialize
post { PostBox.create }
end

# This is auto-generated by the box. Putting it here just to show rough implementation
def post
before_save do
if post_id.value.blank?
record_or_id = yield
id = if record_or_id.is_a?(Avram::Model)
record_or_id.id
else
record_or_id
end
post_id id
end
end
end
end
```

This will most likely require changes to https://github.com/luckyframework/avram/blob/master/src/avram/save_operation.cr so that we have access to an `ASSOCIATIONS` constant that has a list of associations and the column to use.

Relevant examples:
* https://github.com/luckyframework/avram/blob/bb576790a832b41db10ccf8087fa9a43934a9b6f/src/avram/save_operation_template.cr#L43
* https://github.com/luckyframework/avram/blob/d67dd63936e2276f603819076f1c391ea4076cab/src/avram/save_operation.cr#L80-L82

We'd want to add a `add_association_attributes` or something. This will be a bit more complex so I'll try to tackle it, but if someone else wants to take a stab at it, post here and I'll try to help if you run into anything

Contributor guide

Open the contributing guide

Research direction

Start with dependency #331, then read src/avram/save_operation.cr and the referenced section of src/avram/save_operation_template.cr. Trace how associations and columns are represented, and confirm that generated Factory behavior can set a blank association from either a record or an id as described.

Written by the indexing model from the issue text.

Assessment

Tech stack
crystal
Domain
databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.