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

benwbrum / fromthepage / 13528664866

25 Feb 2025 06:48PM UTC coverage: 59.626% (-2.2%) from 61.822%
13528664866

Pull #4471

github

web-flow
Merge 279229e31 into 3b7156fe3
Pull Request #4471: Search test

1548 of 3188 branches covered (48.56%)

Branch coverage included in aggregate %.

84 of 457 new or added lines in 18 files covered. (18.38%)

1 existing line in 1 file now uncovered.

7065 of 11257 relevant lines covered (62.76%)

78.7 hits per line

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

81.74
/app/controllers/application_controller.rb
1
class ApplicationController < ActionController::Base
1✔
2
  protect_from_forgery with: :exception, except: [:switch_locale, :saml]
1✔
3

4
  before_action do
1✔
5
    if current_user && current_user.admin
1,585✔
6
      Rack::MiniProfiler.authorize_request
209✔
7
    end
8
  end
9

10
  before_action :load_objects_from_params
1✔
11
  before_action :store_current_location, :unless => :devise_controller?
1✔
12
  before_action :load_html_blocks
1✔
13
  before_action :authorize_collection
1✔
14
  before_action :configure_permitted_parameters, if: :devise_controller?
1✔
15
  skip_before_action :verify_authenticity_token, if: (:devise_controller? && :codespaces_environment?)
1✔
16
  before_action :set_current_user_in_model
1✔
17
  before_action :masquerade_user!
1✔
18
  before_action :check_search_attempt
1✔
19
  after_action :track_action
1✔
20
  around_action :switch_locale
1✔
21

22
  layout :set_layout
1✔
23

24

25
  def switch_locale(&action)
1✔
26
    @dropdown_locales = I18n.available_locales.reject { |locale| locale.to_s.include? "-" }
12,408✔
27

28
    locale = nil
1,551✔
29

30
    # use user-record locale
31
    if user_signed_in? && !current_user.preferred_locale.blank?
1,551✔
32
      # the user has set their locale manually; use it.
×
33
      locale = current_user.preferred_locale
×
34
    end
35

36
    # if we can't find that, use session locale
37
    if locale.nil?
1,551!
38
      if session[:current_locale]
1,551!
39
        locale = session[:current_locale]
×
40
      end
41
    end
42

43
    # if we can't find that, use browser locale
44
    if locale.nil?
1,551!
45
      # the user might their locale set in the browser
1,551✔
46
      locale = http_accept_language.preferred_language_from(I18n.available_locales)
1,551✔
47
    end
48

49
    if locale.nil? || !I18n.available_locales.include?(locale.to_sym)
1,551✔
50
      # use the default if the above optiosn didn't work
1,205✔
51
      locale = I18n.default_locale
1,205✔
52
    end
53

54
    # append region to locale
55
    related_locales = http_accept_language.user_preferred_languages.select do |loc|
1,551✔
56
      loc.to_s.include?(locale.to_s) &&                              # is related to the chosen locale (is the locale, or is a regional version of it)
692✔
57
      I18n.available_locales.map{|e| e.to_s}.include?(loc.to_s) # is an available locale
4,844✔
58
    end
59

60
    unless related_locales.empty?
1,551✔
61
      # first preferred language from the related locales
346✔
62
      locale = http_accept_language.preferred_language_from(related_locales)
346✔
63
    end
64

65
    # execute the action with the locale
66
    I18n.with_locale(locale, &action)
1,551✔
67
  end
68

69
  # Set the current user in User
70
  def set_current_user_in_model
1✔
71
    User.current_user = current_user
1,582✔
72
  end
73

74
  def current_user
1✔
75
    super || guest_user
32,079✔
76
  end
77

78
  # find the guest user account if a guest user session is currently active
79
  def guest_user
1✔
80
    User.find_by(id: session[:guest_user_id])
1,636✔
81
  end
82

83
  #when the user chooses to transcribe as guest, find guest user id or create new guest user
84
  def guest_transcription
1✔
85

86
    return head(:forbidden) unless GUEST_TRANSCRIPTION_ENABLED
3✔
87

88
    if check_recaptcha(model: @page, :attribute => :errors)
2✔
89
      User.find(session[:guest_user_id].nil? ? session[:guest_user_id] = create_guest_user.id : session[:guest_user_id])
2!
90
      redirect_to :controller => 'transcribe', :action => 'display_page', :page_id => @page.id
