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

pulibrary / pdc_describe / 1aaf6302-d8cf-4943-bb96-5e86951c32a3

pending completion
1aaf6302-d8cf-4943-bb96-5e86951c32a3

Pull #1079

circleci

Bess Sadler
Nil safe doi gsub
Pull Request #1079: Nil safe collection title

2 of 2 new or added lines in 1 file covered. (100.0%)

1777 of 2063 relevant lines covered (86.14%)

100.37 hits per line

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

53.7
/app/controllers/users_controller.rb
1
# frozen_string_literal: true
2
class UsersController < ApplicationController
1✔
3
  # Constants set by the <form> <input> parameters transmitted using POST/PATCH/PUT requests
4
  COLLECTION_MESSAGING_DISABLED = "0"
1✔
5
  COLLECTION_MESSAGING_ENABLED = "1"
1✔
6

7
  before_action :set_user, only: %i[show edit update]
1✔
8

9
  def index
1✔
10
    @users = User.all
×
11
  end
12

13
  # GET /users/1
14
  def show
1✔
15
    @search_terms = params["q"].presence
42✔
16
    @can_edit = can_edit?
42✔
17
    @my_dashboard = current_user.id == @user.id
42✔
18
    @unfinished_works = WorkList.unfinished_works(@user, @search_terms)
42✔
19
    @completed_works = WorkList.completed_works(@user, @search_terms)
42✔
20
    @withdrawn_works = WorkList.withdrawn_works(@user, @search_terms)
42✔
21
    @works_found = @unfinished_works.length + @completed_works.length + @withdrawn_works.length
42✔
22
  end
23

24
  # GET /users/1/edit
25
  def edit
1✔
26
    unless can_edit?
×
27
      Rails.logger.warn("Unauthorized to edit user #{@user.id} (current user: #{current_user.id})")
×
28
      redirect_to user_path(@user)
×
29
    end
30
  end
31

32
  # PATCH/PUT /users/1 or /users/1.json
33
  def update
1✔
34
    if can_edit?
×
35
      respond_to do |format|
×
36
        update_collections_with_messaging if user_params.key?(:collections_with_messaging)
×
37

38
        if @user.update(user_params)
×
39
          format.html { redirect_to user_url(@user), notice: "User was successfully updated." }
×
40
          format.json { render :show, status: :ok, location: @user }
×
41
        else
42
          format.html { render :edit, status: :unprocessable_entity }
×
43
          format.json { render json: @user.errors, status: :unprocessable_entity }
×
44
        end
45
      end
46
    else
47
      Rails.logger.warn("Unauthorized to update user #{@user.id} (current user: #{current_user.id})")
×
48
      redirect_to user_path(@user)
×
49
    end
50
  end
51

52
  private
1✔
53

54
    # Use callbacks to share common setup or constraints between actions.
55
    def set_user
1✔
56
      user_id = user_id_from_url
42✔
57
      @user = User.friendly.find(user_id)
42✔
58
      redirect_to action: action_name, id: @user.friendly_id, status: :moved_permanently unless @user.friendly_id == user_id
42✔
59
    end
60

61
    def user_id_from_url
1✔
62
      # For external users UID is in the form `user-name@gmail.com`, however, Rails eats the ".com" from
63
      # the UID and dumps it into the `format` param. Here we make sure the ".com" is preserved when the
64
      # UID looks to be an external user id.
65
      external_uid = params[:id].include?("@")
42✔
66
      if external_uid && params["format"] == "com"
42✔
67
        "#{params[:id]}.#{params['format']}"
×
68
      else
69
        params[:id]
42✔
70
      end
71
    end
72

73
    # Only allow a list of trusted parameters through.
74
    def user_params
1✔
75
      @user_params ||= params.require(:user).permit([:display_name, :full_name, :family_name, :orcid, :email_messages_enabled, collections_with_messaging: {}])
×
76
    end
77

78
    def can_edit?
1✔
79
      current_user.id == @user.id or current_user.super_admin?
42✔
80
    end
81

82
    def parameter_enables_messaging?(form_value)
1✔
83
      form_value.to_s == COLLECTION_MESSAGING_ENABLED
×
84
    end
85

86
    def update_collections_with_messaging
1✔
87
      if user_params.key?(:collections_with_messaging)
×
88
        extracted = user_params.extract!(:collections_with_messaging)
×
89
        collections_with_messaging = extracted[:collections_with_messaging]
×
90

91
        collections_with_messaging.each_pair do |collection_id, param|
×
92
          selected_collection = Collection.find_by(id: collection_id)
×
93

94
          if parameter_enables_messaging?(param)
×
95
            @user.enable_messages_from(collection: selected_collection)
×
96
          else
97
            @user.disable_messages_from(collection: selected_collection)
×
98
          end
99
        end
100
      end
101
    end
102
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