Skip to content

Commit

Permalink
Make rubocop happy + spec fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SewerusHtd committed Jan 27, 2025
1 parent 3069375 commit 426e721
Show file tree
Hide file tree
Showing 200 changed files with 601 additions and 549 deletions.
12 changes: 6 additions & 6 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ AllCops:
Bundler/OrderedGems:
Enabled: false

RSpec/FactoryBot:
Enabled: false

Layout/DotPosition:
Enabled: false

Expand Down Expand Up @@ -280,6 +277,9 @@ Style/RedundantRegexpEscape:
Style/RedundantSelfAssignment:
Enabled: true

Style/SafeNavigationChainLength:
Max: 5

Style/SingleArgumentDig:
Enabled: true

Expand Down Expand Up @@ -412,13 +412,13 @@ RSpec/VerifiedDoubles:
Exclude:
- 'spec/**/*'

RSpec/FilePath:
Enabled: true

RSpec/AnyInstance:
Exclude:
- 'spec/**/*'

RSpec/IndexedLet:
Enabled: false

RSpec/StubbedMock:
Exclude:
- 'spec/requests/v1/organizations/invitations/invite_organization_admin_spec.rb'
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/resource/position.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ def position_params
end

def response_scope
{ controller_name.to_s => send("#{controller_name}_scope") }
{ controller_name.to_s => send(:"#{controller_name}_scope") }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ class V1::HenryFord::ClinicLocationsController < V1Controller
def index
authorize! :read, Intervention

render json: serialized_hash(ClinicLocation.all.order(:name))
render json: serialized_hash(ClinicLocation.order(:name))
end
end
3 changes: 2 additions & 1 deletion app/controllers/v1/interventions/answers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def csv_attachment

head :no_content unless intervention.reports.attached?

redirect_to(ENV['APP_HOSTNAME'] + Rails.application.routes.url_helpers.rails_blob_path(intervention.newest_report, only_path: true), allow_other_host: true)
redirect_to(ENV.fetch('APP_HOSTNAME', nil) + Rails.application.routes.url_helpers.rails_blob_path(intervention.newest_report, only_path: true),
allow_other_host: true)
end

private
Expand Down
22 changes: 11 additions & 11 deletions app/controllers/v1/interventions/collaborators_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ def index
render json: serialized_response(collaborators_scope)
end

def create
authorize! :manage_collaborators, Intervention

new_collaborators = V1::Intervention::Collaborators::CreateService.call(intervention_load, emails)
def show
authorize! :read, Intervention

render json: serialized_response(new_collaborators), status: :created
render json: serialized_response(collaborators_scope.find_by!(user_id: current_v1_user.id), controller_name.classify, params: { skip_user_data: true })
end

def destroy
def create
authorize! :manage_collaborators, Intervention

V1::Intervention::Collaborators::DestroyService.call(collaborator_load)
new_collaborators = V1::Intervention::Collaborators::CreateService.call(intervention_load, emails)

head :no_content
render json: serialized_response(new_collaborators), status: :created
end

def update
Expand All @@ -31,10 +29,12 @@ def update
render json: serialized_response(collaborator_load)
end

def show
authorize! :read, Intervention
def destroy
authorize! :manage_collaborators, Intervention

render json: serialized_response(collaborators_scope.find_by!(user_id: current_v1_user.id), controller_name.classify, params: { skip_user_data: true })
V1::Intervention::Collaborators::DestroyService.call(collaborator_load)

head :no_content
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class V1::Interventions::PredefinedParticipantsController < V1Controller
before_action :verify_access, except: [:verify]
skip_before_action :authenticate_user!, only: %i[verify]

def show
render json: serialized_response(predefined_participant)
end

def index
render json: serialized_response(predefined_participants)
end

def show
render json: serialized_response(predefined_participant)
end

def create
return head :forbidden unless intervention_load.ability_to_update_for?(current_v1_user)

Expand Down
14 changes: 7 additions & 7 deletions app/controllers/v1/interventions/short_links_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
class V1::Interventions::ShortLinksController < V1Controller
skip_before_action :authenticate_user!, only: %i[verify]

def index
authorize! :index, Intervention
authorize! :index, intervention_load

render json: serialized_short_links_with_clinics
end

def create
authorize! :update, Intervention
authorize! :update, intervention_load
Expand All @@ -12,13 +19,6 @@ def create
render json: serialized_short_links_with_clinics
end

def index
authorize! :index, Intervention
authorize! :index, intervention_load

render json: serialized_short_links_with_clinics
end

def verify
render json: V1::ShortLinks::MapService.call(slug, current_v1_user)
end
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/v1/interventions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ def generated_conversations_transcript

head :no_content unless intervention_load.conversations_transcript.attached?

redirect_to(ENV['APP_HOSTNAME'] + Rails.application.routes.url_helpers.rails_blob_path(intervention_load.conversations_transcript, only_path: true), allow_other_host: true)
redirect_to(ENV.fetch('APP_HOSTNAME', nil) + Rails.application.routes.url_helpers.rails_blob_path(
intervention_load.conversations_transcript, only_path: true
), allow_other_host: true)
end

