Skip to content

Commit

Permalink
Get Rubocop working again
Browse files Browse the repository at this point in the history
  • Loading branch information
julianguyen committed Oct 11, 2023
1 parent 8faf334 commit 77c6e09
Show file tree
Hide file tree
Showing 86 changed files with 244 additions and 291 deletions.
6 changes: 4 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require:
- rubocop-rails
- rubocop-performance
- rubocop-rspec

AllCops:
TargetRubyVersion: 3.1
NewCops: enable
TargetRubyVersion: 3.1.4
Exclude:
- 'bin/**/*'
- 'db/**/*'
Expand Down Expand Up @@ -72,7 +74,7 @@ Metrics/ModuleLength:
Enabled: false

Layout/LineLength:
IgnoredPatterns: ['\A#']
AllowedPatterns: ['\A#']

Metrics/ClassLength:
Enabled: false
42 changes: 20 additions & 22 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,28 @@ end

desc 'Automate the Config Setup for New Environments'
task setup_workspace: :environment do
begin
SECRETS = {
SECRET_KEY_BASE: generate_secret,
DEVISE_SECRET_KEY: generate_secret
}.freeze

%w[development test].each do |environment|
example = Rails.root.join('config', 'env', "#{environment}.example.env")
target = Rails.root.join('config', 'env', "#{environment}.env")
FileUtils.cp(example, target)

