Skip to content

Commit

Permalink
Move request action info to a component
Browse files Browse the repository at this point in the history
In more than one place in the application, we need to know the source
and target packages related to the request action, if any.

So we extract the code from NotificationActionDescriptionComponent
and create an independent component for the source and target
information.
  • Loading branch information
saraycp committed Apr 27, 2022
1 parent 882a4d7 commit cf71829
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class BsRequestActionSourceAndTargetComponent < ApplicationComponent
attr_reader :bs_request_action, :number_of_bs_request_actions

def initialize(bs_request)
super

@bs_request = bs_request
@bs_request_action = @bs_request.bs_request_actions.first
@number_of_bs_request_actions = @bs_request.bs_request_actions.size
end

def call
capture do
if source.present?
concat(tag.span(source))
concat(tag.i(nil, class: 'fas fa-long-arrow-alt-right text-info mx-2'))
end
concat(tag.span(target))
end
end

private

def source
@source ||= if number_of_bs_request_actions > 1
''
else
[bs_request_action.source_project, bs_request_action.source_package].compact.join(' / ')
end
end

def target
return bs_request_action.target_project if number_of_bs_request_actions > 1

[bs_request_action.target_project, bs_request_action.target_package].compact.join(' / ')
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def call
tag.div(class: ['smart-overflow']) do
case @notification.event_type
when 'Event::RequestStatechange', 'Event::RequestCreate', 'Event::ReviewWanted', 'Event::CommentForRequest'
source_and_target
BsRequestActionSourceAndTargetComponent.new(bs_request).call
when 'Event::CommentForProject'
"#{@notification.notifiable.commentable.name}"
when 'Event::CommentForPackage'
Expand All @@ -21,39 +21,7 @@ def call

private

def source_and_target
capture do
if source.present?
concat(tag.span(source))
concat(tag.i(nil, class: 'fas fa-long-arrow-alt-right text-info mx-2'))
end
concat(tag.span(target))
end
end

def source
@source ||= if number_of_bs_request_actions > 1
''
else
[bs_request_action.source_project, bs_request_action.source_package].compact.join(' / ')
end
end

def target
return bs_request_action.target_project if number_of_bs_request_actions > 1

[bs_request_action.target_project, bs_request_action.target_package].compact.join(' / ')
end

def bs_request
@bs_request ||= @notification.notifiable_type == 'BsRequest' ? @notification.notifiable : @notification.notifiable.commentable
end

def bs_request_action
@bs_request_action ||= bs_request.bs_request_actions.first
end

def number_of_bs_request_actions
@number_of_bs_request_actions ||= bs_request.bs_request_actions.size
end
end

0 comments on commit cf71829

Please sign in to comment.