def clear_user_data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ def assign_file_to_setup!
end

def selected_files(files_for)
setup_load.public_send("#{files_for.singularize}_files")
setup_load.public_send(:"#{files_for.singularize}_files")
end
end
20 changes: 10 additions & 10 deletions app/controllers/v1/live_chat/interventions/links_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
# frozen_string_literal: true

class V1::LiveChat::Interventions::LinksController < V1Controller
def update
authorize! :update, Intervention
authorize! :update, intervention_load
return head :forbidden unless intervention_load.ability_to_update_for?(current_v1_user)

link = link_load
link.update!(link_params)
head :ok
end

def create
authorize! :update, Intervention
authorize! :create, intervention_load
Expand All @@ -22,6 +12,16 @@ def create
status: :created
end

def update
authorize! :update, Intervention
authorize! :update, intervention_load
return head :forbidden unless intervention_load.ability_to_update_for?(current_v1_user)

link = link_load
link.update!(link_params)
head :ok
end

def destroy
authorize! :delete, LiveChat::Interventions::Link
authorize! :update, intervention_load
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/v1/sms_plans/alert_phones/phones_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ def create
render json: serialized_response(phone, 'Phone'), status: :created
end

def destroy
phone_load.destroy!
head :no_content
end

def update
phone = phone_load
phone.update!(phone_params)
render json: serialized_response(phone, 'Phone')
end

def destroy
phone_load.destroy!
head :no_content
end

private

def alert_params
Expand Down
20 changes: 10 additions & 10 deletions app/controllers/v1/user_sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ class V1::UserSessionsController < V1Controller
skip_before_action :authenticate_user!, only: %i[create show_or_create]
before_action :validate_intervention_status

def create
validate_session_status
user_session = V1::UserSessions::CreateService.call(session_id, user_id, health_clinic_id)
authorize! :create, user_session
user_session.save!
@current_v1_user_or_guest_user.update!(quick_exit_enabled: true) if intervention.quick_exit?

render json: serialized_response(user_session), status: :ok
end

def show
validate_session_status
authorize! :read, UserSession
Expand All @@ -26,6 +16,16 @@ def show
user_session.user_intervention.in_progress!
end

def create
validate_session_status
user_session = V1::UserSessions::CreateService.call(session_id, user_id, health_clinic_id)
authorize! :create, user_session
user_session.save!
@current_v1_user_or_guest_user.update!(quick_exit_enabled: true) if intervention.quick_exit?

render json: serialized_response(user_session), status: :ok
end

def show_or_create
validate_session_status
user_session = V1::UserSessions::FetchOrCreateService.call(session_id, user_id, health_clinic_id)
Expand Down
23 changes: 12 additions & 11 deletions app/controllers/v1/users/invitations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ def index
render json: serialized_response(researchers_not_accepted_invitations, 'User', { only_email: true })
end

# This endpoint will be hit from mailer link, thus it needs to be public
def edit
user = User.where.not(invitation_token: nil).find_by_invitation_token(invitation_token, true)

# Unfortunetly find_by_invitation_token method doesn't raise exception when there is no user
# and there is no version with !
return redirect_to_web_app(error: I18n.t('users.invite.not_active')) if user.blank?

redirect_to "#{ENV.fetch('WEB_URL', nil)}/register?invitation_token=#{invitation_token}&email=#{user.email}&role=#{user.roles.first}",
allow_other_host: true
end

def create
authorize! :create, User

Expand Down Expand Up @@ -34,17 +46,6 @@ def resend
end
end

# This endpoint will be hit from mailer link, thus it needs to be public
def edit
user = User.where.not(invitation_token: nil).find_by_invitation_token(invitation_token, true) # rubocop:disable Rails/DynamicFindBy

# Unfortunetly find_by_invitation_token method doesn't raise exception when there is no user
# and there is no version with !
return redirect_to_web_app(error: I18n.t('users.invite.not_active')) if user.blank?

redirect_to "#{ENV['WEB_URL']}/register?invitation_token=#{invitation_token}&email=#{user.email}&role=#{user.roles.first}", allow_other_host: true
end

# This endpoint is hit from registration page to register new user from invitation
# link, thus there is no need for authorization
def update
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def terms_confirmation_params
end

def user_params
if (current_v1_user.roles & %w[researcher team_admin]).present? && current_v1_user.id != user_id
if current_v1_user.roles.intersect?(%w[researcher team_admin]) && current_v1_user.id != user_id
params.require(:user).permit(
:active
)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/v1_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ def invalidate_cache(obj)
def redirect_to_web_app(message)
message.transform_values! { |v| Base64.encode64(v) }

redirect_to "#{ENV['WEB_URL']}?#{message.to_query}", allow_other_host: true
redirect_to "#{ENV.fetch('WEB_URL', nil)}?#{message.to_query}", allow_other_host: true
end
end
2 changes: 1 addition & 1 deletion app/jobs/interventions/re_publish_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def perform(intervention_id)
private

