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

Use Strategy in all components

Klaus Zanders2c9a97554440
diff --git a/app/components/shares/bulk_permission_button_component.rb b/app/components/shares/bulk_permission_button_component.rb
index f9a89e18673..b42c5de1dad 100644
--- a/app/components/shares/bulk_permission_button_component.rb
+++ b/app/components/shares/bulk_permission_button_component.rb
@@ -30,11 +30,11 @@
module Shares
class BulkPermissionButtonComponent < ApplicationComponent # rubocop:disable OpenProject/AddPreviewForViewComponent
- def initialize(entity:, available_roles:)
+ def initialize(strategy:)
super
- @entity = entity
- @available_roles = available_roles
+ @entity = strategy.entity
+ @available_roles = strategy.available_roles
end
def update_path
diff --git a/app/components/shares/counter_component.html.erb b/app/components/shares/counter_component.html.erb
index e9281848a58..2e58b8d310d 100644
--- a/app/components/shares/counter_component.html.erb
+++ b/app/components/shares/counter_component.html.erb
@@ -4,7 +4,7 @@
# There's no point in rendering the BulkSelectionCounterComponent even if
# I'm able to manage shares if the only user that the work package is
# currently shared is myself, since I'm not able to manage my own share.
- if sharing_manageable? && shared_with_anyone_else_other_than_myself?
+ if strategy.manageable? && shared_with_anyone_else_other_than_myself?
render(Shares::BulkSelectionCounterComponent.new(count:))
else
render(Shares::ShareCounterComponent.new(count:))
diff --git a/app/components/shares/counter_component.rb b/app/components/shares/counter_component.rb
index 145ee3b8c53..c2581bfb5db 100644
--- a/app/components/shares/counter_component.rb
+++ b/app/components/shares/counter_component.rb
@@ -34,26 +34,22 @@ module Shares
include OpTurbo::Streamable
include OpPrimer::ComponentHelpers
- def initialize(entity:,
- count:,
- sharing_manageable:)
+ def initialize(strategy:, count:)
super
- @entity = entity
+ @strategy = strategy
+ @entity = strategy.entity
@count = count
- @sharing_manageable = sharing_manageable
end
private
- attr_reader :entity, :count
-
- def sharing_manageable? = @sharing_manageable
+ attr_reader :entity, :count, :strategy
def shared_with_anyone_else_other_than_myself?
Member.of_entity(@entity)
.where.not(principal: User.current)
- .any?
+ .exists?
end
end
end
diff --git a/app/components/shares/modal_body_component.html.erb b/app/components/shares/modal_body_component.html.erb
index 05cde3aef1d..789054fb45f 100644
--- a/app/components/shares/modal_body_component.html.erb
+++ b/app/components/shares/modal_body_component.html.erb
@@ -2,7 +2,7 @@
component_wrapper(tag: 'turbo-frame') do
flex_layout(data: { turbo: true }) do |modal_content|
modal_content.with_row do
- render(Shares::InviteUserFormComponent.new(strategy: strategy, errors: errors))
+ render(Shares::InviteUserFormComponent.new(strategy:, errors: errors))
end
modal_content.with_row(mt: 3,
@@ -13,9 +13,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(entity: @entity,
- count: @shares.size,
- sharing_manageable: @sharing_manageable))
+ render(Shares::CounterComponent.new(strategy:, count: @shares.size))
end
header_grid.with_area(:actions,
@@ -57,7 +55,7 @@
button.with_trailing_action_icon(icon: "triangle-down")
I18n.t('sharing.filter.role')
end
- @available_roles.each do |role_hash|
+ strategy.available_roles.each do |role_hash|
menu.with_item(label: role_hash[:label],
href: filter_url(role_option: role_hash),
method: :get,
@@ -74,13 +72,11 @@
tag: :div,
hidden: true, # Prevent flicker on initial render
data: { 'shares--bulk-selection-target': 'bulkActions' }) do
- if @sharing_manageable
- concat(
- render(Shares::BulkPermissionButtonComponent.new(entity: @entity, available_roles: @available_roles))
- )
+ if strategy.manageable?
+ concat(render(Shares::BulkPermissionButtonComponent.new(strategy:)))
concat(
- form_with(url: url_for([:bulk, @entity, Member]),
+ form_with(url: url_for([:bulk, entity, Member]),
method: :delete,
data: { 'shares--bulk-selection-target': 'bulkForm' }) do
render(Primer::Beta::IconButton.new(icon: "trash",
@@ -95,7 +91,7 @@
end
end
- if @shares.blank?
+ if @shares.none?
border_box.with_row do
render(Primer::Beta::Blankslate.new) do |component|
component.with_visual_icon(icon: blankslate_config[:icon], size: :medium)
@@ -111,10 +107,7 @@
end
else
@shares.each do |share|
- render(Shares::ShareRowComponent.new(share: share,
- available_roles: @available_roles,
- sharing_manageable: @sharing_manageable,
- container: border_box))
+ render(Shares::ShareRowComponent.new(share:, strategy:, container: border_box))
end
end
end
diff --git a/app/components/shares/modal_body_component.rb b/app/components/shares/modal_body_component.rb
index 82755950de6..de3a761b140 100644
--- a/app/components/shares/modal_body_component.rb
+++ b/app/components/shares/modal_body_component.rb
@@ -36,8 +36,6 @@ module Shares
attr_reader :strategy,
:entity,
:shares,
- :available_roles,
- :sharing_manageable,
:errors
def initialize(strategy:, shares:, errors: nil)
@@ -46,8 +44,6 @@ module Shares
@strategy = strategy
@entity = strategy.entity
@shares = shares
- @available_roles = strategy.available_roles
- @sharing_manageable = strategy.manageable?
@errors = errors
end
@@ -104,15 +100,15 @@ module Shares
end
end
- def type_filter_option_active?(option)
+ def type_filter_option_active?(option) # rubocop:disable Metrics/AbcSize
principal_type_filter_value = current_filter_value(params[:filters], "principal_type")
project_member_filter_value = current_filter_value(params[:filters], "also_project_member")
- return false if principal_type_filter_value.nil? || project_member_filter_value.nil?
+ return false if principal_type_filter_value.nil? || (project_scoped_entity? && project_member_filter_value.nil?)
principal_type_checked =
option[:value][:principal_type] == principal_type_filter_value
- membership_selected =
+ membership_selected = !project_scoped_entity? ||
option[:value][:project_member] == ActiveRecord::Type::Boolean.new.cast(project_member_filter_value)
principal_type_checked && membership_selected
@@ -123,7 +119,7 @@ module Shares
return false if role_filter_value.nil?
- selected_role = @available_roles.find { _1[:value] == option[:value] }
+ selected_role = strategy.available_roles.find { _1[:value] == option[:value] }
selected_role[:value] == role_filter_value.to_i
end
@@ -181,14 +177,18 @@ module Shares
end
def type_filter_for(option)
- filter = []
- if ActiveRecord::Type::Boolean.new.cast(option[:value][:project_member])
- filter.push({ also_project_member: { operator: "=", values: [OpenProject::Database::DB_VALUE_TRUE] } })
- else
- filter.push({ also_project_member: { operator: "=", values: [OpenProject::Database::DB_VALUE_FALSE] } })
+ filter = [
+ { principal_type: { operator: "=", values: [option[:value][:principal_type]] } }
+ ]
+
+ if project_scoped_entity?
+ if ActiveRecord::Type::Boolean.new.cast(option[:value][:project_member])
+ filter.push({ also_project_member: { operator: "=", values: [OpenProject::Database::DB_VALUE_TRUE] } })
+ else
+ filter.push({ also_project_member: { operator: "=", values: [OpenProject::Database::DB_VALUE_FALSE] } })
+ end
end
- filter.push({ principal_type: { operator: "=", values: [option[:value][:principal_type]] } })
filter
end
diff --git a/app/components/shares/share_row_component.rb b/app/components/shares/share_row_component.rb
index 8f9a1dd6e53..b047f143529 100644
--- a/app/components/shares/share_row_component.rb
+++ b/app/components/shares/share_row_component.rb
@@ -34,17 +34,14 @@ module Shares
include OpTurbo::Streamable
include OpPrimer::ComponentHelpers
- def initialize(share:,
- available_roles:,
- sharing_manageable:,
- container: nil)
+ def initialize(share:, strategy:, container: nil)
super
+ @strategy = strategy
@share = share
- @entity = share.entity
+ @entity = strategy.entity
@principal = share.principal
- @available_roles = available_roles
- @sharing_manageable = sharing_manageable
+ @available_roles = strategy.available_roles
@container = container
end
@@ -54,13 +51,15 @@ module Shares
private
- attr_reader :share, :entity, :principal, :container, :available_roles
+ attr_reader :share, :entity, :principal, :container, :available_roles, :strategy
def share_editable?
@share_editable ||= User.current != share.principal && sharing_manageable?
end
- def sharing_manageable? = @sharing_manageable
+ def sharing_manageable?
+ strategy.manageable?
+ end
def grid_css_classes
if sharing_manageable?
diff --git a/app/models/members/scopes/of_entity.rb b/app/models/members/scopes/of_entity.rb
index b9178a06045..b8413a4d7bd 100644
--- a/app/models/members/scopes/of_entity.rb
+++ b/app/models/members/scopes/of_entity.rb
@@ -33,8 +33,11 @@ module Members::Scopes
class_methods do
# Find all members of a specific Work Package
def of_entity(entity)
- of_any_entity
- .where(entity:)
+ if entity.respond_to?(:project)
+ where(project: entity.project, entity:)
+ else
+ where(project: nil, entity:)
+ end
end
end
end