-
Notifications
You must be signed in to change notification settings - Fork 194
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
Refactor ContentBlockEdition create to accepted nested document attrs #9271
Refactor ContentBlockEdition create to accepted nested document attrs #9271
Conversation
lib/engines/content_object_store/app/models/content_object_store/content_block_edition.rb
Outdated
Show resolved
Hide resolved
125b375
to
a0d25cf
Compare
lib/engines/content_object_store/test/unit/app/models/content_block_edition_test.rb
Show resolved
Hide resolved
Looks good - I totally forgot about |
Following a discussion[1] we want to clarify what we expect this concern to contain. We want this to be more than identification, we want it to be everything that connects an edition to a document. This does diverge us from the main Whitehall we were referencing which may make this harder to merge back together later. But for now the short term benefit of the team knowing how to use it clearly takes priority. [1] #9271 (review)
a0d25cf
to
d45cf66
Compare
Okay folks after looking at that cucumber test I had to work on customising the error messages. That's now done with help from @pezholio ❤️ Whilst I look at this rebase it's ready for another review please. I'm conscious the longer this type of change stays in progress the many more conflicts we'll get. cc @Harriethw |
d45cf66
to
ad57e4f
Compare
Following a discussion[1] we want to clarify what we expect this concern to contain. We want this to be more than identification, we want it to be everything that connects an edition to a document. This does diverge us from the main Whitehall we were referencing which may make this harder to merge back together later. But for now the short term benefit of the team knowing how to use it clearly takes priority. [1] #9271 (review)
d17d3f1
to
acb4d12
Compare
We’ll use this next with custom error messages.
Following a discussion[1] we want to clarify what we expect this concern to contain. We want this to be more than identification, we want it to be everything that connects an edition to a document. This does diverge us from the main Whitehall we were referencing which may make this harder to merge back together later. But for now the short term benefit of the team knowing how to use it clearly takes priority. [1] #9271 (review)
d02b545
to
974078c
Compare
It was becomign cumbersome to remember which attributes were for the document and which were for the edition when it came to creating and updating. Instead of having to track this we can use the Rails `accepts_nested_attributes_for` to help us take care of this. The main Whitehall repository takes this approach[1]. With it we’re able to send around a single blob of params, pass them to the Edition ORM and it’ll figure out what to do, including validations. * We have to make one override to the title error message so we see “Title can’t be blank” rather than “Content document title can’t be blank”. * We also adjust the hidden field for block tag so it puts the params in the same place. Inside `content_block_edition`, rather than `content_block_document_attributes ` at the top level. We’ll now have `content_block_edition: { details: {}, content_block_documentattributes: {}}`. * In renaming these attributes we also fix the form so the error messages correctly link to the title field on error [1] https://github.com/alphagov/whitehall/blob/7cbd2b9b6d97df9741befcc4700fb573fb2e1960/app/models/edition.rb#L82]
974078c
to
cc4a997
Compare
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.
think it makes sense to me...thanks for these improvements!
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.
LGTM 👍
@@ -1,3 +1,5 @@ | |||
en: | |||
activerecord: | |||
attributes: | |||
content_object_store/content_block_edition/content_block_document: |
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.
would you be able to ELI5 how this is used? Is there magic going on in the view somewhere?
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.
Rails magic! Which is why it was so hard to find the right combination.
What I had to go on was this piece of the docs, if you scroll down slightly you get to this:
In the event you need to access nested attributes within a given model, you should nest these under model/attribute at the model level of your translation file:
en:
activerecord:
attributes:
user/role:
admin: "Admin"
contributor: "Contributor"
Which gave me the clue there was some magic /
syntax to make it work for nested objects.
The trouble I had was how content_object_store
came into the picture, i.e why not
content_object_store/content_block_edition/content_object_store/content_block_document:
or
content_object_store/content_block_edition/content_block_document_attributes:
Changes in this PR
It was becomign cumbersome to remember which attributes were for the document and which were for the edition when it came to creating and updating.
Instead of having to track this we can use the Rails
accepts_nested_attributes_for
to help us take care of this. The main Whitehall repository takes this approach[1].With it we’re able to send around a single blob of params, pass them to the Edition ORM and it’ll figure out what to do, including validations.
content_block_edition
, rather thancontent_block_document_attributes
at the top level. We’ll now havecontent_block_edition: { details: {}, content_block_document_attributes: {}}
. This feels more conventional, what do we think?creation.mov