From 977c03bc0b1d28c9aa42726e4797389db741a9f0 Mon Sep 17 00:00:00 2001 From: Chuck Greenman Date: Fri, 31 Jan 2020 14:06:53 -0500 Subject: [PATCH 1/8] Add paper_trail to the Gemfile. --- Gemfile | 2 ++ Gemfile.lock | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/Gemfile b/Gemfile index 480b2399..93e2a1aa 100644 --- a/Gemfile +++ b/Gemfile @@ -58,6 +58,8 @@ gem 'dotenv-rails' gem 'cancancan' +gem 'paper_trail' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: %i[mri mingw x64_mingw] diff --git a/Gemfile.lock b/Gemfile.lock index 79b0161f..26ab6f6b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -144,6 +144,9 @@ GEM mini_portile2 (~> 2.4.0) orm_adapter (0.5.0) pagy (3.10.0) + paper_trail (11.1.0) + activerecord (>= 5.2) + request_store (~> 1.1) parallel (1.20.1) parser (2.7.2.0) ast (~> 2.4.1) @@ -189,6 +192,8 @@ GEM ffi (~> 1.0) rb-readline (0.5.5) regexp_parser (1.8.2) + request_store (1.5.0) + rack (>= 1.4) responders (3.0.1) actionpack (>= 5.0) railties (>= 5.0) @@ -320,6 +325,7 @@ DEPENDENCIES listen (>= 3.0.5, < 3.2) mysql2 pagy (~> 3.7) + paper_trail pdfkit puma (~> 3.11) rails (~> 5.2.3) From a9d304424d0cffbaee896edd619309b6d31c9994 Mon Sep 17 00:00:00 2001 From: Chuck Greenman Date: Fri, 31 Jan 2020 14:10:39 -0500 Subject: [PATCH 2/8] Install paper_trail. --- db/migrate/20200131190904_create_versions.rb | 36 ++++++++++++++++++++ db/schema.rb | 10 ++++++ 2 files changed, 46 insertions(+) create mode 100644 db/migrate/20200131190904_create_versions.rb diff --git a/db/migrate/20200131190904_create_versions.rb b/db/migrate/20200131190904_create_versions.rb new file mode 100644 index 00000000..b67cca75 --- /dev/null +++ b/db/migrate/20200131190904_create_versions.rb @@ -0,0 +1,36 @@ +# This migration creates the `versions` table, the only schema PT requires. +# All other migrations PT provides are optional. +class CreateVersions < ActiveRecord::Migration[5.2] + + # The largest text column available in all supported RDBMS is + # 1024^3 - 1 bytes, roughly one gibibyte. We specify a size + # so that MySQL will use `longtext` instead of `text`. Otherwise, + # when serializing very large objects, `text` might not be big enough. + TEXT_BYTES = 1_073_741_823 + + def change + create_table :versions do |t| + t.string :item_type, {:null=>false} + t.integer :item_id, null: false, limit: 8 + t.string :event, null: false + t.string :whodunnit + t.text :object, limit: TEXT_BYTES + + # Known issue in MySQL: fractional second precision + # ------------------------------------------------- + # + # MySQL timestamp columns do not support fractional seconds unless + # defined with "fractional seconds precision". MySQL users should manually + # add fractional seconds precision to this migration, specifically, to + # the `created_at` column. + # (https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html) + # + # MySQL users should also upgrade to at least rails 4.2, which is the first + # version of ActiveRecord with support for fractional seconds in MySQL. + # (https://github.com/rails/rails/pull/14359) + # + t.datetime :created_at + end + add_index :versions, %i(item_type item_id) + end +end diff --git a/db/schema.rb b/db/schema.rb index f768eb50..66e8205f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -129,4 +129,14 @@ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end + create_table "versions", force: :cascade do |t| + t.string "item_type", null: false + t.integer "item_id", limit: 8, null: false + t.string "event", null: false + t.string "whodunnit" + t.text "object", limit: 1073741823 + t.datetime "created_at" + t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id" + end + end From c80ae72aad459ce9d99a0a7f3b5fd310d7ee31de Mon Sep 17 00:00:00 2001 From: Chuck Greenman Date: Fri, 31 Jan 2020 15:49:58 -0500 Subject: [PATCH 3/8] In progress activity management --- app/assets/javascripts/activity.coffee | 3 ++ app/assets/stylesheets/activity.scss | 3 ++ app/controllers/activity_controller.rb | 10 +++++ app/controllers/application_controller.rb | 1 + app/helpers/activity_helper.rb | 42 ++++++++++++++++++ app/models/conservation_record.rb | 2 + app/views/activity/index.html.erb | 17 +++++++ app/views/activity/show.html.erb | 44 +++++++++++++++++++ config/routes.rb | 1 + ...31192546_add_object_changes_to_versions.rb | 12 +++++ db/schema.rb | 1 + spec/controllers/activity_controller_spec.rb | 6 +++ spec/helpers/activity_helper_spec.rb | 17 +++++++ 13 files changed, 159 insertions(+) create mode 100644 app/assets/javascripts/activity.coffee create mode 100644 app/assets/stylesheets/activity.scss create mode 100644 app/controllers/activity_controller.rb create mode 100644 app/helpers/activity_helper.rb create mode 100644 app/views/activity/index.html.erb create mode 100644 app/views/activity/show.html.erb create mode 100644 db/migrate/20200131192546_add_object_changes_to_versions.rb create mode 100644 spec/controllers/activity_controller_spec.rb create mode 100644 spec/helpers/activity_helper_spec.rb diff --git a/app/assets/javascripts/activity.coffee b/app/assets/javascripts/activity.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/activity.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/activity.scss b/app/assets/stylesheets/activity.scss new file mode 100644 index 00000000..0b73d867 --- /dev/null +++ b/app/assets/stylesheets/activity.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the activity controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/activity_controller.rb b/app/controllers/activity_controller.rb new file mode 100644 index 00000000..e769c127 --- /dev/null +++ b/app/controllers/activity_controller.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class ActivityController < ApplicationController + def index; end + + def show + @version = PaperTrail::Version.find(params[:id]) + @other_versions = PaperTrail::Version.where(item_id: @version.item_id, item_type: @version.item_type) + end +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f1cdab07..bcd2c684 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,6 +3,7 @@ class ApplicationController < ActionController::Base include Pagy::Backend before_action :configure_permitted_parameters, if: :devise_controller? + before_action :set_paper_trail_whodunnit rescue_from CanCan::AccessDenied do |exception| flash[:notice] = exception.message diff --git a/app/helpers/activity_helper.rb b/app/helpers/activity_helper.rb new file mode 100644 index 00000000..99e86639 --- /dev/null +++ b/app/helpers/activity_helper.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +module ActivityHelper + def version_summarizer(version) + user = User.find(version.whodunnit) + user.display_name + ' ' + event_to_summary(version.event) + ' the ' + item_type_to_summary(version.item_type) + ': ' + name_to_summary(version) + end + + def event_to_summary(event) + case event + when 'create' + 'created' + when 'update' + 'updated' + when 'destroy' + 'deleted' + else + 'did something to' + end + end + + def item_type_to_summary(item_type) + case item_type + when 'ConservationRecord' + 'conservation record' + else + 'some item' + end + end + + def name_to_summary(version) + type = version.item_type + id = version.item_id + + if type == 'ConservationRecord' + link_to ConservationRecord.find(id).title, conservation_record_path(id) + elsif type == 'TreatmentReport' + treatment_report_id = TreatmentReport.find(id).conservation_record + link_to ConservationRecord.find(treatment_report_id).title + "'s treatment report", conservation_record_path(id) + end + end +end diff --git a/app/models/conservation_record.rb b/app/models/conservation_record.rb index ac1c0158..60a23f9f 100644 --- a/app/models/conservation_record.rb +++ b/app/models/conservation_record.rb @@ -9,4 +9,6 @@ class ConservationRecord < ApplicationRecord has_one :cost_return_report, dependent: :destroy validates :department, :title, :author, :imprint, :call_number, :item_record_number, presence: true + + has_paper_trail end diff --git a/app/views/activity/index.html.erb b/app/views/activity/index.html.erb new file mode 100644 index 00000000..799e9c2c --- /dev/null +++ b/app/views/activity/index.html.erb @@ -0,0 +1,17 @@ +

