Skip to content
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

Feature: Display additional fields when attaching a record to another #3048

Merged

Conversation

binarygit
Copy link
Contributor

@binarygit binarygit commented Jul 24, 2024

Description

Fixes #2472

Documentation PR: avo-hq/docs.avohq.io#255

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works

Screenshots & recording

When attaching fails:

error.mp4

When attaching is successful:

fixed.mp4

Manual review steps

  1. Step 1
  2. Step 2

Manual reviewer: please leave a comment with output from the test if that's the case.

Copy link

codeclimate bot commented Jul 24, 2024

Code Climate has analyzed commit a00b433 and detected 1 issue on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 1

View more on Code Climate.

@binarygit binarygit force-pushed the display-additional-columns-in-join-records branch 5 times, most recently from b19e4c9 to 1525e4f Compare July 24, 2024 10:34
@binarygit binarygit marked this pull request as ready for review July 24, 2024 12:03
@binarygit binarygit requested a review from Paul-Bob July 24, 2024 12:03
@adrianthedev adrianthedev self-requested a review July 25, 2024 06:33
Copy link
Contributor

@Paul-Bob Paul-Bob left a comment

Choose a reason for hiding this comment

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

There are some conflicts (I guess from this PR) and i18n test is failing.

Let's fix those first please

When fixing the conflicts let's keep the turbo stream response and incorporate the t("avo.attachment_failed", attachment_class: @related_resource.name) in the errors list.

@binarygit binarygit force-pushed the display-additional-columns-in-join-records branch 2 times, most recently from 3ce5e49 to a06f297 Compare July 26, 2024 08:35
@binarygit binarygit requested a review from Paul-Bob July 26, 2024 15:00
@binarygit
Copy link
Contributor Author

@Paul-Bob I can fix the i18n tests before we merge this. It's complaining about missing keys in other i18n files. Currently I will need to go into every one of them and put the key in and we might change the words or something so it might get repetitive.

Could you please review this and I'll make sure I add those keys in before we merge.

@binarygit binarygit force-pushed the display-additional-columns-in-join-records branch from a06f297 to 32644f8 Compare July 30, 2024 10:52
Copy link
Contributor

@Paul-Bob Paul-Bob left a comment

Choose a reason for hiding this comment

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

.

@binarygit binarygit force-pushed the display-additional-columns-in-join-records branch from 32644f8 to 670ba0a Compare July 30, 2024 12:58
@Paul-Bob Paul-Bob self-requested a review July 30, 2024 13:19
Copy link
Contributor

@Paul-Bob Paul-Bob left a comment

Choose a reason for hiding this comment

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

It's looking good! Nice progress @binarygit!

Let's try to use the fields to parse the values that come through params.

app/controllers/avo/associations_controller.rb Outdated Show resolved Hide resolved
@binarygit binarygit requested a review from Paul-Bob July 31, 2024 09:12
Copy link
Contributor

@Paul-Bob Paul-Bob left a comment

Choose a reason for hiding this comment

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

Hey @binarygit we're almost there!

I left some comments, let's prioritize the fill_record method duplication.

lib/avo/fields/has_base_field.rb Outdated Show resolved Hide resolved
app/controllers/avo/associations_controller.rb Outdated Show resolved Hide resolved
lib/avo/resources/base.rb Show resolved Hide resolved
lib/avo/resources/base.rb Outdated Show resolved Hide resolved
def new_join_record
field_names = @attach_fields.map { |field| field.name.tr(" ", "_").downcase }

@resource.fill_record(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@adrianthedev We use fill_record which permits and assigns only those values whose key match the fields declared by the user in their resources file. I think this mitigates the chances of such attacks, no?

def fill_record(record, params, extra_params: [], fields: nil)

Copy link
Contributor

@Paul-Bob Paul-Bob Aug 5, 2024

Choose a reason for hiding this comment

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

I think this mitigates the chances of such attacks, no?

Yes, but let's apply the permit directly inside the additional_params method to increase readability.

def additional_params
  params[:fields].permit(@attach_fields.map(&:id))
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okayy. Got it 😄

@binarygit
Copy link
Contributor Author

I pushed a commit that turns the texts to:
image

Could you take a look @adrianthedev 😄

@Paul-Bob
Copy link
Contributor

Paul-Bob commented Aug 5, 2024

@binarygit we're attaching a patron. The field should be named Patron (singular) and not Patrons (plural). Let's verify how we compute the title for the "Attach patron" text and apply the same strategy

Copy link
Contributor

@Paul-Bob Paul-Bob left a comment

Choose a reason for hiding this comment

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

  • Some tweaks to improve readability
  • Permit params through fields' id instead of fields' name.

Comment on lines 220 to 224
field_names = @attach_fields.map { |field| field.name.tr(" ", "_").downcase }

@resource.fill_record(
@reflection.through_reflection.klass.new,
additional_params.permit(field_names).merge(
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
field_names = @attach_fields.map { |field| field.name.tr(" ", "_").downcase }
@resource.fill_record(
@reflection.through_reflection.klass.new,
additional_params.permit(field_names).merge(
@resource.fill_record(
@reflection.through_reflection.klass.new,
additional_params.merge(

Comment on lines 206 to 208
def additional_params
params[:fields].except("related_id")
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def additional_params
params[:fields].except("related_id")
end
def additional_params
params[:fields].permit(@attach_fields.map(&:id))
end

Comment on lines 202 to 204
def additional_params?
additional_params.keys.count >= 1
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def additional_params?
additional_params.keys.count >= 1
end

Use additional_params.any? instead of dedicated method

Copy link
Contributor

@Paul-Bob Paul-Bob left a comment

Choose a reason for hiding this comment

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

🎆

end

def additional_params
params[:fields].permit(@attach_fields.map(&:id))
Copy link
Collaborator

Choose a reason for hiding this comment

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

One more thing.
Let's see if this permit statement emits a red warning in the console for other params which are sent by the modal.

I'd like to prevent that behavior. Some users when they have a problem with that actions, they will look at that and think that that is the cause of the problem.
So they are led astray.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that behavior depends on the parent's app configuration and can be configured by action_on_unpermitted_parameters (check whole docs here)

Would be great to have a flag to turn it off like: params.permit(on_unnpermited_params: false, ...)

@Paul-Bob Paul-Bob merged commit e5ae21d into avo-hq:main Aug 6, 2024
20 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fields for additional column values/associations while attaching a record to another
3 participants