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

pulibrary / pdc_describe / 4e4e59fc-9df4-4838-9fd4-6c7ea33cdb7c

07 Apr 2025 06:36PM UTC coverage: 1.283% (-94.6%) from 95.862%
4e4e59fc-9df4-4838-9fd4-6c7ea33cdb7c

Pull #1994

circleci

hectorcorrea
Switched to use the autocomplete that we aleady use for ROR. Integrated it with the existing logic for creators
Pull Request #1994: Started adding auto complete to contributors

0 of 46 new or added lines in 2 files covered. (0.0%)

4806 existing lines in 74 files now uncovered.

65 of 5065 relevant lines covered (1.28%)

0.01 hits per line

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

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

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

UNCOV
11
  private
×
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
    #
UNCOV
20
    def message_from_aasm_error(aasm_error:, work:)
×
UNCOV
21
      message = aasm_error.message
×
UNCOV
22
      if work.errors.count > 0
×
UNCOV
23
        message = work.errors.to_a.join(", ")
×
UNCOV
24
      end
×
UNCOV
25
      message.chop! if message.last == "."
×
UNCOV
26
      message
×
UNCOV
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
    #
UNCOV
37
    def validate_modification_permissions(work:, uneditable_message:, current_state_message:)
×
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")
×
UNCOV
45
      else
×
UNCOV
46
        no_error = true
×
UNCOV
47
      end
×
48

UNCOV
49
      no_error
×
UNCOV
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
    #
UNCOV
55
    def params_group_id
×
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}")
×
UNCOV
62
        end
×
UNCOV
63
        group_id
×
UNCOV
64
      end
×
UNCOV
65
    end
×
66

67
    # parses the embargo date from the parameters and parses into a Date object
UNCOV
68
    def embargo_date
×
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)
×
UNCOV
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
×
UNCOV
78
    end
×
79

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

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

UNCOV
94
    def rescue_aasm_error
×
UNCOV
95
      yield
×
UNCOV
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)
×
UNCOV
104
    end
×
105

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

UNCOV
111
      redirect_to root_path
×
UNCOV
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.
UNCOV
120
    def check_for_stale_update(work, params)
×
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
×
UNCOV
131
      end
×
UNCOV
132
      stale
×
UNCOV
133
    end
×
UNCOV
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

© 2025 Coveralls, Inc