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

pulibrary / oawaiver / 40ca2f09-e694-44a5-b909-7cce179b9e67

11 Jul 2024 05:42PM UTC coverage: 0.0% (-80.4%) from 80.383%
40ca2f09-e694-44a5-b909-7cce179b9e67

push

circleci

jrgriffiniii
Trying to expand the RSpec test suite for EmployeesController#ajax_search

0 of 1206 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/app/controllers/waiver_infos_controller.rb
1
# frozen_string_literal: true
2

3
class WaiverInfosController < ApplicationController
×
4
  before_action :authenticate_account!
×
5
  before_action :set_waiver_info, only: %i[show show_mail]
×
6
  before_action :ensure_user_owns_waiver_info, only: %i[show show_mail]
×
7
  respond_to :html
×
8

9
  # GET waivers/search/:search_term
10
  # params :page, :per_page
11
  def solr_search_words
×
12
    args = stripped_args(params, :dont_keep_empties)
×
13
    search_term = args["search_term"] || ""
×
14

15
    waiver_infos = if search_term.length > 1
×
16
                     WaiverInfo.search_with_words(search_term, params[:page], params[:per_page] || WaiverInfo.per_page).results
×
17
                   else
×
18
                     WaiverInfo.all.paginate(page: params[:page], per_page: params[:per_page] || WaiverInfo.per_page)
×
19
                   end
×
20

21
    do_solr_index(search_term, waiver_infos)
×
22
    render(:index)
×
23
  end
×
24

25
  # GET waivers/search/:search_term
26
  # params :page, :per_page
27
  def solr_search_words_post
×
28
    redirect_to action: :solr_search_words, search_term: (params["search_term"] || "").gsub(%r{[.&/]}, " ")
×
29
  end
×
30

31
  # GET waivers
32
  def index
×
33
    models = WaiverInfo.all
×
34

35
    @properties = {}
×
36
    @search_term = ""
×
37
    @waiver_infos = models.paginate(page: params[:page], per_page: params[:per_page])
×
38

39
    render(:index)
×
40
  end
×
41

42
  # GET waiver/requester/me
43
  def index_mine
×
44
    models = if current_account_admin?
×
45
               WaiverInfo.all
×
46
             else
×
47
               WaiverInfo.where(requester: current_account.netid)
×
48
             end
×
49

50
    @properties = current_account_properties
×
51
    @search_term = ""
×
52
    @waiver_infos = models.paginate(page: params[:page], per_page: params[:per_page])
×
53

54
    render(:index)
×
55
  end
×
56

57
  def index_unique_id
×
58
    @properties = unique_id_properties
×
59
    models = search_with_props(@properties)
×
60

61
    @search_term = ""
×
62
    @waiver_infos = models.paginate(page: params[:page], per_page: params[:per_page])
×
63

64
    render(:index)
×
65
  end
×
66

67
  def index_missing_unique_ids
×
68
    logger.debug("index_missing_unique_ids: authenticated as user='#{current_account}'")
×
69
    models = WaiverInfo.find_by_missing_unique_id
×
70

71
    @properties = missing_unique_ids_properties
×
72
    @search_term = ""
×
73
    @waiver_infos = models.paginate(page: params[:page], per_page: params[:per_page])
×
74

75
    render(:index)
×
76
  end
×
77

78
  def search
×
79
    render status: :forbidden unless current_account.admin?
×
80

81
    if params["waiver_info"]
×
82
      @properties = waiver_info_params
×
83
      models = search_with_props(@properties)
×
84

85
      @search_term = ""
×
86
      @waiver_infos = models.paginate(page: params[:page], per_page: params[:per_page])
×
87
    else
×
88
      @waiver_info = WaiverInfo.new
×
89
    end
×
90

91
    render(:index)
×
92
  end
×
93

94
  def new
×
95
    @waiver_info = WaiverInfo.new(author_status: AuthorStatus.status_faculty)
×
96

97
    render(:new_waiver_info)
×
98
  end
×
99

100
  def create
×
101
    redirect_to(root_path) if params["CANCEL"]
×
102

103
    @waiver_info = WaiverInfo.new(waiver_info_params)
×
104
    @waiver_info.errors.clear
×
105
    render(:new_waiver_info) unless @waiver_info.valid?
×
106

107
    redirect_to(root_path) unless params["CONFIRM-WAIVER"]
×
108

109
    @waiver_info.save # save first so ID will be set when generating mail
×
110
    mail = WaiverMailer.new(@waiver_info).granted(@waiver_info.cc_email)
×
111
    mail.deliver!
×
112

113
    mail_record = MailRecord.new_from_mail(mail)
×
114
    mail_record.waiver_info = @waiver_info
×
115
    mail_record.save
×
116

117
    @request_granted = true
×
118
    flash.now[:notice] = "We have sent emails with the waiver to #{mail_record.recipients.join(', ')}"
×
119

120
    render :show
×
121
  rescue StandardError => error
×
122
    @waiver_info.destroy
×
123
    @waiver_info.errors.add(:base, "Could not send an email")
×
124
    @waiver_info.errors.add(:base, error.message)
×
125
    @waiver_info.errors.add(:base, "Did not create the Waiver - Please try again")
×
126

127
    render :new_waiver_info
×
128
  end
×
129

130
  # POST /admin/waiver/:id
131
  def update_by_admin
×
132
    # This should be refactored into an exception (or, CanCanCan should be used)
133
    unless current_account.admin?
×
134
      head(:forbidden)
×
135
      return flash[:alert] = "User account #{current_account} is not an administrator. Please contact an administrator for assistance."
×
136
    end
×
137

138
    @waiver_info = WaiverInfo.find(waiver_id)
×
139

140
    # This handles legacy support for the POST requests
