1class ApplicationController < ActionController::Base
 
2  protect_from_forgery with: :exception
 
3
 
4  before_action :reload_site
 
5  before_action :reload_parliament
 
6  before_action :service_unavailable, unless: :site_enabled?
 
7  before_action :authenticate, if: :site_protected?
 
8  before_action :redirect_to_url_without_format, if: :unknown_format?
 
 9
 
10  before_action :set_seen_cookie_message, if: :show_cookie_message?
 
11  helper_method :show_cookie_message?, :public_petition_facets
 
 
13  hide_action :admin_request?
 
  • Complexity 1 » saikuro
15  def admin_request?
 
16    false
 
17  end
 
 
19  protected
 
  • Complexity 2 » saikuro
21  def authenticate
 
22    authenticate_or_request_with_http_basic(Site.name) do |username, password|
 
23      Site.authenticate(username, password)
 
24    end
 
25  end
 
  • Complexity 1 » saikuro
27  def csv_request?
 
28    request.format.symbol == :csv
 
29  end
 
  • Complexity 1 » saikuro
31  def json_request?
 
32    request.format.symbol == :json
 
33  end
 
  • NilCheck - performs a nil-check » reek
  • Complexity 1 » saikuro
35  def unknown_format?
 
36    request.format.nil?
 
37  end
 
  • DuplicateMethodCall - calls 'request.path' 2 times » reek
  • UncommunicativeVariableName - has the variable name 'e' » reek
  • Complexity 3 » saikuro
39  def url_without_format
 
40    URI.parse(request.original_url).tap do |uri|
 
41      uri.path = File.join(File.dirname(request.path), File.basename(request.path, '.*'))
 
42    end.to_s
 
43  rescue URI::InvalidURIError => e
 
44    home_url
 
45  end
 
  • Complexity 1 » saikuro
47  def redirect_to_url_without_format
 
48    redirect_to url_without_format
 
49  end
 
  • UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
  • Complexity 1 » saikuro
51  def reload_site
 
52    Site.reload
 
53  end
 
  • UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
  • Complexity 1 » saikuro
55  def reload_parliament
 
56    Parliament.reload
 
57  end
 
  • Complexity 1 » saikuro
59  def service_unavailable
 
60    raise Site::ServiceUnavailable, "Sorry, the website is temporarily unavailable"
 
61  end
 
  • UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
  • Complexity 1 » saikuro
63  def site_enabled?
 
64    Site.enabled?
 
65  end
 
  • Complexity 2 » saikuro
67  def site_protected?
 
68    Site.protected? unless request.local?
 
69  end
 
  • UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
  • Complexity 1 » saikuro
71  def parliament_dissolved?
 
72    Parliament.dissolved?
 
73  end
 
  • Complexity 1 » saikuro
75  def redirect_to_home_page
 
76    redirect_to home_url
 
77  end
 
  • Complexity 1 » saikuro
79  def set_seen_cookie_message
 
80    cookies[:seen_cookie_message] = { value: 'yes', expires: 1.year.from_now, httponly: true }
 
81  end
 
  • Complexity 1 » saikuro
83  def set_cors_headers
 
84    headers['Access-Control-Allow-Origin'] = '*'
 
85    headers['Access-Control-Allow-Methods'] = 'GET'
 
86    headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept'
 
87  end
 
  • Complexity 1 » saikuro
89  def show_cookie_message?
 
90    @show_cookie_message ||= cookies[:seen_cookie_message] != 'yes'
 
91  end
 
  • UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
  • Complexity 1 » saikuro
93  def public_petition_facets
 
94    I18n.t('public', scope: :"petitions.facets")
 
95  end
 
  • Complexity 1 » saikuro
97  def do_not_cache
 
98    response.headers['Cache-Control'] = 'no-store, no-cache'
 
 99  end
 
  • UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
  • Complexity 1 » saikuro
101  def current_time
 
102    Time.current.getutc.iso8601
 
103  end
 
104end