Recent Activity

+ + + + + + + + + <% PaperTrail::Version.all.each do |version| %> + + + + + <% end %> + +
ActionDetail
<%= version_summarizer(version).html_safe %><%= link_to "Details", activity_path(version) %>
\ No newline at end of file diff --git a/app/views/activity/show.html.erb b/app/views/activity/show.html.erb new file mode 100644 index 00000000..cda7678d --- /dev/null +++ b/app/views/activity/show.html.erb @@ -0,0 +1,44 @@ +

<%= version_summarizer(@version).html_safe %>

+ +
+ +
+
+ + + + + + + + + + <% @version.changeset.each do |change| %> + + + + + + <% end %> + +
FieldWasIs
<%= change[0] %><%= change[1][0] %><%= change[1][1] %>
+ +

Recent Activity on this Record

+<%= @other_versions.inspect %> + + + + + + + + + <% @version.changeset.each do |change| %> + + + + + + <% end %> + +
FieldWasIs
<%= change[0] %><%= change[1][0] %><%= change[1][1] %>
\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 43864afe..f5af3fa4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ resources :users resources :controlled_vocabularies, except: [:destroy] + resources :activity resources :conservation_records do resources :in_house_repair_records diff --git a/db/migrate/20200131192546_add_object_changes_to_versions.rb b/db/migrate/20200131192546_add_object_changes_to_versions.rb new file mode 100644 index 00000000..b5f2fb4b --- /dev/null +++ b/db/migrate/20200131192546_add_object_changes_to_versions.rb @@ -0,0 +1,12 @@ +# This migration adds the optional `object_changes` column, in which PaperTrail +# will store the `changes` diff for each update event. See the readme for +# details. +class AddObjectChangesToVersions < ActiveRecord::Migration[5.2] + # The largest text column available in all supported RDBMS. + # See `create_versions.rb` for details. + TEXT_BYTES = 1_073_741_823 + + def change + add_column :versions, :object_changes, :text, limit: TEXT_BYTES + end +end diff --git a/db/schema.rb b/db/schema.rb index 66e8205f..87146687 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -136,6 +136,7 @@ t.string "whodunnit" t.text "object", limit: 1073741823 t.datetime "created_at" + t.text "object_changes", limit: 1073741823 t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id" end diff --git a/spec/controllers/activity_controller_spec.rb b/spec/controllers/activity_controller_spec.rb new file mode 100644 index 00000000..66b810cc --- /dev/null +++ b/spec/controllers/activity_controller_spec.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe ActivityController, type: :controller do +end diff --git a/spec/helpers/activity_helper_spec.rb b/spec/helpers/activity_helper_spec.rb new file mode 100644 index 00000000..539a35a3 --- /dev/null +++ b/spec/helpers/activity_helper_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the ActivityHelper. For example: +# +# describe ActivityHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe ActivityHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end From f4db5a9feaeafd99016dfa92dfc3cad016e027f5 Mon Sep 17 00:00:00 2001 From: Chuck Greenman Date: Fri, 31 Jan 2020 17:02:33 -0500 Subject: [PATCH 4/8] In progress. --- app/controllers/activity_controller.rb | 2 +- app/helpers/activity_helper.rb | 23 ++++++++-- app/models/external_repair_record.rb | 2 + app/models/in_house_repair_record.rb | 2 + app/models/treatment_report.rb | 2 + app/models/user.rb | 2 + app/views/activity/index.html.erb | 6 ++- app/views/activity/show.html.erb | 63 ++++++++++++++++---------- 8 files changed, 71 insertions(+), 31 deletions(-) diff --git a/app/controllers/activity_controller.rb b/app/controllers/activity_controller.rb index e769c127..30dd78c2 100644 --- a/app/controllers/activity_controller.rb +++ b/app/controllers/activity_controller.rb @@ -5,6 +5,6 @@ def index; end def show @version = PaperTrail::Version.find(params[:id]) - @other_versions = PaperTrail::Version.where(item_id: @version.item_id, item_type: @version.item_type) + @other_versions = PaperTrail::Version.where(item_id: @version.item_id, item_type: @version.item_type).order('created_at DESC') end end diff --git a/app/helpers/activity_helper.rb b/app/helpers/activity_helper.rb index 99e86639..2f9083a7 100644 --- a/app/helpers/activity_helper.rb +++ b/app/helpers/activity_helper.rb @@ -2,8 +2,11 @@ module ActivityHelper def version_summarizer(version) - user = User.find(version.whodunnit) - user.display_name + ' ' + event_to_summary(version.event) + ' the ' + item_type_to_summary(version.item_type) + ': ' + name_to_summary(version) + display_name = 'Someone' + if !version.whodunnit.nil? + display_name = User.find(version.whodunnit).display_name + end + display_name + ' ' + event_to_summary(version.event) + ' the ' + item_type_to_summary(version.item_type) + ': ' + name_to_summary(version) end def event_to_summary(event) @@ -23,6 +26,14 @@ def item_type_to_summary(item_type) case item_type when 'ConservationRecord' 'conservation record' + when 'TreatmentReport' + 'treatment record' + when 'ExternalRepairRecord' + 'external repair record' + when 'InHouseRepairRecord' + 'in house repair record' + when 'User' + 'user' else 'some item' end @@ -35,8 +46,14 @@ def name_to_summary(version) if type == 'ConservationRecord' link_to ConservationRecord.find(id).title, conservation_record_path(id) elsif type == 'TreatmentReport' - treatment_report_id = TreatmentReport.find(id).conservation_record + treatment_report_id = TreatmentReport.find(id).conservation_record.id link_to ConservationRecord.find(treatment_report_id).title + "'s treatment report", conservation_record_path(id) + elsif type == "User" + User.find(id).display_name end end + + def changeset_format(field, value) + + end end diff --git a/app/models/external_repair_record.rb b/app/models/external_repair_record.rb index d48e0728..624931b0 100644 --- a/app/models/external_repair_record.rb +++ b/app/models/external_repair_record.rb @@ -2,4 +2,6 @@ class ExternalRepairRecord < ApplicationRecord belongs_to :conservation_record + + has_paper_trail end diff --git a/app/models/in_house_repair_record.rb b/app/models/in_house_repair_record.rb index 625ac126..d02a04f8 100644 --- a/app/models/in_house_repair_record.rb +++ b/app/models/in_house_repair_record.rb @@ -2,4 +2,6 @@ class InHouseRepairRecord < ApplicationRecord belongs_to :conservation_record + + has_paper_trail end diff --git a/app/models/treatment_report.rb b/app/models/treatment_report.rb index 3b700c24..946048a9 100644 --- a/app/models/treatment_report.rb +++ b/app/models/treatment_report.rb @@ -2,4 +2,6 @@ class TreatmentReport < ApplicationRecord belongs_to :conservation_record + + has_paper_trail end diff --git a/app/models/user.rb b/app/models/user.rb index 8062310b..253e17d4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -28,4 +28,6 @@ def active_for_authentication? super && account_active? end + + has_paper_trail end diff --git a/app/views/activity/index.html.erb b/app/views/activity/index.html.erb index 799e9c2c..e5c3768a 100644 --- a/app/views/activity/index.html.erb +++ b/app/views/activity/index.html.erb @@ -2,14 +2,16 @@ - + + - <% PaperTrail::Version.all.each do |version| %> + <% PaperTrail::Version.all.order('created_at DESC').each do |version| %> + <% end %> diff --git a/app/views/activity/show.html.erb b/app/views/activity/show.html.erb index cda7678d..a47f1f98 100644 --- a/app/views/activity/show.html.erb +++ b/app/views/activity/show.html.erb @@ -5,39 +5,52 @@
-
ActionActivityWhen Detail
<%= version_summarizer(version).html_safe %><%= version.created_at.in_time_zone("America/New_York").strftime("%-m/%-d/%y: %H:%M %Z") %> <%= link_to "Details", activity_path(version) %>
- - - - - +<% if @version.event == 'create' %> +
+
+
+
No Diff Information to Show
+
+