2✔
91
    else
92
      # TODO: Get some kind of flash notification on failure
×
93
      flash[:error] = t('layouts.application.recaptcha_validation_failed')
×
94
      flash.keep
×
95
      redirect_to :controller => 'transcribe', :action => 'guest', :page_id => @page.id
×
96
    end
97

98
  end
99

100
  def create_guest_user
1✔
101
    user = User.new { |user| user.guest = true}
4✔
102
    user.email = "guest_#{Time.now.to_i}#{rand(99)}@example.com"
2✔
103
    user.save(:validate => false)
2✔
104
    user
2✔
105
  end
106

107
  def remove_col_id
1✔
108
    #if there's a col_id set, needs to be removed to prevent breadcrumb issues
109
    if session[:col_id]
264✔
110
      session[:col_id] = nil
9✔
111
    end
112
  end
113

114
  # See ActionController::RequestForgeryProtection for details
115
  # Uncomment the :secret if you're not using the cookie session store
116
  # protect_from_forgery :secret => 'I Hate InvalidAuthenticityToken'
117
  rescue_from ActiveRecord::RecordNotFound do |e|
1✔
118
    bad_record_id(e)
1✔
119
  end
120

121
  def load_objects_from_params
1✔
122
    # this needs to be ordered from the specific to the
123
    # general, so that parent_id will load the appropriate
124
    # object without being overridden by child_id.parent
125
    # whenever both are specified on the parameters
126

127
    if params[:article_id]
1,562✔
128
      @article = Article.find(params[:article_id])
55✔
129
      if session[:col_id] != nil
55✔
130
        @collection = set_friendly_collection(session[:col_id])
16✔
131
        session[:col_id] = nil
16✔
132
      else
39✔
133
        @collection = @article.collection
39✔
134
      end
135
    end
136
    if params[:page_id]
1,562✔
137
      @page = Page.find(params[:page_id])
362✔
138
      @work = @page.work
362✔
139
      if session[:col_id] != nil
362✔
140
        @collection = set_friendly_collection(session[:col_id])
130✔
141
        session[:col_id] = nil
130✔
142
      else
232✔
143
        @collection = @page.collection
232✔
144
      end
145
    end
146
    if params[:work_id]
1,562✔
147
      @work = Work.friendly.find(params[:work_id])
385✔
148
      @collection = @work.collection
385✔
149
    end
150
    if params[:document_set_id]
1,562✔
151
      @document_set = DocumentSet.friendly.find(params[:document_set_id])
13✔
152
      @collection = @document_set.collection
13✔
153
    end
154
    if params[:collection_id]
1,562✔
155
      @collection = set_friendly_collection(params[:collection_id])
746✔
156
    end
157

158
    if params[:user_id]
1,562✔
159
      @user = User.friendly.find(params[:user_id])
71✔
160
    end
161

162
    # category stuff may be orthogonal to collections and articles
163
    if params[:category_id]
1,561✔
164
      @category = Category.find(params[:category_id])
6✔
165
    end
166

167
    # consider loading work and collection from the versions
168
    if params[:page_version_id]
1,561!
169
      @page_version = PageVersion.find(params[:page_version_id])
×
170
      @page = @page_version.page
×
171
      @work = @page.work
×
172
      @collection = @work.collection
×
173
    end
174
    if params[:article_version_id]
1,561!
175
      @article_version = ArticleVersion.find(params[:article_version_id])
×
176
      @article = @article_version.article
×
177
      @collection = @article.collection
×
178
    end
179
    if params[:collection_ids]
1,561!
180
      @collection_ids = params[:collection_ids]
×
181
    end
182

183

184
    if self.class.module_parent == Thredded && @collection
1,561✔
185
      Thredded::Engine.routes.default_url_options = { user_slug: @collection.owner.slug, collection_id: @collection.slug }
1✔
186
    else
1,560✔
187
      Thredded::Engine.routes.default_url_options = { user_slug: 'nil', collection_id: 'nil' }
1,560✔
188
    end
189

190
  end
191

192

193
  def set_friendly_collection(id)
1✔
194
    if Collection.friendly.exists?(id)
892✔
195
      @collection = Collection.friendly.find(id)
777✔
196
    elsif DocumentSet.friendly.exists?(id)
115✔
197
      @collection = DocumentSet.friendly.find(id)
