• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_describe / 35525209-8c8b-426d-b992-849111d800ff

02 Jun 2026 07:47PM UTC coverage: 41.528% (-54.0%) from 95.52%
35525209-8c8b-426d-b992-849111d800ff

Pull #2324

circleci

carolyncole
Updating code to allow for multiple WorkActivity classes based on the transition
Pull Request #2324: Updating code to allow for multiple WorkActivity classes based on the…

35 of 87 new or added lines in 6 files covered. (40.23%)

2096 existing lines in 79 files now uncovered.

1647 of 3966 relevant lines covered (41.53%)

4.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

22.73
/app/controllers/application_controller.rb
1
# frozen_string_literal: true
2
class ApplicationController < ActionController::Base
1✔
3
  protect_from_forgery with: :exception
1✔
4
  before_action :authenticate_user!
1✔
5
  around_action :rescue_redis_error
1✔
6

7
  def new_session_path(_scope)
1✔
8
    new_user_session_path
×
9
  end
10

11
  private
1✔
12

13
    # Take all the errors from the exception and the work and combine them into a single error message that can be shown to the user
14
    #
15
    # @param [AASM::InvalidTransition] aasm_error Error thrown by trying to transition states
16
    # @param [Work] work The work that had the issue
17
    #
18
    # @return [String] a combined error message for the work and transition error
19
    #
20
    def message_from_aasm_error(aasm_error:, work:)
1✔
UNCOV
21
      message = aasm_error.message
×
UNCOV
22
      if work.errors.count > 0
×
UNCOV
23
        message = work.errors.to_a.join(", ")
×
24
      end
UNCOV
25
      message.chop! if message.last == "."
×
UNCOV
26
      message
×
27
    end
28

29
    # Validates that the current user can modify the work
30
    #
31
    # @params [Work] work the work to be modifed
32
    # @params [String] uneditable_message message to send to honey bandger about the work not being editable by the user
33
    # @params [String] current_state_message message to send to honey bandger about the work not being editable in the current state
34
    #
35
    # @returns false if an error occured
36
    #
37
    def validate_modification_permissions(work:, uneditable_message:, current_state_message:)
1✔
UNCOV
38
      no_error = false
×
UNCOV
39
      if current_user.blank? || !work.editable_by?(current_user)
×
UNCOV
40
        Honeybadger.notify(uneditable_message)
×
UNCOV
41
        redirect_to root_path, notice: I18n.t("works.uneditable.privs")
×
UNCOV
42
      elsif !work.editable_in_current_state?(current_user)
×
UNCOV
43
        Honeybadger.notify(current_state_message)
×
UNCOV
44
        redirect_to root_path, notice: I18n.t("works.uneditable.approved")
×
45
      else
UNCOV
46
        no_error = true
×
47
      end
48

UNCOV
49
      no_error
×
50
    end
51

52
    # returns either the group sent in by the user in group_id
53
    #   OR the default group for the user
54
    #
55
    def params_group_id
1✔
56
      # Do not allow a nil for the group id
UNCOV
57
      @params_group_id ||= begin
×
UNCOV
58
        group_id = params[:group_id]
×
UNCOV
59
        if group_id.blank?
×
UNCOV
60
          group_id = current_user.default_group.id
×
UNCOV
61
          Honeybadger.notify("We got a nil group as part of the parameters #{params} #{request}")
×
62
        end
UNCOV
63
        group_id
×
64
      end
65
    end
66

67
    # parses the embargo date from the parameters and parses into a Date object
68
    def embargo_date
1✔
UNCOV
69
      embargo_date_param = params["embargo-date"]
×
UNCOV
70
      return nil if embargo_date_param.blank?
×
71

UNCOV
72
      Date.parse(embargo_date_param)
×
73
    rescue Date::Error
UNCOV
74
      message = "Failed to parse the embargo date #{embargo_date_param} for Work #{@work.id}"
×
UNCOV
75
      Rails.logger.error(message)
×
UNCOV
76
      Honeybadger.notify(message)
×
UNCOV
77
      nil
×
78
    end
79

80
    # parameters utilize to update the work converted from the user's parameters
81
    def update_params
1✔
82
      {
UNCOV
83
        group_id: params_group_id,
×
84
        embargo_date:,
85
        resource: FormToResourceService.convert(params, @work)
86
      }
87
    end
88

89
    def prepare_decorators_for_work_form(work)
1✔
UNCOV
90
      @work_decorator = WorkDecorator.new(work, current_user)
×
UNCOV
91
      @form_resource_decorator = FormResourceDecorator.new(work, current_user)
×
92
    end
93

94
    def rescue_aasm_error
1✔
UNCOV
95
      yield
×
96
    rescue AASM::InvalidTransition => error
UNCOV
97
      message = message_from_aasm_error(aasm_error: error, work: @work)
×
98

UNCOV
99
      Honeybadger.notify("Invalid #{@work.current_transition}: #{error.message} errors: #{message}")
×
UNCOV
100
      transition_error_message = "We apologize, the following errors were encountered: #{message}. Please contact the PDC Describe administrators for any assistance."
×
UNCOV
101
      @errors = [transition_error_message]
×
102

UNCOV
103
      redirect_aasm_error(transition_error_message)
×
104
    end
105

106
    def rescue_redis_error
1✔
UNCOV
107
      yield
×
108
    rescue HealthMonitor::Providers::RedisException => error
UNCOV
109
      Honeybadger.notify("Connection failure for Redis: #{error.message}")
×
110

UNCOV
111
      redirect_to root_path
×
112
    end
113

114
    # Logs if the work is being updated with stale data.
115
    #
116
    # This can happen if user A opens two browser windows, edits the data on both of them,
117
    # and saves from both of them. This could also happen if a user A edits the data,
118
    # another user (say user B) also edits the data and saves it before user A saves their
119
    # own changes.
120
    def check_for_stale_update(work, params)
1✔
UNCOV
121
      stale = false
×
UNCOV
122
      last_updated_at = params["last_updated_at"] || "" # when was this record last saved (when the edit form was loaded)
×
UNCOV
123
      loaded_at = params["loaded_at"] || "00:00" # when was the edit form loaded
×
UNCOV
124
      edit_time = (Time.now.in_time_zone - loaded_at.to_time).to_i
×
UNCOV
125
      if work.updated_at.to_s != last_updated_at
×
UNCOV
126
        log_message = "Update to work #{work.id} by #{current_user.uid} via will overwrite changes"
×
UNCOV
127
        log_message << " (current: #{work.updated_at}, with: #{last_updated_at}, edit time: #{edit_time} seconds)"
×
UNCOV
128
        Rails.logger.warn log_message
×
UNCOV
129
        Honeybadger.notify log_message
×
UNCOV
130
        stale = true
×
131
      end
UNCOV
132
      stale
×
133
    end
134
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc