Skip to content

Commit

Permalink
Pass 'current_user' around as a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ncounter committed Feb 23, 2024
1 parent 018b898 commit 36d0b34
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/api/app/components/notification_component.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
.row
.col
= notification_icon
= render NotificationNotifiableLinkComponent.new(@notification)
= render NotificationNotifiableLinkComponent.new(@notification, @current_user)
%small.text-nowrap
= render TimeComponent.new(time: @notification.created_at)
- if @notification.notifiable_type == 'BsRequest'
Expand Down
3 changes: 2 additions & 1 deletion src/api/app/components/notification_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ class NotificationComponent < ApplicationComponent
'WorkflowRun' => 'Workflow run'
}.freeze

def initialize(notification:, selected_filter:, page:, show_more:)
def initialize(notification:, selected_filter:, page:, show_more:, current_user:)
super

@notification = notification
@selected_filter = selected_filter
@page = page
@show_more = show_more
@current_user = current_user

Check warning on line 23 in src/api/app/components/notification_component.rb

View check run for this annotation

Codecov / codecov/patch

src/api/app/components/notification_component.rb#L23

Added line #L23 was not covered by tests
end

def notification_icon
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# rubocop:disable Metrics/ClassLength
class NotificationNotifiableLinkComponent < ApplicationComponent
def initialize(notification)
def initialize(notification, current_user)
super

@notification = notification
@current_user = current_user
end

def call
Expand Down Expand Up @@ -123,7 +124,7 @@ def notifiable_link_path
when 'Event::ReportForProject', 'Event::ReportForPackage'
@notification.event_type.constantize.notification_link_path(@notification)
when 'Event::ReportForUser'
Rails.application.routes.url_helpers.user_path(@notification.event_payload['user_login'], notification_id: @notification.id) if !@notification.event_user.is_deleted? || helpers.current_user_is_admin
Rails.application.routes.url_helpers.user_path(@notification.event_payload['user_login'], notification_id: @notification.id) if !@notification.event_user.is_deleted? || @current_user.is_admin?

Check warning on line 127 in src/api/app/components/notification_notifiable_link_component.rb

View check run for this annotation

Codecov / codecov/patch

src/api/app/components/notification_notifiable_link_component.rb#L127

Added line #L127 was not covered by tests
when 'Event::ReportForRequest'
bs_request = @notification.notifiable.reportable
Rails.application.routes.url_helpers.request_show_path(bs_request.number, notification_id: @notification.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def index
@show_read_all_button = show_read_all_button?
@filtered_project = Project.find_by(name: params[:project])
@selected_filter = selected_filter
@current_user = User.session
end

def update
Expand Down
4 changes: 0 additions & 4 deletions src/api/app/helpers/webui/user_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ def filter_message(params)
"#{result}."
end

def current_user_is_admin
User.admin_session?
end

private

def project_package_message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
.list-group.list-group-flush.mt-3
= render NotificationComponent.with_collection(notifications, selected_filter: selected_filter,
page: params[:page],
show_more: params[:show_more])
show_more: params[:show_more],
current_user: current_user)
= paginate notifications, views_prefix: 'webui', window: 2, params: { action: 'index', id: nil }

- content_for :ready_function do
Expand Down
3 changes: 2 additions & 1 deletion src/api/app/views/webui/users/notifications/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
.col-md-8.col-lg-9.px-0.px-md-3#notifications-list
= render partial: 'notifications_list', locals: { notifications: @notifications,
selected_filter: @selected_filter,
show_read_all_button: @show_read_all_button }
show_read_all_button: @show_read_all_button,
current_user: @current_user }
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
RSpec.describe NotificationNotifiableLinkComponent, type: :component do
let(:current_user) { create(:user) }

context 'for a BsRequest notification with multiple actions' do
let(:bs_request) { create(:bs_request_with_submit_action, number: 456_345) }
let(:notification) { create(:notification, :request_state_change, notifiable: bs_request) }
Expand All @@ -7,7 +9,7 @@
# Extra BsRequestAction
bs_request.bs_request_actions << create(:bs_request_action_add_maintainer_role)

render_inline(described_class.new(notification))
render_inline(described_class.new(notification, current_user))
end

it 'renders a link to the BsRequest with a generic text and its number' do
Expand All @@ -20,7 +22,7 @@
let(:notification) { create(:notification, :request_state_change, notifiable: bs_request) }

before do
render_inline(described_class.new(notification))
render_inline(described_class.new(notification, current_user))
end

it 'renders a link to the BsRequest with the text containing its action and number' do
Expand All @@ -33,7 +35,7 @@
let(:notification) { create(:notification, :request_created, notifiable: bs_request) }

before do
render_inline(described_class.new(notification))
render_inline(described_class.new(notification, current_user))
end

it 'renders a link to the BsRequest with the text containing its action and number' do
Expand All @@ -46,7 +48,7 @@
let(:notification) { create(:notification, :review_wanted, notifiable: bs_request) }

before do
render_inline(described_class.new(notification))
render_inline(described_class.new(notification, current_user))
end

it 'renders a link to the BsRequest with the text containing its action and number' do
Expand All @@ -60,7 +62,7 @@
let(:notification) { create(:notification, :comment_for_request, notifiable: comment) }

before do
render_inline(described_class.new(notification))
render_inline(described_class.new(notification, current_user))
end

it "renders a link to the comment's BsRequest with the text containing its action and number" do
Expand All @@ -74,7 +76,7 @@
let(:notification) { create(:notification, :comment_for_project, notifiable: comment) }

before do
render_inline(described_class.new(notification))
render_inline(described_class.new(notification, current_user))
end

it "renders a link to the comment's project" do
Expand All @@ -89,7 +91,7 @@
let(:notification) { create(:notification, :comment_for_package, notifiable: comment) }

before do
render_inline(described_class.new(notification))
render_inline(described_class.new(notification, current_user))
end

it "renders a link to the comment's package" do
Expand All @@ -103,7 +105,7 @@
let(:package) { notification.notifiable.reportable.commentable }

before do
render_inline(described_class.new(notification))
render_inline(described_class.new(notification, current_user))
end

it 'renders a link to the reported content' do
Expand All @@ -116,7 +118,7 @@
let(:package) { notification.notifiable.reports.first.reportable.commentable }

before do
render_inline(described_class.new(notification))
render_inline(described_class.new(notification, current_user))
end

it 'renders a link to the reportable' do
Expand All @@ -129,7 +131,7 @@
let(:package) { notification.notifiable.reports.first.reportable.commentable }

before do
render_inline(described_class.new(notification))
render_inline(described_class.new(notification, current_user))
end

it 'renders a link to the reportable' do
Expand Down

0 comments on commit 36d0b34

Please sign in to comment.