114✔
198
    elsif !DocumentSet.find_by(slug: id).nil?
1!
199
      @collection = DocumentSet.find_by(slug: id)
✔
200
    elsif !Collection.find_by(slug: id).nil?
1!
201
      @collection = Collection.find_by(slug: id)
×
202
    end
203

204
    # check to make sure URLs haven't gotten scrambled
205
    if @work
892✔
206
      if @work.collection != @collection
564✔
207
        # this could be a document set or a bad collection
65✔
208
        unless @collection.is_a? DocumentSet
65!
209
          @collection = @work.collection
×
210
        end
211
      end
212
    end
213
    return @collection
892✔
214
  end
215

216
  def bad_record_id(e)
1✔
217
    logger.error("Bad record ID exception for params=#{params.inspect}")
1✔
218
    logger.error(e.backtrace[2])
1✔
219
    if @collection
1!
220
      redirect_to :controller => 'collection', :action => 'show', :collection_id => @collection.id
×
221
    else
1✔
222
      redirect_to "/404"
1✔
223
    end
224

225
    return
226
  end
227

228
  def load_html_blocks
1✔
229
    @html_blocks = {}
1,553✔
230
    page_blocks =
231
      PageBlock.where(controller: controller_name, view: action_name)
1,553✔
232
    page_blocks.each do |b|
1,553✔
233
        if b && b.html
797✔
234
          b.rendered_html = render_to_string(:inline => b.html)
74✔
235
        else
723✔
236
          b.rendered_html = ''
723✔
237
        end
238
        @html_blocks[b.tag] = b
797✔
239
    end
240
  end
241

242
  def authorize_collection
1✔
243
    return unless @collection
1,584✔
244
    if self.class.module_parent.name == 'Thredded'
853✔
245
      unless @collection.messageboards_enabled
1!
246
        flash[:error] = t('message_boards_are_disabled', :project => @collection.title)
×
247
        redirect_to main_app.user_profile_path(@collection.owner)
×
248
      end
249
    end
250

251
    return unless @collection.restricted
853✔
252
    return if (params[:controller] == 'iiif')
48!
253

254
    unless @collection.show_to?(current_user)
48✔
255
      # second chance?
2✔
256
      unless set_fallback_collection
2!
257
        flash[:error] = t('unauthorized_collection', :project => @collection.title)
2✔
258
        redirect_to main_app.user_profile_path(@collection.owner)
2✔
259
      end
260
    end
261
  end
262

263
  def set_fallback_collection
1✔
264
    if @work && @work.collection.supports_document_sets
2✔
265
      alternative_set = @work.document_sets.unrestricted.first
2✔
266
      if alternative_set
2!
267
        @collection = alternative_set
×
268
        true
×
269
      else
2✔
270
        false
2✔
271
      end
272
    else
×
273
      false
×
274
    end
275
  end
276

277

278
  def configure_permitted_parameters
1✔
279
    devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:login, :email, :password, :password_confirmation, :display_name, :owner, :paid_date, :activity_email) }
69✔
280
    devise_parameter_sanitizer.permit(:sign_in) { |u| u.permit(:login_id, :login, :email, :password, :remember_me) }
90✔
281
    devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:login, :email, :password, :current_password, :password_confirmation, :real_name) }
69✔
282
  end
283

284
  # Redirect to admin or owner dashboard after sign in
285
    # Always send admins to admin dashboard
286
    # Everyone else should go back to where they came from if their previous page is set
287
    # Otherwise owners should go to their dashboards
288
    # And everyone else should go to user dashboard/watchlist
289
  def after_sign_in_path_for(resource)
1✔
290
    if current_user.admin
17✔
291
      admin_path
4✔
292
    elsif !session[:user_return_to].blank? && session[:user_return_to] != '/' && !session[:user_return_to].include?('/landing')
13✔
293
      session[:user_return_to]
2✔
294
    elsif current_user.owner
11✔
295
      if current_user.collections.any?
4✔
296
        dashboard_owner_path
2✔
297
      else
2✔
298
        dashboard_startproject_path
2✔
299
      end
300
    else
7✔
301
      dashboard_watchlist_path
7✔
302
    end
303
  end
304

305
  # destroy guest user session if a user signs out, then redirect to root path
306
  def after_sign_out_path_for(resource)
1✔
307
    if session[:guest_user_id]
×
308
      session[:guest_user_id] = nil
