Skip to content

Commit

Permalink
feat(fworks): simple register import and audit
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantk committed Sep 12, 2023
1 parent 2988113 commit 15b7242
Show file tree
Hide file tree
Showing 91 changed files with 2,000 additions and 264 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
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ gem "omniauth"
gem "omniauth_openid_connect"
gem "omniauth-rails_csrf_protection"
gem "pandoc-ruby"
gem "paper_trail"
gem "pg"
gem "pg_search"
gem "puma", "~> 6"
Expand All @@ -51,6 +52,7 @@ gem "rollbar"
gem "scenic"
gem "sidekiq", "~> 6.4"
gem "sidekiq-cron", "~> 1.10"
gem "simple_xlsx_reader"
gem "slack-notifier"
gem "sprockets-rails"
gem "stimulus-rails"
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ GEM
validate_url
webfinger (~> 2.0)
pandoc-ruby (2.1.7)
paper_trail (15.0.0)
activerecord (>= 6.1)
request_store (~> 1.4)
parallel (1.23.0)
parser (3.2.2.3)
ast (~> 2.4.1)
Expand Down Expand Up @@ -491,6 +494,8 @@ GEM
redis-store (1.9.2)
redis (>= 4, < 6)
regexp_parser (2.8.1)
request_store (1.5.1)
rack (>= 1.4)
rexml (3.2.6)
rollbar (3.4.0)
rouge (4.1.3)
Expand Down Expand Up @@ -572,6 +577,9 @@ GEM
fugit (~> 1.8)
globalid (>= 1.0.1)
sidekiq (>= 6)
simple_xlsx_reader (5.0.0)
nokogiri
rubyzip
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand Down Expand Up @@ -705,6 +713,7 @@ DEPENDENCIES
omniauth-rails_csrf_protection
omniauth_openid_connect
pandoc-ruby
paper_trail
pg
pg_search
pry-byebug
Expand All @@ -727,6 +736,7 @@ DEPENDENCIES
shoulda-matchers
sidekiq (~> 6.4)
sidekiq-cron (~> 1.10)
simple_xlsx_reader
simplecov
slack-notifier
spring
Expand Down
4 changes: 2 additions & 2 deletions app/assets/stylesheets/application.sass.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

@import "./base/frontend";
@import "./base/misc";
@import "./components/activity-log";
@import "./components/supported/procurement-stage-colours";
@import "./components/govuk-radios-small";
@import "./components/autocomplete";
Expand Down Expand Up @@ -47,12 +48,11 @@
&.wide-container {
max-width: 2000px;
}

@media (min-width: 1020px) {
&.wide-container {
margin-left: 30px;
margin-right: 30px;
}
}
}

4 changes: 4 additions & 0 deletions app/assets/stylesheets/base/_misc.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
align-items: center;
}

.pull-up-10 {
margin-top: -10px;
}

