Skip to content

Commit

Permalink
feat(fworks): basic import, register and log
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantk committed Sep 7, 2023
1 parent 622aaa1 commit 16e3b06
Show file tree
Hide file tree
Showing 79 changed files with 1,645 additions and 263 deletions.
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
30 changes: 30 additions & 0 deletions app/controllers/frameworks/frameworks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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: [])
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
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[PaperTrail::Version]

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)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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
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}")
end
end
5 changes: 5 additions & 0 deletions app/models/frameworks/activity_loggable_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Frameworks::ActivityLoggableVersion < PaperTrail::Version
def presentable_chages
PresentableChanges.new(self)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Frameworks::ActivityLoggableVersion::PresentableChanges
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 non_common_object_changes
@version.object_changes
.except("id", "created_at", "updated_at")
.map { |field, changes| change_type(field).new(@version, field, changes) }
end

def change_type(field)
field.ends_with?("_id") ? BelongsToAssociationChange : FieldChange
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Frameworks::ActivityLoggableVersion::PresentableChanges::BelongsToAssociationChange < Frameworks::ActivityLoggableVersion::PresentableChanges::FieldChange
private

def to
item.activity_log_association_display_name(association:, id: changes.last)
end

def from
item.activity_log_association_display_name(association:, id: changes.first)
end

def association
field.split("_id").first
end
end
Loading

0 comments on commit 16e3b06

Please sign in to comment.