Skip to content

Commit 23c04a2

Browse files
Merge pull request #733 from olleolleolle/patch-2
Ruby syntax: replace and/not with &&/!
2 parents 0838290 + a24f8c0 commit 23c04a2

File tree

9 files changed

+22
-22
lines changed

9 files changed

+22
-22
lines changed

app/controllers/devise_token_auth/concerns/set_user_by_token.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def set_user_by_token(mapping=nil)
4747
end
4848

4949
# user has already been found and authenticated
50-
return @resource if @resource and @resource.class == rc
50+
return @resource if @resource && @resource.class == rc
5151

5252
# ensure we clear the client_id
5353
if !@token
@@ -78,12 +78,12 @@ def set_user_by_token(mapping=nil)
7878

7979
def update_auth_header
8080
# cannot save object if model has invalid params
81-
return unless @resource and @resource.valid? and @client_id
81+
return unless @resource && @resource.valid? && @client_id
8282

8383
# Generate new client_id with existing authentication
8484
@client_id = nil unless @used_auth_by_token
8585

86-
if @used_auth_by_token and not DeviseTokenAuth.change_headers_on_each_request
86+
if @used_auth_by_token && !DeviseTokenAuth.change_headers_on_each_request
8787
# should not append auth header if @resource related token was
8888
# cleared by sign out in the meantime
8989
return if @resource.reload.tokens[@client_id].nil?
@@ -142,9 +142,9 @@ def resource_class(m=nil)
142142

143143

144144
def is_batch_request?(user, client_id)
145-
not params[:unbatch] and
146-
user.tokens[client_id] and
147-
user.tokens[client_id]['updated_at'] and
145+
!params[:unbatch] &&
146+
user.tokens[client_id] &&
147+
user.tokens[client_id]['updated_at'] &&
148148
Time.parse(user.tokens[client_id]['updated_at']) > @request_started_at - DeviseTokenAuth.batch_request_buffer_throttle
149149
end
150150
end

app/controllers/devise_token_auth/confirmations_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class ConfirmationsController < DeviseTokenAuth::ApplicationController
33
def show
44
@resource = resource_class.confirm_by_token(params[:confirmation_token])
55

6-
if @resource and @resource.id
6+
if @resource && @resource.id
77
# create client id
88
client_id = SecureRandom.urlsafe_base64(nil, false)
99
token = SecureRandom.urlsafe_base64(nil, false)

app/controllers/devise_token_auth/passwords_controller.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def edit
7676
reset_password_token: resource_params[:reset_password_token]
7777
})
7878

79-
if @resource and @resource.id
79+
if @resource && @resource.id
8080
client_id = SecureRandom.urlsafe_base64(nil, false)
8181
token = SecureRandom.urlsafe_base64(nil, false)
8282
token_hash = BCrypt::Password.create(token)
@@ -119,7 +119,7 @@ def update
119119
end
120120

121121
# ensure that password params were sent
122-
unless password_resource_params[:password] and password_resource_params[:password_confirmation]
122+
unless password_resource_params[:password] && password_resource_params[:password_confirmation]
123123
return render_update_error_missing_password
124124
end
125125

app/controllers/devise_token_auth/registrations_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def render_destroy_error
186186
def resource_update_method
187187
if DeviseTokenAuth.check_current_password_before_update == :attributes
188188
"update_with_password"
189-
elsif DeviseTokenAuth.check_current_password_before_update == :password and account_update_params.has_key?(:password)
189+
elsif DeviseTokenAuth.check_current_password_before_update == :password && account_update_params.has_key?(:password)
190190
"update_with_password"
191191
elsif account_update_params.has_key?(:current_password)
192192
"update_with_password"

app/controllers/devise_token_auth/sessions_controller.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def create
2929
@resource = resource_class.where(q, q_value).first
3030
end
3131

32-
if @resource and valid_params?(field, q_value) and (!@resource.respond_to?(:active_for_authentication?) or @resource.active_for_authentication?)
32+
if @resource && valid_params?(field, q_value) && (!@resource.respond_to?(:active_for_authentication?) || @resource.active_for_authentication?)
3333
valid_password = @resource.valid_password?(resource_params[:password])
3434
if (@resource.respond_to?(:valid_for_authentication?) && !@resource.valid_for_authentication? { valid_password }) || !valid_password
3535
render_create_error_bad_credentials
@@ -50,7 +50,7 @@ def create
5050
yield @resource if block_given?
5151

