Skip to content

Commit db88871

Browse files
committed
Code style for controllers
1 parent 11183c0 commit db88871

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+296
-230
lines changed

app/controllers/api.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
2+
13
module Api
24
end
3-

app/controllers/api/beta.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Api
24
module Beta
35
end

app/controllers/api/beta/base_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'json'
24

35
class Api::Beta::BaseController < ApplicationController

app/controllers/api/beta/course_id_information_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class Api::Beta::CourseIdInformationController < Api::Beta::BaseController
24
before_action :doorkeeper_authorize!, scopes: [:public]
35

app/controllers/api/beta/participant_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class Api::Beta::ParticipantController < Api::Beta::BaseController
24
before_action :doorkeeper_authorize!, scopes: [:public]
35

app/controllers/api/beta/stats_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class Api::Beta::StatsController < Api::Beta::BaseController
24
before_action :doorkeeper_authorize!, scopes: [:public]
35

app/controllers/api/v8.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Api
24
module V8
35
end

app/controllers/application_controller.rb

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'version'
24
require 'twitter-bootstrap-breadcrumbs'
35

@@ -71,10 +73,10 @@ def check_api_version
7173
return respond_with_error("Please update the TMC client. API version #{ApiVersion::API_VERSION} required but got #{params[:api_version]}", 404, nil, obsolete_client: true)
7274
end
7375

74-
unless params[:client].blank? # Client and client version checks are optional
76+
if params[:client].present? # Client and client version checks are optional
7577
begin
7678
check_client_version(params[:client], params[:client_version])
77-
rescue
79+
rescue StandardError
7880
return respond_with_error($!.message, 404, nil, obsolete_client: true)
7981
end
8082
end
@@ -84,17 +86,17 @@ def check_api_version
8486
def check_client_version(client_name, client_version)
8587
begin
8688
client_version = Version.new(client_version) unless client_version.nil?
87-
rescue
88-
fail "Invalid version string: #{client_version}"
89+
rescue StandardError
90+
raise "Invalid version string: #{client_version}"
8991
end
9092

9193
valid_clients = SiteSetting.value('valid_clients')
9294
if valid_clients.is_a?(Enumerable)
9395
vc = valid_clients.find { |c| c['name'] == client_name }
94-
fail 'Invalid TMC client.' if vc.nil?
96+
raise 'Invalid TMC client.' if vc.nil?
9597

96-
if !client_version.nil? && !vc['min_version'].blank?
97-
fail 'Please update the TMC client.' if client_version < Version.new(vc['min_version'])
98+
if !client_version.nil? && vc['min_version'].present?
99+
raise 'Please update the TMC client.' if client_version < Version.new(vc['min_version'])
98100
else
99101
return # without version check
100102
end
@@ -119,7 +121,7 @@ def set_controller_and_action_name
119121
end
120122

121123
def unauthorized!(message = nil)
122-
raise CanCan::AccessDenied.new(message)
124+
raise CanCan::AccessDenied, message
123125
end
124126

125127
def unauthorize_guest!(message = 'Authentication required')
@@ -140,18 +142,20 @@ def respond_access_denied(msg = 'Access denied')
140142

