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

pulibrary / pdc_describe / 40b615b9-79d2-4e09-b764-9b235482309b

20 Apr 2026 05:39PM UTC coverage: 94.16% (-1.3%) from 95.44%
40b615b9-79d2-4e09-b764-9b235482309b

Pull #2261

circleci

bess
Style fix
Pull Request #2261: Fix vite deploy

3531 of 3750 relevant lines covered (94.16%)

227.26 hits per line

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

95.45
/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✔
21
      message = aasm_error.message
11✔
22
      if work.errors.count > 0
11✔
23
        message = work.errors.to_a.join(", ")
6✔
24
      end
25
      message.chop! if message.last == "."
11✔
26
      message
11✔
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✔
38
      no_error = false
144✔
39
      if current_user.blank? || !work.editable_by?(current_user)
144✔
40
        Honeybadger.notify(uneditable_message)
5✔
41
        redirect_to root_path, notice: I18n.t("works.uneditable.privs")
5✔
42
      elsif !work.editable_in_current_state?(current_user)
139✔
43
        Honeybadger.notify(current_state_message)
5✔
44
        redirect_to root_path, notice: I18n.t("works.uneditable.approved")
5✔
45
      else
46
        no_error = true
134✔
47
      end
48

49
      no_error
144✔
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
57
      @params_group_id ||= begin
33✔
58
        group_id = params[:group_id]
33✔
59
        if group_id.blank?
33✔
60
          group_id = current_user.default_group.id
9✔
61
          Honeybadger.notify("We got a nil group as part of the parameters #{params} #{request}")
9✔
62
        end
63
        group_id
33✔
64
      end
65
    end
66

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

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

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

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

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

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

103
      redirect_aasm_error(transition_error_message)
11✔
104
    end
105

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

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✔
121
      stale = false
32✔
122
      last_updated_at = params["last_updated_at"] || "" # when was this record last saved (when the edit form was loaded)
32✔
123
      loaded_at = params["loaded_at"] || "00:00" # when was the edit form loaded
32✔
124
      edit_time = (Time.now.in_time_zone - loaded_at.to_time).to_i
32✔
125
      if work.updated_at.to_s != last_updated_at
32✔
126
        log_message = "Update to work #{work.id} by #{current_user.uid} via will overwrite changes"
30✔
127
        log_message << " (current: #{work.updated_at}, with: #{last_updated_at}, edit time: #{edit_time} seconds)"
30✔
128
        Rails.logger.warn log_message
30✔
129
        Honeybadger.notify log_message
30✔
130
        stale = true
30✔
131
      end
132
      stale
32✔
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