5252
render_create_success
53-
elsif @resource and not (!@resource.respond_to?(:active_for_authentication?) or @resource.active_for_authentication?)
53+
elsif @resource && !(!@resource.respond_to?(:active_for_authentication?) || @resource.active_for_authentication?)
5454
render_create_error_not_confirmed
5555
else
5656
render_create_error_bad_credentials
@@ -63,7 +63,7 @@ def destroy
6363
client_id = remove_instance_variable(:@client_id) if @client_id
6464
remove_instance_variable(:@token) if @token
6565

66-
if user and client_id and user.tokens[client_id]
66+
if user && client_id && user.tokens[client_id]
6767
user.tokens.delete(client_id)
6868
user.save!
6969

app/models/devise_token_auth/concerns/user.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ def token_is_current?(token, client_id)
127127

128128
return true if (
129129
# ensure that expiry and token are set
130-
expiry and token and
130+
expiry && token &&
131131

132132
# ensure that the token has not yet expired
133-
DateTime.strptime(expiry.to_s, '%s') > Time.now and
133+
DateTime.strptime(expiry.to_s, '%s') > Time.now &&
134134

135135
# ensure that the token is valid
136136
DeviseTokenAuth::Concerns::User.tokens_match?(token_hash, token)
@@ -147,10 +147,10 @@ def token_can_be_reused?(token, client_id)
147147

148148
return true if (
149149
# ensure that the last token and its creation time exist
150-
updated_at and last_token and
150+
updated_at && last_token &&
151151

152152
# ensure that previous token falls within the batch buffer throttle time of the last request
153-
Time.parse(updated_at) > Time.now - DeviseTokenAuth.batch_request_buffer_throttle and
153+
Time.parse(updated_at) > Time.now - DeviseTokenAuth.batch_request_buffer_throttle &&
154154

155155
# ensure that the token is valid
156156
::BCrypt::Password.new(last_token) == token
@@ -166,7 +166,7 @@ def create_new_auth_token(client_id=nil)
166166
token_hash = ::BCrypt::Password.create(token)
167167
expiry = (Time.now + DeviseTokenAuth.token_lifespan).to_i
168168

169-
if self.tokens[client_id] and self.tokens[client_id]['token']
169+
if self.tokens[client_id] && self.tokens[client_id]['token']
170170
last_token = self.tokens[client_id]['token']
171171
end
172172

@@ -189,7 +189,7 @@ def build_auth_header(token, client_id='default')
189189
expiry = self.tokens[client_id]['expiry'] || self.tokens[client_id][:expiry]
190190

191191
max_clients = DeviseTokenAuth.max_number_of_devices
192-
while self.tokens.keys.length > 0 and max_clients < self.tokens.keys.length
192+
while self.tokens.keys.length > 0 && max_clients < self.tokens.keys.length
193193
oldest_token = self.tokens.min_by { |cid, v| v[:expiry] || v["expiry"] }
194194
self.tokens.delete(oldest_token.first)
195195
end

app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module DeviseTokenAuth::Concerns::UserOmniauthCallbacks
1717

1818
# only validate unique email among users that registered by email
1919
def unique_email_user
20-
if provider == 'email' and self.class.where(provider: 'email', email: email).count > 0
20+
if provider == 'email' && self.class.where(provider: 'email', email: email).count > 0
2121
errors.add(:email, :taken)
2222
end
2323
end

lib/devise_token_auth/rails/routes.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def mount_devise_token_auth_for(resource, opts)
5757
get "#{full_path}/validate_token", controller: "#{token_validations_ctrl}", action: "validate_token"
5858

5959
# omniauth routes. only define if omniauth is installed and not skipped.
60-
if defined?(::OmniAuth) and not opts[:skip].include?(:omniauth_callbacks)
60+
if defined?(::OmniAuth) && !opts[:skip].include?(:omniauth_callbacks)
6161
match "#{full_path}/failure", controller: omniauth_ctrl, action: "omniauth_failure", via: [:get]
6262
match "#{full_path}/:provider/callback", controller: omniauth_ctrl, action: "omniauth_success", via: [:get]
6363

lib/devise_token_auth/url.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def self.generate(url, params = {})
44
uri = URI(url)
55

66
res = "#{uri.scheme}://#{uri.host}"
7-
res += ":#{uri.port}" if (uri.port and uri.port != 80 and uri.port != 443)
7+
res += ":#{uri.port}" if (uri.port && uri.port != 80 && uri.port != 443)
88
res += "#{uri.path}" if uri.path
99
query = [uri.query, params.to_query].reject(&:blank?).join('&')
1010
res += "?#{query}"

0 commit comments

Comments
 (0)