141143
def respond_with_error(msg, code = 500, exception = nil, extra_json_keys = {})
142144
respond_to do |format|
143-
format.html { render 'shared/respond_with_error',
144-
locals: { message: ERB::Util.html_escape(msg), exception: exception },
145-
layout: true,
146-
status: code }
145+
format.html do
146+
render 'shared/respond_with_error',
147+
locals: { message: ERB::Util.html_escape(msg), exception: exception },
148+
layout: true,
149+
status: code
150+
end
147151
format.json do
148152
if code == 401
149153
# To support older TmcNetBeans versions using faulty http basic auth
150154
if client_supports_http_basic_auth?
151155
response.headers['WWW-Authenticate'] = "Basic realm=\"#{msg}\""
152156
render json: { error: msg }.merge(extra_json_keys), status: code
153157
else
154-
render json: { error: msg }.merge(extra_json_keys), status: 403
158+
render json: { error: msg }.merge(extra_json_keys), status: :forbidden
155159
end
156160
else
157161
render json: { error: msg }.merge(extra_json_keys), status: code
@@ -167,8 +171,8 @@ def client_supports_http_basic_auth?
167171
return true if params[:client].blank?
168172
client = params[:client]
169173
client_version = begin
170-
Version.new(params['client_version']) unless params['client_version'].blank?
171-
rescue
174+
Version.new(params['client_version']) if params['client_version'].present?
175+
rescue StandardError
172176
end
173177
!(client == 'netbeans_plugin' && client_version < Version.new('0.8.0'))
174178
end
@@ -189,7 +193,7 @@ def params_starting_with(prefix, permitted, options = {})
189193
permitted = permitted.map { |f| prefix + f } unless permitted == :all
190194

191195
result = Hash[params.select do |k, v|
192-
k.start_with?(prefix) && !v.blank? && (permitted == :all || permitted.include?(k))
196+
k.start_with?(prefix) && v.present? && (permitted == :all || permitted.include?(k))
193197
end]
194198
if options[:remove_prefix]
195199
result = Hash[result.map { |k, v| [k.sub(/^#{prefix}/, ''), v] }]
@@ -207,7 +211,7 @@ def render_csv(options = {})
207211
headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0'
208212
headers['Expires'] = 'Thu, 01 Dec 1994 16:00:00 GMT'
209213

210-
if request.env['HTTP_USER_AGENT'] =~ /msie/i
214+
if /msie/i.match?(request.env['HTTP_USER_AGENT'])
211215
headers['Pragma'] = 'public'
212216
headers['Content-type'] = 'text/plain'
213217
else

app/controllers/auths_controller.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
# frozen_string_literal: true
2+
13
# Provides an authentication service.
24
#
35
# Lets external services such as tmc-comet check whether a given (username, password) or
46
# (username, session_id) is valid.
57
class AuthsController < ApplicationController
6-
OK_MESSAGE = 'OK'.freeze
7-
FAIL_MESSAGE = 'FAIL'.freeze
8+
OK_MESSAGE = 'OK'
9+
FAIL_MESSAGE = 'FAIL'
810

911
skip_authorization_check
1012
skip_before_action :verify_authenticity_token
1113

1214
def show
1315
if params[:username].present? && params[:session_id].present?
1416
user = User.find_by(login: params[:username])
15-
user = User.find_by('lower(email) = ?', params[:username].downcase) unless user
17+
user ||= User.find_by('lower(email) = ?', params[:username].downcase)
1618
# Allows using oauth2 tokens of the new api for authenticating
1719
res = if user && Doorkeeper::AccessToken.find_by(resource_owner_id: user.id, token: params[:session_id])
1820
OK_MESSAGE

app/controllers/breadcrumb_helpers.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
# frozen_string_literal: true
3+
24
# Helper methods for making breadcrumbs.
35
#
46
# See https://github.com/weppos/breadcrumbs_on_rails
@@ -15,12 +17,12 @@ def add_course_breadcrumb
1517
def add_exercise_breadcrumb
1618
if @exercise
1719
add_breadcrumb "Exercise #{@exercise.name}", exercise_path(@exercise)
18-
elsif @submission && @submission.exercise
20+
elsif @submission&.exercise
1921
add_breadcrumb "Exercise #{@submission.exercise.name}", exercise_path(@submission.exercise)
20-
elsif @submission && @submission.exercise_name
22+
elsif @submission&.exercise_name
2123
add_breadcrumb "(deleted exercise #{@submission.exercise_name}"
2224
else
23-
fail 'Neither @exercise nor @submission.exercise_name set'
25+
raise 'Neither @exercise nor @submission.exercise_name set'
2426
end
2527
end
2628

0 commit comments

Comments
 (0)