Skip to content

Commit

Permalink
feat(fworks): basic editing of provider contact
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantk committed Sep 12, 2023
1 parent add26d3 commit 66c968c
Show file tree
Hide file tree
Showing 21 changed files with 261 additions and 63 deletions.
3 changes: 3 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,6 @@ Style/DateTime:
- 'lib/microsoft_graph/transformer/attachment.rb'
- 'lib/microsoft_graph/transformer/message.rb'
- 'lib/microsoft_graph/transformer/update_message.rb'

Rails/UniqueValidationWithoutIndex:
Enabled: false
7 changes: 6 additions & 1 deletion app/controllers/frameworks/frameworks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ def show
private

def filter_form_params
params.fetch(:frameworks_filter, {}).permit(:sort_by, :sort_order, status: [], provider: [], e_and_o_lead: [], proc_ops_lead: [], category: [])
params.fetch(:frameworks_filter, {}).permit(
:sort_by, :sort_order,
status: [], provider: [],
e_and_o_lead: [], proc_ops_lead: [],
category: [], provider_contact: []
)
end

def set_back_url
Expand Down
27 changes: 27 additions & 0 deletions app/controllers/frameworks/provider_contacts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
class Frameworks::ProviderContactsController < Frameworks::ApplicationController
before_action :redirect_to_register_tab, unless: :turbo_frame_request?, only: :index
before_action :set_back_url, only: %i[show edit]

def index
@filtering = Frameworks::ProviderContact.filtering(filter_form_params)
@provider_contacts = @filtering.results.paginate(page: params[:provider_contacts_page])
end

def show
@provider_contact = Frameworks::ProviderContact.find(params[:id])
end

def edit
@provider_contact = Frameworks::ProviderContact.find(params[:id])
end

def update
@provider_contact = Frameworks::ProviderContact.find(params[:id])

if @provider_contact.update(provider_contact_params)
redirect_to frameworks_provider_contact_path(@provider_contact)
else
render :edit
end
end

private

def set_back_url
@back_url = back_link_param
end

def filter_form_params
params.fetch(:provider_contacts_filter, {}).permit(:sort_by, :sort_order, provider: [])
end

def provider_contact_params
params.require(:frameworks_provider_contact).permit(:name, :email, :phone)
end

def redirect_to_register_tab
redirect_to frameworks_root_path(anchor: "provider-contacts", **request.params.except(:controller, :action))
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
module Frameworks::ActivityLoggable::ActivityLogPresentable
extend ActiveSupport::Concern

def activity_log_association_display_name(association:, id:)
self.class.reflections[association].klass.find_by(id:).try(:activity_log_display_name)
end

def activity_log_display_type
self.class.to_s.demodulize.underscore.humanize
included do
delegate :version_at, to: :paper_trail
end

def activity_log_display_name
try(:full_name) || try(:short_name) || try(:name)
end

def activity_log_display(field)
return send("activity_log_#{field}") if respond_to?("activity_log_#{field}")
return send("display_#{field}") if respond_to?("display_#{field}")
def activity_log_display_type
self.class.to_s.demodulize.underscore.humanize
end
end
7 changes: 5 additions & 2 deletions app/models/frameworks/activity_loggable_version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class Frameworks::ActivityLoggableVersion < PaperTrail::Version
def presentable_chages
PresentableChanges.new(self)
include Presentable

def item_association_at_version_or_current(association:, id:, version_at: created_at)
current_version_of_association = item.class.reflections[association].klass.find_by(id:)
current_version_of_association.try(:version_at, version_at).presence || current_version_of_association
end
end
23 changes: 23 additions & 0 deletions app/models/frameworks/activity_loggable_version/presentable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Frameworks::ActivityLoggableVersion::Presentable
extend ActiveSupport::Concern

def presentable_changes
Frameworks::ActivityLoggableVersion::PresentableChanges.new(self)
end

def display_field_version(field:, value:)
display_value =
if field.ends_with?("_id")
display_field_version_for_association(association: field.split("_id").first, id: value)
else
item.try("display_field_version_#{field}", value)
end

display_value.presence || value
end

def display_field_version_for_association(association:, id:)
record = item_association_at_version_or_current(association:, id:)
record.try(:activity_log_display_name).presence || record.try(:full_name).presence || record.try(:name)
end
end
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
class Frameworks::ActivityLoggableVersion::PresentableChanges
PresentableChange = Struct.new(:field, :from, :to, keyword_init: true)

include Enumerable

def initialize(version)
@version = version
end

def each(&block)
if block_given?
non_common_object_changes.each(&block)
else
to_enum(:each)
end
end

private
def each
return to_enum(:each) unless block_given?

def non_common_object_changes
@version.object_changes
.except("id", "created_at", "updated_at")
.map { |field, changes| change_type(field).new(@version, field, changes) }
end
@version.object_changes.except("id", "created_at", "updated_at").each do |field, changes|
from = @version.display_field_version(field:, value: changes.first)
to = @version.display_field_version(field:, value: changes.last)

def change_type(field)
field.ends_with?("_id") ? BelongsToAssociationChange : FieldChange
yield PresentableChange.new(field:, from:, to:)
end
end
end

This file was deleted.

1 change: 1 addition & 0 deletions app/models/frameworks/framework.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Frameworks::Framework < ApplicationRecord
include SpreadsheetImportable
include Sourceable
include Presentable
include ActivityLogPresentable
include Filterable
include Sortable

Expand Down
11 changes: 11 additions & 0 deletions app/models/frameworks/framework/activity_log_presentable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Frameworks::Framework::ActivityLogPresentable
extend ActiveSupport::Concern

def display_field_version_dfe_start_date(date)
Date.parse(date).strftime("%d/%m/%y") if date.present?
end

def display_field_version_dfe_end_date(date)
display_field_version_dfe_start_date(date)
end
end
1 change: 1 addition & 0 deletions app/models/frameworks/framework/filterable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Frameworks::Framework::Filterable
scope :by_category, ->(category_ids) { where(support_category_id: Array(category_ids)) }
scope :by_e_and_o_lead, ->(e_and_o_lead_ids) { where(e_and_o_lead_id: Array(e_and_o_lead_ids)) }
scope :by_proc_ops_lead, ->(proc_ops_lead_ids) { where(proc_ops_lead_id: Array(proc_ops_lead_ids)) }
scope :by_provider_contact, ->(provider_contact_ids) { where(provider_contact_id: Array(provider_contact_ids)) }
end

class_methods do
Expand Down
6 changes: 6 additions & 0 deletions app/models/frameworks/framework/filtering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Frameworks::Framework::Filtering
attribute :e_and_o_lead, default: -> { [] }
attribute :proc_ops_lead, default: -> { [] }
attribute :category, default: -> { [] }
attribute :provider_contact, default: -> { [] }
attribute :sort_by
attribute :sort_order

Expand Down Expand Up @@ -43,6 +44,10 @@ def available_status_filter_options
Frameworks::Framework.statuses.map { |label, _id| [label.humanize, label] }
end

def available_provider_contact_options
Frameworks::ProviderContact.sort_by_name("ascending").pluck(:name, :id)
end

def available_sort_options
Frameworks::Framework.available_sort_options
end
Expand All @@ -60,6 +65,7 @@ def filters
Support::Concerns::ScopeFilter.new(e_and_o_lead, scope: :by_e_and_o_lead),
Support::Concerns::ScopeFilter.new(proc_ops_lead, scope: :by_proc_ops_lead),
Support::Concerns::ScopeFilter.new(category, scope: :by_category),
Support::Concerns::ScopeFilter.new(provider_contact, scope: :by_provider_contact),
]
end
end
3 changes: 3 additions & 0 deletions app/models/frameworks/provider_contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ class Frameworks::ProviderContact < ApplicationRecord

belongs_to :provider
has_many :frameworks

validates :name, presence: true
validates :email, presence: true, uniqueness: true
end
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<%= activity.event.humanize %>d <%= activity.item.activity_log_display_type %> <%= activity.item.activity_log_display_name %>
<%= activity.event.humanize %>d <%= activity.item.activity_log_display_type %>
<%= link_to "[+]", "#", title: "See changes", class: "govuk-link", "data-component" => "toggle-panel-visibility", "data-panel" => activity_log_item.id %>