This version was logged right after it was created, so there is nothing to compare against.

+
+
+
+<% else %> +
FieldWasIs
+ + + + + - - <% @version.changeset.each do |change| %> - - - - - - <% end %> - -
FieldWasIs
<%= change[0] %><%= change[1][0] %><%= change[1][1] %>
+ + <% @version.changeset.each do |change| %> + + <%= change[0] %> + <%= change[1][0] %> + <%= change[1][1] %> + + <% end %> + + +<% end %> + + +

Other Activity on this Record

-

Recent Activity on this Record

-<%= @other_versions.inspect %> - - - + + + - <% @version.changeset.each do |change| %> + <% @other_versions.all.each do |version| %> - - - + + <% end %> From 294ad34346324edf3fde639c7e884febb7a106f4 Mon Sep 17 00:00:00 2001 From: Glen Horton Date: Thu, 17 Dec 2020 18:00:55 -0500 Subject: [PATCH 5/8] Satisfy rubocop --- .rubocop.yml | 1 + app/helpers/activity_helper.rb | 19 ++++++++----------- app/models/treatment_report.rb | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 4a75002d..20d671bc 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -34,6 +34,7 @@ Metrics/MethodLength: - "app/controllers/search_controller.rb" - "app/controllers/treatment_reports_controller.rb" - "app/models/ability.rb" + - "app/helpers/activity_helper.rb" Style/ExpandPathArguments: Exclude: diff --git a/app/helpers/activity_helper.rb b/app/helpers/activity_helper.rb index 2f9083a7..51866486 100644 --- a/app/helpers/activity_helper.rb +++ b/app/helpers/activity_helper.rb @@ -3,10 +3,8 @@ module ActivityHelper def version_summarizer(version) display_name = 'Someone' - if !version.whodunnit.nil? - display_name = User.find(version.whodunnit).display_name - end - display_name + ' ' + event_to_summary(version.event) + ' the ' + item_type_to_summary(version.item_type) + ': ' + name_to_summary(version) + display_name = User.find(version.whodunnit).display_name unless version.whodunnit.nil? + "#{display_name} #{event_to_summary(version.event)} the #{item_type_to_summary(version.item_type)}: #{name_to_summary(version)}" end def event_to_summary(event) @@ -43,17 +41,16 @@ def name_to_summary(version) type = version.item_type id = version.item_id - if type == 'ConservationRecord' + case type + when 'ConservationRecord' link_to ConservationRecord.find(id).title, conservation_record_path(id) - elsif type == 'TreatmentReport' + when 'TreatmentReport' treatment_report_id = TreatmentReport.find(id).conservation_record.id - link_to ConservationRecord.find(treatment_report_id).title + "'s treatment report", conservation_record_path(id) - elsif type == "User" + link_to "#{ConservationRecord.find(treatment_report_id).title}'s treatment report", conservation_record_path(id) + when 'User' User.find(id).display_name end end - def changeset_format(field, value) - - end + def changeset_format(field, value); end end diff --git a/app/models/treatment_report.rb b/app/models/treatment_report.rb index 946048a9..e20988f2 100644 --- a/app/models/treatment_report.rb +++ b/app/models/treatment_report.rb @@ -2,6 +2,6 @@ class TreatmentReport < ApplicationRecord belongs_to :conservation_record - + has_paper_trail end From 9eaa72f63460533dffea841e1eeb18953f3a3673 Mon Sep 17 00:00:00 2001 From: Glen Horton Date: Thu, 17 Dec 2020 18:02:31 -0500 Subject: [PATCH 6/8] Add .env files to .gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index ba9a127b..b7035cf3 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,7 @@ # Don't track .DS_Store on MacOS .DS_Store + +.env.production.local +.env.test.local +.env.development.local From aeab1bc80bd1621d34503368d83b4c7d0918d67e Mon Sep 17 00:00:00 2001 From: Glen Horton Date: Thu, 17 Dec 2020 18:08:53 -0500 Subject: [PATCH 7/8] Add Devise controller helper --- spec/rails_helper.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 43df1da7..1cd8ba21 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -64,4 +64,6 @@ config.include FactoryBot::Syntax::Methods Rails.application.load_seed + + config.include Devise::Test::ControllerHelpers, type: :controller end From 91404dab77239e07271785e5300fdb0eb748c036 Mon Sep 17 00:00:00 2001 From: harithavytla Date: Fri, 8 Jan 2021 11:44:11 -0600 Subject: [PATCH 8/8] Paper Trail Paper Trail Specs PapertTrail Paper trail Specs Paper Trail Paper Trail Paper Trail Paper Trail Paper Trail --- .rubocop.yml | 12 +- app/controllers/activity_controller.rb | 4 +- app/helpers/activity_helper.rb | 48 ++- app/models/cost_return_report.rb | 1 + app/views/activity/index.html.erb | 5 +- app/views/activity/show.html.erb | 7 +- app/views/conservation_records/index.html.erb | 4 +- app/views/shared/_navigation.html.erb | 3 + spec/controllers/activity_controller_spec.rb | 44 +++ spec/factories/activity.rb | 8 + spec/helpers/activity_helper_spec.rb | 335 +++++++++++++++++- spec/views/activity/index.html.erb | 48 +++ spec/views/activity/show.html.erb | 38 ++ .../index.html.erb_spec.rb | 4 +- 14 files changed, 537 insertions(+), 24 deletions(-) create mode 100644 spec/factories/activity.rb create mode 100644 spec/views/activity/index.html.erb create mode 100644 spec/views/activity/show.html.erb diff --git a/.rubocop.yml b/.rubocop.yml index 20d671bc..8d693b48 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -20,6 +20,7 @@ Metrics/AbcSize: Exclude: - app/helpers/in_house_repair_records_helper.rb - app/helpers/external_repair_records_helper.rb + - app/helpers/activity_helper.rb Metrics/BlockLength: Exclude: @@ -28,7 +29,8 @@ Metrics/BlockLength: Metrics/LineLength: Exclude: - "app/views/conservation_records/_conservation_record.json.jbuilder" - + - "spec/helpers/activity_helper_spec.rb" + Metrics/MethodLength: Exclude: - "app/controllers/search_controller.rb" @@ -36,6 +38,14 @@ Metrics/MethodLength: - "app/models/ability.rb" - "app/helpers/activity_helper.rb" +Metrics/CyclomaticComplexity: + Exclude: + - "app/helpers/activity_helper.rb" + +Metrics/PerceivedComplexity: + Exclude: + - "app/helpers/activity_helper.rb" + Style/ExpandPathArguments: Exclude: - "config/puma.rb" diff --git a/app/controllers/activity_controller.rb b/app/controllers/activity_controller.rb index 30dd78c2..71fc2848 100644 --- a/app/controllers/activity_controller.rb +++ b/app/controllers/activity_controller.rb @@ -1,7 +1,9 @@ # frozen_string_literal: true class ActivityController < ApplicationController - def index; end + def index + @pagy, @versions = pagy(PaperTrail::Version.all, items: 50) + end def show @version = PaperTrail::Version.find(params[:id]) diff --git a/app/helpers/activity_helper.rb b/app/helpers/activity_helper.rb index 51866486..d692e9f6 100644 --- a/app/helpers/activity_helper.rb +++ b/app/helpers/activity_helper.rb @@ -25,30 +25,60 @@ def item_type_to_summary(item_type) when 'ConservationRecord' 'conservation record' when 'TreatmentReport' - 'treatment record' + 'treatment report' when 'ExternalRepairRecord' 'external repair record' when 'InHouseRepairRecord' 'in house repair record' when 'User' 'user' + when 'CostReturnReport' + 'cost return report' else 'some item' end end def name_to_summary(version) - type = version.item_type - id = version.item_id - - case type + case version.item_type when 'ConservationRecord' - link_to ConservationRecord.find(id).title, conservation_record_path(id) + if ConservationRecord.where(id: version.item_id).exists? + link_to ConservationRecord.find(version.item_id).title, conservation_record_path(version.item_id) + else + version.object.nil? ? ' ' : version.object.try(:split, 'title: ').last.try(:split, 'author: ').first.chomp + end when 'TreatmentReport' - treatment_report_id = TreatmentReport.find(id).conservation_record.id - link_to "#{ConservationRecord.find(treatment_report_id).title}'s treatment report", conservation_record_path(id) + if TreatmentReport.where(id: version.item_id).exists? + treatment_report_id = TreatmentReport.find(version.item_id).conservation_record.id + link_to ConservationRecord.find(treatment_report_id).title.to_s, conservation_record_path(treatment_report_id) + else + # version.object.split('title: ').last.split('author: ').first.chomp + 'Record has been deleted' + end when 'User' - User.find(id).display_name + User.find(version.item_id).display_name + when 'InHouseRepairRecord' + if InHouseRepairRecord.where(id: version.item_id).exists? + in_house_repair_id = InHouseRepairRecord.find(version.item_id).conservation_record.id + link_to InHouseRepairRecord.find(version.item_id).conservation_record.title, conservation_record_path(in_house_repair_id) + else + 'Record has been deleted' + end + when 'ExternalRepairRecord' + if ExternalRepairRecord.where(id: version.item_id).exists? + external_repair_id = ExternalRepairRecord.find(version.item_id).conservation_record.id + link_to ExternalRepairRecord.find(version.item_id).conservation_record.title, conservation_record_path(external_repair_id) + else + 'Record has been deleted' + end + when 'CostReturnReport' + if CostReturnReport.where(id: version.item_id).exists? + cost_return_report_id = CostReturnReport.find(version.item_id).conservation_record.id + link_to ConservationRecord.find(cost_return_report_id).title.to_s, conservation_record_path(cost_return_report_id) + else + # CostReturnReport.find(version.item_id).conservation_record.title + 'Record has been deleted' + end end end diff --git a/app/models/cost_return_report.rb b/app/models/cost_return_report.rb index c63e6cfa..01c4eea2 100644 --- a/app/models/cost_return_report.rb +++ b/app/models/cost_return_report.rb @@ -2,4 +2,5 @@ class CostReturnReport < ApplicationRecord belongs_to :conservation_record + has_paper_trail end diff --git a/app/views/activity/index.html.erb b/app/views/activity/index.html.erb index e5c3768a..6343ca91 100644 --- a/app/views/activity/index.html.erb +++ b/app/views/activity/index.html.erb @@ -8,7 +8,7 @@ - <% PaperTrail::Version.all.order('created_at DESC').each do |version| %> + <% @versions.order('created_at DESC').each do |version| %> <% end %> -
FieldWasIsActionWhenDetail
<%= change[0] %><%= change[1][0] %><%= change[1][1] %><%= version_summarizer(version).html_safe %><%= version.created_at.in_time_zone("America/New_York").strftime("%-m/%-d/%y: %H:%M %Z") %> + <%= link_to "Details", activity_path(version) %>
<%= version_summarizer(version).html_safe %> <%= version.created_at.in_time_zone("America/New_York").strftime("%-m/%-d/%y: %H:%M %Z") %> @@ -16,4 +16,5 @@
\ No newline at end of file + +
<%== pagy_bootstrap_nav(@pagy).html_safe %>
diff --git a/app/views/activity/show.html.erb b/app/views/activity/show.html.erb index a47f1f98..feb6e965 100644 --- a/app/views/activity/show.html.erb +++ b/app/views/activity/show.html.erb @@ -1,10 +1,5 @@

<%= version_summarizer(@version).html_safe %>

-
- -
-
- <% if @version.event == 'create' %>
@@ -54,4 +49,4 @@ <% end %> - \ No newline at end of file + diff --git a/app/views/conservation_records/index.html.erb b/app/views/conservation_records/index.html.erb index 0115451d..b4c03ad4 100644 --- a/app/views/conservation_records/index.html.erb +++ b/app/views/conservation_records/index.html.erb @@ -31,8 +31,8 @@ <% if can? :crud, ConservationRecord %> <%= link_to 'Edit', edit_conservation_record_path(conservation_record) %> - <%= link_to 'Destroy', conservation_record, method: :delete, data: { confirm: 'Are you sure?' } %> - <% end %> + <%= link_to 'Destroy', conservation_record, method: :delete, data: { confirm: 'Are you sure?' } %> + <% end %> <% end %> diff --git a/app/views/shared/_navigation.html.erb b/app/views/shared/_navigation.html.erb index b4d5a0ff..19c95014 100644 --- a/app/views/shared/_navigation.html.erb +++ b/app/views/shared/_navigation.html.erb @@ -19,6 +19,9 @@ + <% end %>