Skip to content

Commit

Permalink
[fix] scope requests controller to organization (#1999)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwr18 authored Sep 4, 2024
1 parent 00b33b4 commit 5f9899e
Show file tree
Hide file tree
Showing 5 changed files with 570 additions and 121 deletions.
12 changes: 6 additions & 6 deletions app/components/metric/metric.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="<%= class_attr %>">
<div <%= attrs %>>
<% if total.present? && styles.include?(:large) %>
<div
class="Metric-progress"
Expand All @@ -7,11 +7,11 @@
<% end %>

<span class="Metric-icon">
<% if custom_icon %>
<%= svg custom_icon %>
<% else %>
<%= c 'icon', icon: icon, style: :inline %>
<% end %>
<% if custom_icon %>
<%= svg custom_icon %>
<% else %>
<%= c 'icon', icon: icon, style: :inline %>
<% end %>
</span>

<span class="Metric-value">
Expand Down
54 changes: 31 additions & 23 deletions app/controllers/requests_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

# TODO: Refactor to remove the need to disable rubocop
# rubocop:disable Metrics/ClassLength
class RequestsController < ApplicationController
before_action :set_request, only: %i[show edit update notifications destroy messages_by_contributor stats]
before_action :notifications_params, only: :notifications
Expand All @@ -9,8 +11,8 @@ class RequestsController < ApplicationController

def index
@filter = filter_param
@sent_requests_count = Request.broadcasted.count
@planned_requests_count = Request.planned.count
@sent_requests_count = @organization.requests.broadcasted.count
@planned_requests_count = @organization.requests.planned.count
@requests = filtered_requests.page(params[:page])
end

Expand All @@ -36,24 +38,18 @@ def create
end

def new
@request = Request.new(organization: @organization)
@request = @organization.requests.new
end

def edit; end

def update
@request.files.purge_later if @request.files.attached? && request_params[:files].blank?
if @request.update(request_params)
if @request.planned?
redirect_to organization_requests_path(@request.organization_id, filter: :planned), flash: {
success: I18n.t('request.schedule_request_success',
count: Contributor.active.with_tags(@request.tag_list).count,
scheduled_datetime: I18n.l(@request.schedule_send_for, format: :long))
}
else
redirect_to organization_request_path(@request.organization_id, @request),
flash: { success: I18n.t('request.success', count: @request.stats[:counts][:recipients]) }
end
success_message, filter = update_request_redirect_specifics
redirect_to organization_requests_path(@request.organization_id, filter: filter), flash: {
success: success_message
}
else
render :edit, status: :unprocessable_entity
end
Expand Down Expand Up @@ -89,30 +85,29 @@ def stats
value: stats[:counts][:contributors],
total: stats[:counts][:recipients],
label: I18n.t('components.request_metrics.contributors', count: stats[:counts][:contributors]),
icon: 'single-03'
icon: 'single-03',
data: { testid: 'unique-contributors-replied-ratio' }
},
{
value: stats[:counts][:replies],
label: I18n.t('components.request_metrics.replies', count: stats[:counts][:replies]),
icon: 'a-chat'
icon: 'a-chat',
data: { testid: 'total-replies-count' }
},
{
value: stats[:counts][:photos],
label: I18n.t('components.request_metrics.photos', count: stats[:counts][:photos]),
icon: 'camera'
icon: 'camera',
data: { testid: 'photos-count' }
}
]
render(InlineMetrics::InlineMetrics.new(metrics: metrics), content_type: 'text/html')
end

private

def set_contributor
@contributor = Contributor.find(params[:contributor_id])
end

def set_request
@request = Request.find(params[:id])
@request = @organization.requests.find(params[:id])
end

def available_tags
Expand Down Expand Up @@ -161,9 +156,22 @@ def filter_param

def filtered_requests
if @filter == :planned
Request.planned.reorder(schedule_send_for: :desc).includes(:tags)
@organization.requests.planned.reorder(schedule_send_for: :desc).includes(:tags)
else
@organization.requests.broadcasted.includes(:tags)
end
end

def update_request_redirect_specifics
if @request.planned?
[I18n.t('request.schedule_request_success',
count: @request.organization.contributors.active.with_tags(@request.tag_list).count,
scheduled_datetime: I18n.l(@request.schedule_send_for, format: :long)),
:planned]
else
Request.broadcasted.includes(:tags)
[I18n.t('request.success', count: @request.organization.contributors.active.with_tags(@request.tag_list).count),
nil]
end
end
end
# rubocop:enable Metrics/ClassLength
Loading

0 comments on commit 5f9899e

Please sign in to comment.