Skip to content

Batch tutorial: create placements directly - #332

Merged
lmrodriguezr merged 2 commits into
mainfrom
direct-batch-placements
Sep 4, 2026
Merged

lmrodriguezr merged 2 commits into
mainfrom
direct-batch-placements

Conversation

@vangberg

Copy link
Copy Markdown
Contributor

I think the batch tutorial predates the Placement class. Currently it creates placements implicitly by setting Name#parent and Name#incertae_sedis, which then creates placements via Name#ensure_consistent_placement which is invoked as an after_save hook.

In preparation for #173 I have refactored the batch tutorial to create placements directly and added a bunch of tests verifying current behavior.

Comment thread test/fixtures/names.yml
rank: species
status: 20 # ICNP

nanobdellaceae:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I feel some tension between using fixtures with names that describe the fixture (e.g. valid_name or unpublished_name) and then just using the name like here. Not sure what direction to go in.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think mixing them up is ok, depending on the aim of the test(s) using the fixture

Comment thread app/models/tutorial/batch.rb Outdated
name_attributes = i.except('parent', 'incertae_sedis')
placement_attributes = {
parent: i['parent'], incertae_sedis: i['incertae_sedis'],
incertae_sedis_text: i['description'], preferred: true

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should we only carry over description if incertae_sedis is true? I think so.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's right, the guard you added later is indeed needed

Comment thread app/models/tutorial/batch.rb Outdated
}

Name.new(name_attributes).tap do |name|
name.placements.build(placement_attributes.merge(name: name))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is it necessary to merge in name when using name.placements.build?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is gone, right? I guess it's inherited directly from i

Comment thread app/models/tutorial/batch.rb Outdated
def save_batch_placement(name, attributes)
# Reuse matching alternatives and demote the current placement before
# promotion so the name never attempts to save two preferred placements.
placement = name.placements.find_or_initialize_by(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is now the 3rd place in the code where we are doing something to set preferred placement. I have an idea for an alternative:

Add Name#preferred_placement_id and Name.belongs_to :preferred_placement, …. That way we enforce that there can only be one preferred placement. Thoughts, @lmrodriguezr ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll try and see what this looks like in another branch.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, that's certainly a better pattern. Migration might be tricky, but we should be able to do it with a simple rake task if it gets too complex for a change block.

@vangberg
vangberg marked this pull request as draft September 3, 2026 09:28
@vangberg
vangberg force-pushed the direct-batch-placements branch 4 times, most recently from 09057f0 to 2304b7b Compare September 3, 2026 11:57
@vangberg
vangberg force-pushed the direct-batch-placements branch from 2304b7b to f967e96 Compare September 3, 2026 12:12
Comment thread app/models/tutorial/batch.rb Dismissed
@vangberg
vangberg force-pushed the direct-batch-placements branch from f967e96 to 7281549 Compare September 3, 2026 12:28
Comment thread app/models/name.rb
def ensure_consistent_placement
if parent_id.present? || incertae_sedis.present?
placement_incertae_sedis =
if saved_change_to_incertae_sedis?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This will go away in #337

@vangberg
vangberg force-pushed the direct-batch-placements branch from 7281549 to 145ea82 Compare September 3, 2026 12:39
@vangberg
vangberg marked this pull request as ready for review September 3, 2026 12:39
@vangberg

vangberg commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

@lmrodriguezr I think this is finally ready for review.

This is somewhat unrelated to the pull request, but it allowed me to do some local testing

@lmrodriguezr lmrodriguezr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This was long overdue in my to-do list, thank you!!

@lmrodriguezr
lmrodriguezr merged commit 73badf0 into main Sep 4, 2026
4 checks passed
@lmrodriguezr
lmrodriguezr deleted the direct-batch-placements branch September 4, 2026 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants