Skip to content

Automatically provide a language in diary entries factory#7009

Merged
tomhughes merged 1 commit intoopenstreetmap:masterfrom
pablobm:diary-entry-factory-lang
Apr 24, 2026
Merged

Automatically provide a language in diary entries factory#7009
tomhughes merged 1 commit intoopenstreetmap:masterfrom
pablobm:diary-entry-factory-lang

Conversation

@pablobm
Copy link
Copy Markdown
Contributor

@pablobm pablobm commented Apr 16, 2026

When working on something the other day, I noticed that the default setting for the diary entries factory (create(:diary_entry)) requires that a Language record for the English language exists in DB.

Should this be simplified? I am providing an option that is perhaps slightly hacky, but I think it works well for the use it gets in this codebase.

@pablobm pablobm force-pushed the diary-entry-factory-lang branch from 5ad31c2 to 48ea403 Compare April 16, 2026 14:55
sequence(:title) { |n| "Diary entry #{n}" }
sequence(:body) { |n| "This is diary entry #{n}" }

language { Language.find_by(:code => "en") || create(:language, :code => "en") }
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we use find_or_create_by here? It does side-step factory_bot entirely, but is perhaps easier to read.

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.

Hm, losing the factory feels a bit wrong. How about the following?

Suggested change
language { Language.find_by(:code => "en") || create(:language, :code => "en") }
language do
Language.find_or_create_by(:code => "en") do |record|
record.assign_attributes(attributes_for(:language))
end
end

Significantly more verbose though...

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.

Personally, and after spending some time hunting through the factorybot documentation assuming that there must be a better way, it seems that @pablobm's original suggestion is probably best.

To be honest I'm struggling to understand how this has been working... Does factorybot really just associate to a random record if you don't tell it to do something else?

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.

It confused me too when I was looking into it. It's not FactoryBot, it's our SQL schema, which defaults to en:

    language_code character varying DEFAULT 'en'::character varying NOT NULL,

@tomhughes
Copy link
Copy Markdown
Member

Possibly the tests in diary_entries_controller_test.rb should be included in this?

@pablobm
Copy link
Copy Markdown
Contributor Author

pablobm commented Apr 24, 2026

Possibly the tests in diary_entries_controller_test.rb should be included in this?

I left that one out because it explicitly sets languages in some tests, so I felt like it made sense to allow the language being created separately. Eg: check out test_index_language or test_rss.

@tomhughes
Copy link
Copy Markdown
Member

Yes I was aware of that but at least in some cases in might make sense but it can probably argued either way.

I think this is fine as far as it goes anyway, so there's no reason not to merge it now.

@tomhughes tomhughes merged commit d80f26c into openstreetmap:master Apr 24, 2026
17 of 18 checks passed
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.

4 participants