<%= link_to "[+]", "#", title: "See changes", class: "govuk-link", "data-component" => "toggle-panel-visibility", "data-panel" => activity.id %>

<div class="govuk-!-display-none govuk-!-margin-top-5" id="<%= activity.id %>">
<div class="govuk-!-display-none govuk-!-margin-top-5" id="<%= activity_log_item.id %>">
<table class="govuk-table">
<tbody class="govuk-table__body">
<% activity.presentable_chages.each do |changes| %>
<% activity.presentable_changes.each do |changes| %>
<tr class="govuk-table__row">
<th scope="row" class="govuk-table__header govuk-!-width-one-quarter"><%= changes.field_name %></th>
<td class="govuk-table__cell"><%= changes.display %></td>
<th scope="row" class="govuk-table__header govuk-!-width-one-quarter"><%= changes.field.humanize %></th>
<td class="govuk-table__cell">
<%= changes.from.presence || "empty" %>
<span class="activity-log-change-arrow"></span>
<%= changes.to.presence || "empty" %>
</td>
</tr>
<% end %>
</tbody>
Expand Down
16 changes: 12 additions & 4 deletions app/views/frameworks/frameworks/_index_filters.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,44 @@
<% end %>
<% end %>

<%= expander title: "Category", subtitle: "#{@filtering.number_of_selected(:categories)} selected", expanded: false do %>
<%= expander title: "Category", subtitle: "#{@filtering.number_of_selected(:categories)} selected", expanded: @filtering.number_of_selected(:categories).positive? do %>
<%= form.govuk_check_boxes_fieldset :category, legend: nil, small: true, form_group: { class: "govuk-!-margin-bottom-0" } do %>
<% @filtering.available_category_options.each do |option| %>
<%= form.govuk_check_box :category, option.last, exclusive: false, label: { text: option.first } %>
<% end %>
<% end %>
<% end %>

<%= expander title: "Provider", subtitle: "#{@filtering.number_of_selected(:providers)} selected", expanded: false do %>
<%= expander title: "Provider", subtitle: "#{@filtering.number_of_selected(:providers)} selected", expanded: @filtering.number_of_selected(:providers).positive? do %>
<%= form.govuk_check_boxes_fieldset :provider, legend: nil, small: true, form_group: { class: "govuk-!-margin-bottom-0" } do %>
<% @filtering.available_provider_filter_options.each do |option| %>
<%= form.govuk_check_box :provider, option.last, exclusive: false, label: { text: option.first } %>
<% end %>
<% end %>
<% end %>

<%= expander title: "Proc-Ops Lead", subtitle: "#{@filtering.number_of_selected(:proc_ops_leads)} selected", expanded: false do %>
<%= expander title: "Proc-Ops Lead", subtitle: "#{@filtering.number_of_selected(:proc_ops_leads)} selected", expanded: @filtering.number_of_selected(:proc_ops_leads).positive? do %>
<%= form.govuk_check_boxes_fieldset :proc_ops_lead, legend: nil, small: true, form_group: { class: "govuk-!-margin-bottom-0" } do %>
<% @filtering.available_proc_ops_lead_options.each do |option| %>
<%= form.govuk_check_box :proc_ops_lead, option.last, exclusive: false, label: { text: option.first } %>
<% end %>
<% end %>
<% end %>

<%= expander title: "E&O Lead", subtitle: "#{@filtering.number_of_selected(:e_and_o_leads)} selected", expanded: false do %>
<%= expander title: "E&O Lead", subtitle: "#{@filtering.number_of_selected(:e_and_o_leads)} selected", expanded: @filtering.number_of_selected(:e_and_o_leads).positive? do %>
<%= form.govuk_check_boxes_fieldset :e_and_o_lead, legend: nil, small: true, form_group: { class: "govuk-!-margin-bottom-0" } do %>
<% @filtering.available_e_and_o_lead_options.each do |option| %>
<%= form.govuk_check_box :e_and_o_lead, option.last, exclusive: false, label: { text: option.first } %>
<% end %>
<% end %>
<% end %>

<%= expander title: "Provider Contact", subtitle: "#{@filtering.number_of_selected(:provider_contacts)} selected", expanded: @filtering.number_of_selected(:provider_contacts).positive? do %>
<%= form.govuk_check_boxes_fieldset :e_and_o_lead, legend: nil, small: true, form_group: { class: "govuk-!-margin-bottom-0" } do %>
<% @filtering.available_provider_contact_options.each do |option| %>
<%= form.govuk_check_box :provider_contact, option.last, exclusive: false, label: { text: option.first } %>
<% end %>
<% end %>
<% end %>
<% end %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
<dl class="govuk-summary-list">
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">Name</dt>
<dd class="govuk-summary-list__value"><%= provider_contact.name %></dd>
<dd class="govuk-summary-list__value"><%= link_to provider_contact.name, frameworks_provider_contact_path(provider_contact, back_to: current_url_b64(:provider_contacts)), class: "govuk-link", "data-turbo" => false %></dd>
</div>

<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">Email</dt>
<dd class="govuk-summary-list__value"><%= provider_contact.email %></dd>
</div>

<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">Phone</dt>
<dd class="govuk-summary-list__value"><%= provider_contact.phone %></dd>
</div>

<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">Provider</dt>
<dd class="govuk-summary-list__value"><%= provider_contact.provider_name %></dd>
Expand Down
21 changes: 21 additions & 0 deletions app/views/frameworks/provider_contacts/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<%= content_for :title, "GHBS | Provider Contacts | Edit #{@provider_contact.name}" %>

<%= turbo_frame_tag dom_id(@provider_contact) do %>
<span class="govuk-caption-l">Edit Framework Provider Contact</span>
<h1 class="govuk-heading-l"><%= @provider_contact.name %></h2>

<h2 class="govuk-heading-m">Details</h2>

<div class="govuk-!-width-two-thirds">
<%= form_for @provider_contact do |f| %>
<%= f.govuk_text_field :name %>
<%= f.govuk_text_field :email %>
<%= f.govuk_text_field :phone %>

<div class="govuk-button-group flex-align-center">
<%= f.govuk_submit "Save changes" %>
<%= link_to "Cancel", @provider_contact, class: "govuk-link govuk-link--no-visited-state" %>
</div>
<% end %>
</div>
<% end %>
40 changes: 40 additions & 0 deletions app/views/frameworks/provider_contacts/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<%= content_for :title, "GHBS | Provider Contacts | #{@provider_contact.name}" %>

<%= turbo_frame_tag dom_id(@provider_contact) do %>
<span class="govuk-caption-l">Framework Provider Contact</span>
<h1 class="govuk-heading-l"><%= @provider_contact.name %></h2>

<h2 class="govuk-heading-m">Details</h2>
<dl class="govuk-summary-list govuk-!-width-two-thirds">
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">Name</dt>
<dd class="govuk-summary-list__value"><%= @provider_contact.name %></dd>
</div>

<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">Email</dt>
<dd class="govuk-summary-list__value"><%= @provider_contact.email %></dd>
</div>

<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">Phone</dt>
<dd class="govuk-summary-list__value"><%= @provider_contact.phone %></dd>
</div>

<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">Provider</dt>
<dd class="govuk-summary-list__value"><%= @provider_contact.provider_name %></dd>
</div>
</dl>

<h2 class="govuk-heading-m">Actions</h2>
<ul class="govuk-list">
<li><%= link_to "Edit Contact", edit_frameworks_provider_contact_path(@provider_contact), class: "govuk-link" %></li>
<li><%= link_to "View Owned Frameworks", frameworks_frameworks_path(frameworks_filter: { provider_contact: [@provider_contact.id] }), class: "govuk-link", "data-turbo" => false %></li>
</ul>

<h2 class="govuk-heading-m">History</h2>
<div class="govuk-!-width-two-thirds">
<%= render "frameworks/activity_log_items/list", activity_log_items: @provider_contact.activity_log_items %>
</div>
<% end %>
Loading

0 comments on commit 66c968c

Please sign in to comment.