.spread-out {
display: flex;
align-items: center;
Expand Down
20 changes: 20 additions & 0 deletions app/assets/stylesheets/components/_activity-log.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.activity-log-table {
@extend .govuk-\!-font-size-16;

table {
@extend .govuk-\!-font-size-16;
}

.activity-log-date {
width: 180px;
}
.activity-log-person {
width: 180px;
}

.activity-log-change-arrow {
color: #a2a2a2;
margin-left: 5px;
margin-right: 5px;
}
}
15 changes: 12 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ApplicationController < ActionController::Base
default_form_builder GOVUKDesignSystemFormBuilder::FormBuilder

before_action :authenticate_user!, except: %i[health_check maintenance]
before_action :set_current_request_id

protect_from_forgery

Expand All @@ -24,7 +25,7 @@ def maintenance

protected

helper_method :current_user, :cookie_policy, :record_ga?, :engagement_portal?, :support_portal?, :frameworks_portal?
helper_method :current_user, :cookie_policy, :record_ga?, :engagement_portal?, :support_portal?, :frameworks_portal?, :current_url_b64

# @return [User, Guest]
#
Expand Down Expand Up @@ -86,9 +87,17 @@ def cookie_policy
CookiePolicy.new(cookies)
end

def current_url_b64
Base64.encode64(request.fullpath)
def current_url_b64(tab = nil)
Base64.encode64("#{request.fullpath}#{"##{tab.to_s.dasherize}" if tab.present?}")
end

def back_link_param(back_to = params[:back_to])
return if back_to.blank?

Base64.decode64(back_to)
end

def tracking_base_properties = { user_id: current_user.id }

def set_current_request_id = Current.request_id = request.request_id
end
14 changes: 0 additions & 14 deletions app/controllers/engagement/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@ module Engagement
class ApplicationController < ::ApplicationController
include SupportAgents

helper_method :current_url_b64

protected

def back_link_param(back_to = params[:back_to])
return if back_to.blank?

Base64.decode64(back_to)
end

def current_url_b64(tab = "")
Base64.encode64("#{request.fullpath}##{tab.to_s.dasherize}")
end

private

def portal_namespace = :engagement
Expand Down
1 change: 1 addition & 0 deletions app/controllers/frameworks/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Frameworks::ApplicationController < ApplicationController
include SupportAgents
before_action { Current.actor = current_agent }

private

Expand Down
35 changes: 35 additions & 0 deletions app/controllers/frameworks/frameworks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Frameworks::FrameworksController < Frameworks::ApplicationController
before_action :redirect_to_register_tab, unless: :turbo_frame_request?, only: :index
before_action :set_back_url, only: %i[new show]

def new; end

def index
@filtering = Frameworks::Framework.filtering(filter_form_params)
@frameworks = @filtering.results.paginate(page: params[:frameworks_page])
end

def show
@framework = Frameworks::Framework.find(params[:id])
@activity_log_items = @framework.activity_log_items.paginate(page: params[:activities_page])
end

private

def filter_form_params
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
@back_url = back_link_param
end

def redirect_to_register_tab
redirect_to frameworks_root_path(anchor: "frameworks-register", **request.params.except(:controller, :action))
end
end
11 changes: 11 additions & 0 deletions app/controllers/frameworks/management/activity_logs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Frameworks::Management::ActivityLogsController < Frameworks::Management::ApplicationController
before_action { @back_url = frameworks_management_path }

def show
@activity_log_items = Frameworks::ActivityLogItem.paginate(page: params[:page], per_page: 50)
end

private

def authorize_agent_scope = [super, :manage_frameworks_activity_log?]
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Frameworks::Management::ApplicationController < Frameworks::ApplicationController
private

def authorize_agent_scope = [super, :access_admin_settings?]
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Frameworks::Management::ManagementController < Frameworks::Management::ApplicationController
def index; end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Frameworks::Management::RegisterUploadsController < Frameworks::Management::ApplicationController
before_action { @back_url = frameworks_management_path }

def new; end

def create
Frameworks::Framework.import_from_spreadsheet(register_upload_params[:spreadsheet])

redirect_to frameworks_management_path
end

private

def register_upload_params
params.require(:register_upload).permit(:spreadsheet)
end

def authorize_agent_scope = [super, :manage_frameworks_register_upload?]
end
45 changes: 45 additions & 0 deletions app/controllers/frameworks/provider_contacts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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
end
2 changes: 1 addition & 1 deletion app/models/current.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Current < ActiveSupport::CurrentAttributes
attribute :user, :agent
attribute :user, :agent, :actor, :request_id
end
7 changes: 7 additions & 0 deletions app/models/frameworks/activity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Frameworks::Activity
extend ActiveSupport::Concern

included do
has_one :activity_log_item, as: :activity, touch: true
end
end
12 changes: 12 additions & 0 deletions app/models/frameworks/activity_log_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Frameworks::ActivityLogItem < ApplicationRecord
include Presentable

belongs_to :actor, polymorphic: true, optional: true
belongs_to :subject, polymorphic: true, optional: true
delegated_type :activity, types: %w[Frameworks::ActivityLoggableVersion]

before_create do
self.guid ||= Current.request_id
self.actor ||= Current.actor
end
end
11 changes: 11 additions & 0 deletions app/models/frameworks/activity_log_item/presentable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Frameworks::ActivityLogItem::Presentable
extend ActiveSupport::Concern

def display_created_at
created_at.strftime("%d/%m/%Y %H:%M:%S")
end

def display_actor
actor.try(:full_name)
end
end
19 changes: 19 additions & 0 deletions app/models/frameworks/activity_loggable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Frameworks::ActivityLoggable
extend ActiveSupport::Concern
include ActivityLogPresentable

included do
has_paper_trail versions: { class_name: "Frameworks::ActivityLoggableVersion" }

has_many :activity_log_items, as: :subject

after_create :log_latest_version_in_activity_log
after_update :log_latest_version_in_activity_log
end

private

def log_latest_version_in_activity_log
Frameworks::ActivityLogItem.create!(subject: self, activity: versions.last, activity_type: "Frameworks::ActivityLoggableVersion")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Frameworks::ActivityLoggable::ActivityLogPresentable
extend ActiveSupport::Concern

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_type
self.class.to_s.demodulize.underscore.humanize
end
end
8 changes: 8 additions & 0 deletions app/models/frameworks/activity_loggable_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Frameworks::ActivityLoggableVersion < PaperTrail::Version
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
Loading

0 comments on commit 15b7242

Please sign in to comment.