Skip to content

Commit

Permalink
Do not pass down the project.project_custom_field_project_mappings to…
Browse files Browse the repository at this point in the history
… the CustomFieldRowComponent but call it from the project object.

This is safe to do, because we are calling the project_custom_field_project_mappings
on the same project object multiple times and this won't result in an N+1 query.
  • Loading branch information
dombesz committed Mar 22, 2024
1 parent 9b3b5e1 commit d05351b
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ class CustomFieldRowComponent < ApplicationComponent
include OpPrimer::ComponentHelpers
include OpTurbo::Streamable

def initialize(project:, project_custom_field:, project_custom_field_project_mappings:)
def initialize(project:, project_custom_field:)
super

@project = project
@project_custom_field = project_custom_field
@project_custom_field_project_mappings = project_custom_field_project_mappings
@project_custom_field_project_mappings = project.project_custom_field_project_mappings
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
flex.with_row do
render(Projects::Settings::ProjectCustomFieldSections::ShowComponent.new(
project: @project,
project_custom_field_section:,
project_custom_fields: project_custom_field_section.custom_fields,
project_custom_field_project_mappings: @project_custom_field_project_mappings
project_custom_field_section:
))
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,11 @@ class IndexComponent < ApplicationComponent
include OpPrimer::ComponentHelpers
include OpTurbo::Streamable

def initialize(
project:,
project_custom_field_sections:,
project_custom_field_project_mappings:
)
def initialize(project:, project_custom_field_sections:)
super

@project = project
@project_custom_field_sections = project_custom_field_sections
@project_custom_field_project_mappings = project_custom_field_project_mappings
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
render(Projects::Settings::ProjectCustomFieldSections::CustomFieldRowComponent.new(
project: @project,
project_custom_field:,
project_custom_field_project_mappings: @project_custom_field_project_mappings,
))
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,12 @@ class ShowComponent < ApplicationComponent
include OpPrimer::ComponentHelpers
include OpTurbo::Streamable

def initialize(
project:,
project_custom_field_section:,
project_custom_fields:,
project_custom_field_project_mappings:
)
def initialize(project:, project_custom_field_section:)
super

@project = project
@project_custom_field_section = project_custom_field_section
@project_custom_fields = project_custom_fields
@project_custom_field_project_mappings = project_custom_field_project_mappings
@project_custom_fields = project_custom_field_section.custom_fields
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def update_header_via_turbo_stream
def update_section_via_turbo_stream(project_custom_field_section:)
update_via_turbo_stream(
component: ::Settings::ProjectCustomFieldSections::ShowComponent.new(
# Note: `first_and_last:` argument is necessary here, because we render
# a single custom field section, and not a list of sections. Calling first?
# and last? method in the component will not result in an N+1 in this case.
project_custom_field_section:
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ module ComponentStreams
included do
def update_sections_via_turbo_stream(
project: @project,
project_custom_field_sections: @project_custom_field_sections,
project_custom_field_project_mappings: @project_custom_field_project_mappings
project_custom_field_sections: @project_custom_field_sections
)
update_via_turbo_stream(
component: ::Projects::Settings::ProjectCustomFieldSections::IndexComponent.new(
project:,
project_custom_field_sections:,
project_custom_field_project_mappings:
project_custom_field_sections:
)
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ def eager_load_project_custom_field_data
.merge(ProjectCustomField.visible)
.group(:id, "custom_fields.id")
.order(:position, :position_in_custom_field_section)

@project_custom_field_project_mappings = ProjectCustomFieldProjectMapping
.where(project_id: @project.id)
.to_a
end

def set_project_custom_field_section
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ See COPYRIGHT and LICENSE files for more details.
<%= render(Projects::Settings::ProjectCustomFieldSections::IndexComponent.new(
project: @project,
project_custom_field_sections: @project_custom_field_sections,
project_custom_field_project_mappings: @project_custom_field_project_mappings,
)) %>
</div>

0 comments on commit d05351b

Please sign in to comment.