def send_scheduled_invitation_from_past(user_sessions, intervention)
user_sessions.where(scheduled_at: intervention.paused_at..DateTime.now).each do |user_session|
user_sessions.where(scheduled_at: intervention.paused_at..DateTime.now).find_each do |user_session|
user_session.session.send_link_to_session(user_session.user, user_session.health_clinic)
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/live_chat/generate_transcript_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def perform(obj_id, model_class, file_attach_point, intervention_name, user_id)

return unless user.email_notification

LiveChat::TranscriptMailer.send("#{object_name.downcase}_transcript", user.email, object).deliver_now
LiveChat::TranscriptMailer.send(:"#{object_name.downcase}_transcript", user.email, object).deliver_now
end

private
Expand Down
13 changes: 7 additions & 6 deletions app/jobs/user_session_jobs/send_question_sms_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ def perform(user_id, question_id, user_session_id, reminder, postponed = false)
outdated_message = false

if user.pending_sms_answer && question.type == 'Question::Sms'
datetime_of_next_job = DateTime.current.in_time_zone(ENV['CSV_TIMESTAMP_TIME_ZONE']) + 5.minutes
datetime_of_next_job = DateTime.current.in_time_zone(ENV.fetch('CSV_TIMESTAMP_TIME_ZONE', nil)) + 5.minutes

# Skip question if next day
if datetime_of_next_job < DateTime.current.in_time_zone(ENV['CSV_TIMESTAMP_TIME_ZONE']).end_of_day
if datetime_of_next_job < DateTime.current.in_time_zone(ENV.fetch('CSV_TIMESTAMP_TIME_ZONE', nil)).end_of_day
UserSessionJobs::SendQuestionSmsJob.set(wait_until: datetime_of_next_job).perform_later(user_id, question_id, user_session_id, false, true)
else
outdated_message = true
Expand Down Expand Up @@ -65,15 +65,16 @@ def schedule_question_followups(user, question, user_session)
# Get proper configuration
every_number_of_hours = question.sms_reminders['per_hours'].to_i
for_number_of_days = question.sms_reminders['number_of_days'].to_i
from = ActiveSupport::TimeZone[ENV['CSV_TIMESTAMP_TIME_ZONE']].parse(question.sms_reminders['from'] || '')
to = ActiveSupport::TimeZone[ENV['CSV_TIMESTAMP_TIME_ZONE']].parse(question.sms_reminders['to'] || '')
from = ActiveSupport::TimeZone[ENV.fetch('CSV_TIMESTAMP_TIME_ZONE', nil)].parse(question.sms_reminders['from'] || '')
to = ActiveSupport::TimeZone[ENV.fetch('CSV_TIMESTAMP_TIME_ZONE', nil)].parse(question.sms_reminders['to'] || '')

return unless every_number_of_hours && for_number_of_days && from && to

# Prepare all vars for calculation of all reminders
reminders_datetimes = [DateTime.current.in_time_zone(ENV['CSV_TIMESTAMP_TIME_ZONE'])]
reminders_datetimes = [DateTime.current.in_time_zone(ENV.fetch('CSV_TIMESTAMP_TIME_ZONE', nil))]
calculated_datetime = reminders_datetimes.last
last_possible_reminder = ActiveSupport::TimeZone[ENV['CSV_TIMESTAMP_TIME_ZONE']].parse(question.sms_reminders['to']) + (for_number_of_days - 1).days
last_possible_reminder = ActiveSupport::TimeZone[ENV.fetch('CSV_TIMESTAMP_TIME_ZONE',
nil)].parse(question.sms_reminders['to']) + (for_number_of_days - 1).days

# Calculate all possible datetimes
while calculated_datetime + every_number_of_hours.hour < last_possible_reminder
Expand Down
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class ApplicationMailer < ActionMailer::Base
around_action :with_locale, if: -> { params && params[:locale].present? }

default from: ENV['EMAIL_DEFAULT_FROM']
default from: ENV.fetch('EMAIL_DEFAULT_FROM', nil)
layout 'mailer'

protected
Expand Down
6 changes: 3 additions & 3 deletions app/models/audio/text_to_speech/google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ def client
def credentials
@credentials ||= begin
if Rails.env.development?
Oj.load_file(ENV['GOOGLE_APPLICATION_CREDENTIALS'])
Oj.load_file(ENV.fetch('GOOGLE_APPLICATION_CREDENTIALS', nil))
else
Oj.load(ENV['GOOGLE_APPLICATION_CREDENTIALS'])
Oj.load(ENV.fetch('GOOGLE_APPLICATION_CREDENTIALS', nil))
end
rescue Oj::ParseError
ENV['GOOGLE_APPLICATION_CREDENTIALS']
ENV.fetch('GOOGLE_APPLICATION_CREDENTIALS', nil)
end
end

Expand Down
Loading

0 comments on commit 426e721

Please sign in to comment.