×
309
    end
310
    root_path
×
311
  end
312

313
  # Wrapper around redirect_to for modal ajax forms
314
  def ajax_redirect_to(options={}, response_status={})
1✔
315
    if request.xhr?
52✔
316
      head :created, location: url_for(options)
8✔
317
    else
44✔
318
      redirect_to options, response_status
44✔
319
    end
320
  end
321

322
  private
1✔
323

324
  def set_layout
1✔
325
    request.xhr? ? false : nil
2,259✔
326
  end
327
end
328

329
  def page_params(page)
1✔
330
    if @collection
642✔
331
      collection = @collection
642✔
332
    else
×
NEW
333
      collection = page.work.access_object
×
334
    end
335

336
    if page.status_new?
642✔
337
      if user_signed_in?
315✔
338
        collection_transcribe_page_path(page.work.collection.owner, collection, page.work, page)
300✔
339
      else
15✔
340
        collection_guest_page_path(page.work.collection.owner, collection, page.work, page)
15✔
341
      end
342
    else
327✔
343
      collection_display_page_path(page.work.collection.owner, collection, page.work, page)
327✔
344
    end
345
  end
346

347

348
  def track_action
1✔
349
    extras = {}
1,562✔
350
    if @collection
1,562✔
351
      if @collection.is_a? DocumentSet
1,084✔
352
        extras[:document_set_id] = @collection.id
116✔
353
        extras[:document_set_title] = @collection.title
116✔
354
        extras[:collection_id] = @collection.collection.id
116✔
355
        extras[:collection_title] = @collection.collection.title
116✔
356
      else
968✔
357
        extras[:collection_id] = @collection.id
968✔
358
        extras[:collection_title] = @collection.title
968✔
359
      end
360
    end
361
    extras[:work_id] = @work.id if @work
1,562✔
362
    extras[:work_title] = @work.title if @work
1,562✔
363
    extras[:page_id] = @page.id if @page
1,562✔
364
    extras[:page_title] = @page.title if @page
1,562✔
365
    extras[:article_id] = @article.id if @article
1,562✔
366
    extras[:article_title] = @article.title if @article
1,562✔
367
    ahoy.track("#{controller_name}##{action_name}", extras) unless action_name == "still_editing"
1,562✔
368
  end
369

370

371
  def check_api_access
1✔
372
    if (defined? @collection) && @collection
4✔
373
      if @collection.restricted && !@collection.api_access
2!
374
        if @api_user.nil? || !(@api_user.like_owner?(@collection))
×
375
          render :status => 403, :plain => 'This collection is private.  The collection owner must enable API access to it or make it public for it to appear.'
×
376
        end
377
      end
378
    end
379
  end
380

381
  def set_api_user
1✔
382
    authenticate_with_http_token do |token, options|
16✔
383
      @api_user = User.find_by(api_key: token)
12✔
384
    end
385
  end
386

387
  def check_search_attempt
1✔
388
    if session[:search_attempt_id]
1,582!
389
      your_profile = controller_name == "user" && @user == current_user
×
390
      if ["dashboard", "static"].include?(controller_name) || your_profile
×
391
        session[:search_attempt_id] = nil
×
392
      end
393
    end
394
  end
395

396
  def update_search_attempt_contributions
1✔
397
    if session[:search_attempt_id]
66!
398
      search_attempt = SearchAttempt.find(session[:search_attempt_id])
×
399
      search_attempt.increment!(:contributions)
×
400
    end
401
  end
402

403
  def update_search_attempt_user(user, session_var)
1✔
404
    if session_var[:search_attempt_id]
450!
405
      search_attempt = SearchAttempt.find(session_var[:search_attempt_id])
×
406
      search_attempt.user = user
×
407
      search_attempt.owner = user.owner
×
408
      search_attempt.save
×
409
    end
410
  end
411

412
private
1✔
413
  def store_current_location
1✔
414
    store_location_for(:user, request.url)
1,229✔
415
  end
416
  def check_recaptcha(options)
1✔
417
    return verify_recaptcha(options) if RECAPTCHA_ENABLED
2!
418
    true
2✔
419
  end
420
  def codespaces_environment?
1✔
421
    Rails.env.development? && ENV["CODESPACES"] == "true"
712✔
422
  end
423
# class ApplicationController < ActionController::Base
424
#   protect_from_forgery
425
# 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