Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Move loading of shares to the strategy

Klaus Zanders4bd51fe2667d
diff --git a/app/components/shares/modal_body_component.html.erb b/app/components/shares/modal_body_component.html.erb
index aaa239c859b..fbf12195a64 100644
--- a/app/components/shares/modal_body_component.html.erb
+++ b/app/components/shares/modal_body_component.html.erb
@@ -21,7 +21,7 @@
border_box.with_header(color: :muted, data: { 'test-selector': 'op-share-dialog-header' }) do
grid_layout('op-share-dialog-modal-body--header', tag: :div, align_items: :center) do |header_grid|
header_grid.with_area(:counter, tag: :div) do
- render(Shares::CounterComponent.new(strategy:, count: @shares.size))
+ render(Shares::CounterComponent.new(strategy:, count: strategy.shares.size))
end
header_grid.with_area(:actions,
@@ -99,7 +99,7 @@
end
end
- if @shares.none?
+ if strategy.shares.none?
border_box.with_row do
if strategy.custom_empty_state_component?
render(strategy.empty_state_component.new(strategy: strategy))
@@ -108,7 +108,7 @@
end
end
else
- @shares.each do |share|
+ strategy.shares.each do |share|
render(Shares::ShareRowComponent.new(share:, strategy:, container: border_box))
end
end
diff --git a/app/components/shares/modal_body_component.rb b/app/components/shares/modal_body_component.rb
index a07102c374d..6e69008d4ef 100644
--- a/app/components/shares/modal_body_component.rb
+++ b/app/components/shares/modal_body_component.rb
@@ -35,15 +35,13 @@ module Shares
attr_reader :strategy,
:entity,
- :shares,
:errors
- def initialize(strategy:, shares:, errors: nil)
+ def initialize(strategy:, errors: nil)
super
@strategy = strategy
@entity = strategy.entity
- @shares = shares
@errors = errors
end
diff --git a/app/components/shares/share_dialog_component.html.erb b/app/components/shares/share_dialog_component.html.erb
index df84ba60efd..3bfaad1719a 100644
--- a/app/components/shares/share_dialog_component.html.erb
+++ b/app/components/shares/share_dialog_component.html.erb
@@ -2,7 +2,7 @@
render(Primer::Alpha::Dialog.new(title: t(:label_share_project_list), id: 'sharing-modal', data: { 'keep-open-on-submit': true }, size: :xlarge)) do |d|
d.with_header(variant: :large, mb: 3)
d.with_body do
- render(Shares::ModalBodyComponent.new(strategy:, shares:, errors:))
+ render(Shares::ModalBodyComponent.new(strategy:, errors:))
end
end
%>
diff --git a/app/components/shares/share_dialog_component.rb b/app/components/shares/share_dialog_component.rb
index 702f5adc556..63ad3ba6f08 100644
--- a/app/components/shares/share_dialog_component.rb
+++ b/app/components/shares/share_dialog_component.rb
@@ -34,16 +34,15 @@ module Shares
include OpTurbo::Streamable
include OpPrimer::ComponentHelpers
- def initialize(shares:, strategy:, errors:)
+ def initialize(strategy:, errors:)
super
- @shares = shares
@strategy = strategy
@errors = errors
end
private
- attr_reader :shares, :strategy, :errors
+ attr_reader :strategy, :errors
end
end
diff --git a/app/controllers/shares_controller.rb b/app/controllers/shares_controller.rb
index da10380ae58..5305f4c12df 100644
--- a/app/controllers/shares_controller.rb
+++ b/app/controllers/shares_controller.rb
@@ -32,7 +32,6 @@ class SharesController < ApplicationController
include MemberHelper
before_action :load_entity
- before_action :load_shares, only: %i[index dialog]
before_action :load_selected_shares, only: %i[bulk_update bulk_destroy]
before_action :load_share, only: %i[destroy update resend_invite]
before_action :enterprise_check, only: %i[index]
@@ -44,11 +43,11 @@ class SharesController < ApplicationController
def dialog; end
def index
- unless @query.valid?
- flash.now[:error] = query.errors.full_messages
+ unless sharing_strategy.query.valid?
+ flash.now[:error] = sharing_strategy.query.errors.full_messages
end
- render Shares::ModalBodyComponent.new(strategy: sharing_strategy, shares: @shares, errors: @errors), layout: nil
+ render Shares::ModalBodyComponent.new(strategy: sharing_strategy, errors: @errors), layout: nil
end
def create # rubocop:disable Metrics/AbcSize,Metrics/PerceivedComplexity
@@ -83,11 +82,11 @@ class SharesController < ApplicationController
def update
create_or_update_share(@share.principal.id, params[:role_ids])
- load_shares
+ shares = sharing_strategy.shares(reload: true)
- if @shares.empty?
+ if shares.empty?
respond_with_replace_modal
- elsif @shares.include?(@share)
+ elsif shares.include?(@share)
respond_with_update_permission_button
else
respond_with_remove_share
@@ -104,6 +103,7 @@ class SharesController < ApplicationController
end
end
+ # TODO: This is still work package specific
def resend_invite
OpenProject::Notifications.send(OpenProject::Events::WORK_PACKAGE_SHARED,
work_package_member: @share,
@@ -165,10 +165,11 @@ class SharesController < ApplicationController
end
def respond_with_replace_modal
+ sharing_strategy.shares(reload: true)
+
replace_via_turbo_stream(
component: Shares::ModalBodyComponent.new(
strategy: sharing_strategy,
- shares: @new_shares || load_shares,
errors: @errors
)
)
@@ -199,7 +200,6 @@ class SharesController < ApplicationController
),
target_component: Shares::ModalBodyComponent.new(
strategy: sharing_strategy,
- shares: load_shares,
errors: @errors
)
)
@@ -297,10 +297,10 @@ class SharesController < ApplicationController
def load_entity # rubocop:disable Metrics/AbcSize
if params["work_package_id"]
@entity = WorkPackage.visible.find(params["work_package_id"])
- @sharing_strategy = SharingStrategies::WorkPackageStrategy.new(@entity, user: current_user)
+ @sharing_strategy = SharingStrategies::WorkPackageStrategy.new(@entity, user: current_user, query_params:)
elsif params["project_query_id"]
@entity = ProjectQuery.visible.find(params["project_query_id"])
- @sharing_strategy = SharingStrategies::ProjectQueryStrategy.new(@entity, user: current_user)
+ @sharing_strategy = SharingStrategies::ProjectQueryStrategy.new(@entity, user: current_user, query_params:)
else
raise ArgumentError, <<~ERROR
Nested the SharesController under an entity controller that is not yet configured to support sharing.
@@ -311,10 +311,6 @@ class SharesController < ApplicationController
Request Path: #{request.path}
ERROR
end
-
- if @entity.respond_to?(:project)
- @project = @entity.project
- end
end
def load_share
@@ -322,15 +318,7 @@ class SharesController < ApplicationController
end
def current_visible_member_count
- @current_visible_member_count ||= load_shares.size
- end
-
- def load_query
- @query = sharing_strategy.shares_query(params)
- end
-
- def load_shares
- @shares = load_query.results
+ @current_visible_member_count ||= sharing_strategy.shares.size
end
def load_selected_shares
@@ -338,4 +326,10 @@ class SharesController < ApplicationController
.of_entity(@entity)
.where(id: params[:share_ids])
end
+
+ def query_params
+ params
+ .slice(:filters, :sortBy, :groupBy)
+ .permit! # ParamsToQueryService will parse the data, so we can permit everything here
+ end
end
diff --git a/app/models/sharing_strategies/base_strategy.rb b/app/models/sharing_strategies/base_strategy.rb
index ce2b186799b..445ebd18a08 100644
--- a/app/models/sharing_strategies/base_strategy.rb
+++ b/app/models/sharing_strategies/base_strategy.rb
@@ -30,9 +30,10 @@ module SharingStrategies
class BaseStrategy
attr_reader :entity, :user
- def initialize(entity, user: User.current)
+ def initialize(entity, query_params:, user: User.current)
@entity = entity
@user = user
+ @query_params = query_params
end
def available_roles
@@ -80,12 +81,12 @@ module SharingStrategies
nil
end
- def shares_query(params) # rubocop:disable Metrics/AbcSize
+ def query # rubocop:disable Metrics/AbcSize
return @query if defined?(@query)
@query = ParamsToQueryService
.new(Member, user, query_class: Queries::Members::NonInheritedMemberQuery)
- .call(params)
+ .call(@query_params)
# Set default filter on the entity
@query.where("entity_id", "=", entity.id)
@@ -94,9 +95,14 @@ module SharingStrategies
@query.where("project_id", "=", entity.project.id)
end
- @query.order(name: :asc) unless params[:sortBy]
+ @query.order(name: :asc) unless @query_params[:sortBy]
@query
end
+
+ def shares(reload: false)
+ @shares = nil if reload
+ @shares ||= query.results
+ end
end
end
diff --git a/app/views/shares/dialog.turbo_stream.erb b/app/views/shares/dialog.turbo_stream.erb
index 56f9e3b1a3a..ec19e379e13 100644
--- a/app/views/shares/dialog.turbo_stream.erb
+++ b/app/views/shares/dialog.turbo_stream.erb
@@ -1,3 +1,3 @@
<%= turbo_stream.dialog do
- render(Shares::ShareDialogComponent.new(strategy: @sharing_strategy, shares: @shares, errors: @errors))
+ render(Shares::ShareDialogComponent.new(strategy: @sharing_strategy, errors: @errors))
end %>