# insert the secrets into the file
content = File.read(target)
%w[SECRET_KEY_BASE DEVISE_SECRET_KEY].each do |key|
content.sub!(%(#{key}=""), %(#{key}="#{SECRETS[key.to_sym]}"))
end
File.write(target, content)
SECRETS = {
SECRET_KEY_BASE: generate_secret,
DEVISE_SECRET_KEY: generate_secret
}.freeze

%w[development test].each do |environment|
example = Rails.root.join('config', 'env', "#{environment}.example.env")
target = Rails.root.join('config', 'env', "#{environment}.env")
FileUtils.cp(example, target)

# insert the secrets into the file
content = File.read(target)
%w[SECRET_KEY_BASE DEVISE_SECRET_KEY].each do |key|
content.sub!(%(#{key}=""), %(#{key}="#{SECRETS[key.to_sym]}"))
end

puts "Workspace setup completed successfully 🎉"
rescue StandardError => e
puts "Exception Occurred #{e.class}. Message: #{e.message}. Backtrace: \n #{e.backtrace.join("\n")}"
Rails.logger.error "Exception Occurred #{e.class}. Message: #{e.message}. Backtrace: \n #{e.backtrace.join("\n")}"
File.write(target, content)
end

puts 'Workspace setup completed successfully 🎉'
rescue StandardError => e
puts "Exception Occurred #{e.class}. Message: #{e.message}. Backtrace: \n #{e.backtrace.join("\n")}"
Rails.logger.error "Exception Occurred #{e.class}. Message: #{e.message}. Backtrace: \n #{e.backtrace.join("\n")}"
end

# Run the secret task, grab the output, strip the new lines
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/allies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ def index

def add
AllyshipCreator.perform(ally_id: params[:ally_id],
current_user: current_user)
current_user:)
redirect_to_path(allies_path)
end

def remove
user_id = current_user.id
ally_id = params[:ally_id].to_i
Allyship.where(user_id: user_id, ally_id: ally_id).destroy_all
Allyship.where(user_id:, ally_id:).destroy_all
redirect_to_path(allies_path)
end
end
8 changes: 3 additions & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class ApplicationController < ActionController::Base

private

def with_timezone
def with_timezone(&)
timezone = Time.find_zone(cookies[:timezone])
Time.use_zone(timezone) { yield }
Time.use_zone(timezone, &)
end

def set_locale
Expand Down Expand Up @@ -64,9 +64,7 @@ def configure_for_accept_invitation
end

def after_sign_in_path_for(resource)
if resource.respond_to?(:pwned?) && resource.pwned?
cookies[:pwned] = { value: true, expires: Time.current + 10.minutes }
end
cookies[:pwned] = { value: true, expires: 10.minutes.from_now } if resource.respond_to?(:pwned?) && resource.pwned?

super
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def delete

def remove_meeting_notification!(comment_id)
CommentNotificationsService.remove(
comment_id: comment_id,
comment_id:,
model_name: 'Meeting'
)
end
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/concerns/moments_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ def saving_as_draft?

def empty_array_for(*symbols)
symbols.each do |symbol|
if moment_params[symbol].nil? && @moment.has_attribute?(symbol)
@moment[symbol] = []
end
@moment[symbol] = [] if moment_params[symbol].nil? && @moment.has_attribute?(symbol)
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/concerns/pages_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def modify_resources
item['locations']&.map! { |location| t("#{pr}.locations.#{location}") }
item['tags'].sort_by!(&:downcase)
item['languages'].sort_by!(&:downcase)
item['locations']&.sort_by! { |location| location.downcase }
item['locations']&.sort_by!(&:downcase)
end
resources
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def format_success(format, model_object, is_update)
path = show_path(model_object)
format.html { redirect_to path }
status = is_update ? :ok : :created
format.json { render :show, status: status, location: model_object }
format.json { render :show, status:, location: model_object }
end

def format_failure(format, model_object, is_update)
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/concerns/strategies_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ def render_errors(strategy)

def empty_array_for(*symbols)
symbols.each do |symbol|
if strategy_params[symbol].nil? && @strategy.has_attribute?(symbol)
@strategy[symbol] = []
end
@strategy[symbol] = [] if strategy_params[symbol].nil? && @strategy.has_attribute?(symbol)
end
end

Expand Down
12 changes: 6 additions & 6 deletions app/controllers/groups/memberships_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ def create
GroupNotifier.new(@group, 'new_group_member', current_user)
.send_notifications_to(@group.leaders)

flash[:notice] = t('groups.join.success')
flash.now[:notice] = t('groups.join.success')
redirect_to_group
rescue
rescue StandardError
flash[:alert] = t('groups.join.group_not_exists')
redirect_to groups_path
end
Expand All @@ -25,10 +25,10 @@ def destroy
@group_member = find_group_member(my_id)
# Cannot leave When you are the only leader
if @group.leader_ids == [my_id]
flash[:alert] = t('groups.leave.error')
flash.now[:alert] = t('groups.leave.error')
else
@group_member.destroy
flash[:notice] = notice_destroy(my_id)
flash.now[:notice] = notice_destroy(my_id)
end
redirect_to groups_path
end
Expand All @@ -41,9 +41,9 @@ def kick
# Cannot kick if you are not a leader (hacker prevention)
if @group.leader_ids.include?(current_user.id)
@group_member.destroy
flash[:notice] = notice_destroy(member_id)
flash.now[:notice] = notice_destroy(member_id)
else
flash[:alert] = t('groups.leave.remove_member_error')
flash.now[:alert] = t('groups.leave.remove_member_error')
end
redirect_to groups_path
end
Expand Down
6 changes: 2 additions & 4 deletions app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def index
# GET /groups/1
# GET /groups/1.json
def show
if @group.members.include? current_user
@meetings = @group.meetings.includes(:leaders)
end
@meetings = @group.meetings.includes(:leaders) if @group.members.include? current_user

@page_new = t('meetings.new') if @group.led_by?(current_user)
end
Expand All @@ -35,7 +33,7 @@ def new
def edit
return if @group.leaders.include?(current_user)

flash[:error] = t('groups.form.error_edit_permission')
flash.now[:error] = t('groups.form.error_edit_permission')
redirect_to_path(groups_path)
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/medications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def add_refill(medication)
dosage = @medication&.dosage || medication[:dosage]
no_of_days = total.to_i / dosage.to_i
no_of_weeks = no_of_days.to_i / weekly_dosage.length
days = 7 * no_of_weeks + (no_of_days % weekly_dosage.length)
days = (7 * no_of_weeks) + (no_of_days % weekly_dosage.length)
Time.zone.now.strftime('%d/%m/%Y').to_date + days - 1
end

Expand Down
8 changes: 4 additions & 4 deletions app/controllers/meetings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ def meeting_params

def send_notification(meeting, members, type)
MeetingNotificationsService.handle_members(
current_user: current_user,
meeting: meeting,
type: type,
members: members
current_user:,
meeting:,
type:,
members:
)
end

Expand Down
6 changes: 2 additions & 4 deletions app/controllers/moments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def show
show_with_comments(@moment)
resources_data = get_resources_data(@moment, current_user)
@resources = ResourceRecommendations.new(
moment: @moment, current_user: current_user
moment: @moment, current_user:
).call
@show_crisis_prevention = resources_data[:show_crisis_prevention]
@resources_tags = resources_data[:tags]
Expand All @@ -41,9 +41,7 @@ def new

# GET /moments/1/edit
def edit
unless @moment.user_id == current_user.id
redirect_to_path(moment_path(@moment))
end
redirect_to_path(moment_path(@moment)) unless @moment.user_id == current_user.id
set_association_variables!
end

Expand Down
6 changes: 3 additions & 3 deletions app/controllers/profile_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def remove_ban
private

def update_and_email(user, banned)
user.update(banned: banned)
user.update(banned:)
if banned
BannedMailer.add_ban_email(user).deliver_now
else
Expand All @@ -53,8 +53,8 @@ def ban(banned)

def notice_or_alert(user, notice)
name = user&.name || params[:user_id]
return { notice: t("reports.#{notice}", name: name) } if user.present?
return { notice: t("reports.#{notice}", name:) } if user.present?

{ alert: t("reports.#{notice}_error", name: name) }
{ alert: t("reports.#{notice}_error", name:) }
end
end
2 changes: 1 addition & 1 deletion app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def notice_or_alert(report)
def valid_report?(user)
current_user.ally?(user) &&
(params[:comment_id].blank? ||
Comment.where(id: params[:comment_id]).exists?)
Comment.exists?(id: params[:comment_id]))
end

def create_report
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def posts
private

def search_by_email(email)
User.where(email: email)
User.where(email:)
.where.not(id: current_user.id)
.where.not(banned: true)
end
Expand Down
12 changes: 6 additions & 6 deletions app/controllers/secret_shares_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
class SecretSharesController < ApplicationController
skip_before_action :if_not_signed_in, only: [:show]

def show
@moment = Moment.find_secret_share!(params[:id])
@page_author = User.find(@moment.user_id)
render 'moments/show'
end

def create
# Authorisation check will automatically raise ActiveRecord::RecordNotFound
moment = Moment.where(user: current_user).friendly.find(params[:moment])
Expand All @@ -13,12 +19,6 @@ def create
redirect_to moment_path(moment, anchor: 'secretShare')
end

def show
@moment = Moment.find_secret_share!(params[:id])
@page_author = User.find(@moment.user_id)
render 'moments/show'
end

def destroy
moment = Moment.find_by!(user: current_user, id: params[:id])
moment.update!(secret_share_identifier: nil)
Expand Down
8 changes: 0 additions & 8 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@ class SessionsController < Devise::SessionsController
before_action :set_recaptcha, only: [:new]
prepend_before_action :invalidate_all_sessions, only: [:destroy]

def new
super
end

def create
super
set_user_locale if user_signed_in?
end

def destroy
super
end

private

def set_user_locale
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/strategies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ def new

# GET /strategies/1/edit
def edit
unless @strategy.user_id == current_user.id
redirect_to_path(strategy_path(@strategy))
end
redirect_to_path(strategy_path(@strategy)) unless @strategy.user_id == current_user.id
@viewers = current_user.allies_by_status(:accepted)
@categories = current_user.categories.is_visible
.or(Category.where(id: @strategy.category_ids))
Expand All @@ -75,7 +73,7 @@ def premade
strategy = premade_strategy
respond_to do |format|
if strategy.save
PerformStrategyReminder.create!(strategy: strategy, active: false)
PerformStrategyReminder.create!(strategy:, active: false)
format.json { head :no_content }
else
format.json { render_errors(strategy) }
Expand Down Expand Up @@ -124,7 +122,7 @@ def set_strategy
end

def quick_create_params(viewers)
{ user_id: current_user.id, comment: true, viewers: viewers, visible: true,
{ user_id: current_user.id, comment: true, viewers:, visible: true,
description: params[:strategy][:description], published_at: Time.zone.now,
category: params[:strategy][:category], name: params[:strategy][:name] }
end
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/users/invitations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ def update
AllyshipCreator.perform(ally_id: resource.id,
current_user: resource.invited_by)
if resource.class.allow_insecure_sign_in_after_accept
# rubocop:disable Layout/LineLength
flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
# rubocop:enable Layout/LineLength
set_flash_message :notice, flash_message if is_flashing_format?
sign_in(resource_name, resource)
respond_with resource, location: after_accept_path_for(resource)
Expand Down
Loading

0 comments on commit 77c6e09

Please sign in to comment.