141
    redirect_to(:edit_by_admin) unless params[:commit] && params[:commit] == "Save"
×
142

143
    if @waiver_info.update(update_waiver_info_params)
×
144
      flash[:notice] = "Waiver information successfully updated"
×
145
      redirect_to(@waiver_info)
×
146
    else
×
147
      error_messages = @waiver_info.errors.full_messages.join(". ")
×
148
      flash.now[:alert] = "Waiver information could not be successfully updated: #{error_messages}."
×
149
      redirect_to(:edit_by_admin, id: @waiver_info.id)
×
150
    end
×
151
  end
×
152

153
  # GET /admin/waiver/:id
154
  def edit_by_admin
×
155
    # This should be refactored into an exception (or, CanCanCan should be used)
156
    unless current_account.admin?
×
157
      head(:forbidden)
×
158
      return flash[:alert] = "User account #{current_account} is not an administrator. Please contact an administrator for assistance."
×
159
    end
×
160

161
    @waiver_info = WaiverInfo.find(waiver_id)
×
162

163
    # This handles the POST request, and should be refactored into a new action, #create_by_admin
164
    redirect_to(:update_by_admin) if params[:commit] && params[:commit] == "Save"
×
165

166
    render(:edit_by_admin)
×
167
  end
×
168

169
  private
×
170

171
  def waiver_id
×
172
    params[:id]
×
173
  end
×
174

175
  def current_account_properties
×
176
    {
×
177
      "Requester or Author" => current_account.email
×
178
    }
×
179
  end
×
180

181
  def unique_id_properties
×
182
    {
×
183
      author_unique_id: author_unique_id_param
×
184
    }
×
185
  end
×
186

187
  def missing_unique_ids_properties
×
188
    {
×
189
      missing: "unique_id"
×
190
    }
×
191
  end
×
192

193
  def author_unique_id_param
×
194
    params[:author_unique_id]
×
195
  end
×
196

197
  def do_solr_index(words, waivers)
×
198
    @properties = []
×
199
    @search_term = words
×
200
    @waiver_infos = waivers
×
201
  end
×
202

203
  def search_with_props(search_props)
×
204
    props = {}
×
205

206
    search_props.each do |k, v|
×
207
      props[k] = v.strip
×
208
    end
×
209

210
    title = props.delete("title")
×
211
    journal = props.delete("journal")
×
212
    notes = props.delete("notes")
×
213

214
    @waiver_infos = WaiverInfo.where(props)
×
215
    @author = Employee.find_by_unique_id(props["author_unique_id"]) if props["author_unique_id"]
×
216
    @waiver_infos = @waiver_infos.where("title LIKE ?", "%#{title}%") if title
×
217
    @waiver_infos = @waiver_infos.where("journal LIKE ?", "%#{journal}%") if journal
×
218
    @waiver_infos = @waiver_infos.where("notes LIKE ?", "%#{notes}%") if notes
×
219
    @waiver_infos
×
220
  end
×
221

222
  def account_owns_waiver?
×
223
    return unless current_account
×
224

225
    current_account.authenticated? && @waiver_info.requester == current_account
×
226
  end
×
227

228
  def current_account_admin?
×
229
    return unless current_account
×
230

231
    current_account.admin?
×
232
  end
×
233

234
  def ensure_user_owns_waiver_info
×
235
    # TODO: what about author ?
236
    return if current_account.admin?
×
237

238
    logger.debug "ensure_user_owns_waiver_info #{@waiver_info.id} #{current_account}"
×
239
    render nothing: true, status: :forbidden unless account_owns_waiver?
×
240
  end
×
241

242
  # Use callbacks to share common setup or constraints between actions.
243
  def set_waiver_info
×
244
    @waiver_info = WaiverInfo.find(params[:id])
×
245
  end
×
246

247
  # Never trust parameters from the scary internet, only allow the white list through.
248
  def update_waiver_info_params
×
249
    permitted_waiver_info_params = params.require(:waiver_info)
×
250
    permitted = permitted_waiver_info_params.permit(
×
251
      :author_unique_id,
×
252
      :author_first_name,
×
253
      :author_last_name,
×
254
      :author_status,
×
255
      :author_department,
×
256
      :author_email,
×
257
      :title,
×
258
      :journal,
×
259
      :journal_issn,
×
260
      :notes
×
261
    )
×
262

263
    stripped_args(permitted, :keep_empties)
×
264
  end
×
265

266
  def default_params
×
267
    {
×
268
      "requester" => current_account.netid,
×
269
      "requester_email" => current_account.email
×
270
    }
×
271
  end
×
272

273
  # Never trust parameters from the scary internet, only allow the white list through.
274
  def waiver_info_params
×
275
    return default_params unless params["waiver_info"]
×
276

277
    permitted_waiver_info_params = params.require(:waiver_info)
×
278
    permitted = permitted_waiver_info_params.permit(
×
279
      :requester,
×
280
      :requester_email,
×
281
      :author_unique_id,
×
282
      :author_first_name,
×
283
      :author_last_name,
×
284
      :author_preferred_name,
×
285
      :author_status,
×
286
      :author_department,
×
287
      :author_email,
×
288
      :title,
×
289
      :journal,
×
290
      :journal_issn,
×
291
      :cc_email,
×
292
      :notes,
×
293
      :search_term
×
294
    )
×
295

296
    values = stripped_args(permitted, :dont_keep_empties)
×
297
    default_params.merge(values)
×
298
  end
×
299

300
  # I am not certain that this is needed
301
  def stripped_args(args, mode)
×
302
    hsh = {}
×
303
    keep = mode == :keep_empties
×
304
    args.each do |k, v|
×
305
      v = v.strip
×
306
      hsh[k] = v if keep || v.present?
×
307
    end
×
308
    hsh
×
309
  end
×
310
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