Skip to content

Commit

Permalink
Merge branch 'main' into bootstrap-5
Browse files Browse the repository at this point in the history
  • Loading branch information
werebus committed Oct 23, 2024
2 parents 56ece7a + bd2cf44 commit 5cc3690
Show file tree
Hide file tree
Showing 20 changed files with 73 additions and 66 deletions.
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require:
- rubocop-capybara
- rubocop-factory_bot
- rubocop-performance
- rubocop-rails
- rubocop-rspec
- rubocop-rspec_rails

AllCops:
Exclude:
Expand Down Expand Up @@ -71,7 +74,7 @@ RSpec/ExampleLength:
- hash
- heredoc

RSpec/FactoryBot/ConsistentParenthesesStyle:
FactoryBot/ConsistentParenthesesStyle:
EnforcedStyle: omit_parentheses

RSpec/MultipleMemoizedHelpers:
Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ group :development do
gem 'listen'
gem 'rb-readline', require: false
gem 'rubocop', require: false
gem 'rubocop-capybara', require: false
gem 'rubocop-factory_bot', require: false
gem 'rubocop-performance', require: false
gem 'rubocop-rails', require: false
gem 'rubocop-rspec', require: false
gem 'rubocop-rspec_rails', require: false
end

group :development, :test do
Expand Down
25 changes: 18 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ GEM
debug_inspector (>= 1.2.0)
builder (3.3.0)
byebug (11.1.3)
capistrano (3.14.1)
capistrano (3.19.1)
airbrussh (>= 1.0.0)
i18n
rake (>= 10.0.0)
Expand Down Expand Up @@ -121,10 +121,10 @@ GEM
actionmailer (>= 5.2, < 8)
activesupport (>= 5.2, < 8)
execjs (2.7.0)
factory_bot (6.1.0)
factory_bot (6.5.0)
activesupport (>= 5.0.0)
factory_bot_rails (6.1.0)
factory_bot (~> 6.1.0)
factory_bot_rails (6.4.3)
factory_bot (~> 6.4)
railties (>= 5.0.0)
ffi (1.15.0)
friendly_id (5.5.0)
Expand Down Expand Up @@ -156,7 +156,8 @@ GEM
icalendar (2.7.1)
ice_cube (~> 0.16)
ice_cube (0.16.3)
jbuilder (2.11.2)
jbuilder (2.13.0)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
json (2.7.2)
language_server-protocol (3.17.0.3)
Expand Down Expand Up @@ -301,15 +302,22 @@ GEM
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.32.3)
parser (>= 3.3.1.0)
rubocop-capybara (2.21.0)
rubocop (~> 1.41)
rubocop-factory_bot (2.26.1)
rubocop (~> 1.61)
rubocop-performance (1.15.0)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rails (2.17.2)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)
rubocop-rspec (2.15.0)
rubocop (~> 1.33)
rubocop-rspec (3.1.0)
rubocop (~> 1.61)
rubocop-rspec_rails (2.30.0)
rubocop (~> 1.61)
rubocop-rspec (~> 3, >= 3.0.1)
ruby-progressbar (1.13.0)
ruby_parser (3.15.1)
sexp_processor (~> 4.9)
Expand Down Expand Up @@ -407,9 +415,12 @@ DEPENDENCIES
rspec-html-matchers
rspec-rails
rubocop
rubocop-capybara
rubocop-factory_bot
rubocop-performance
rubocop-rails
rubocop-rspec
rubocop-rspec_rails
sassc-rails
selenium-webdriver
simplecov
Expand Down
18 changes: 8 additions & 10 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

class ApplicationController < ActionController::Base
attr_accessor :current_user

before_action :check_primary_account, :set_current_user, :set_roster, :set_paper_trail_whodunnit

def self.api_accessible(**options)
Expand All @@ -11,7 +9,7 @@ def self.api_accessible(**options)
end

def confirm_change(object, message = nil)
change = object.versions.where(whodunnit: @current_user).last
change = object.versions.where(whodunnit: Current.user).last
flash[:change] = change.try(:id)
# If we know what change occurred, use it to write the message.
# If we don't, try and infer from the current controller action.
Expand Down Expand Up @@ -40,20 +38,20 @@ def report_errors(object, fallback_location:)
# 3. Admins of specifically the current roster

def require_admin
render file: 'public/401.html', status: :unauthorized unless @current_user.admin?
render file: 'public/401.html', status: :unauthorized unless Current.user.admin?
end

def require_admin_in_roster
render file: 'public/401.html', status: :unauthorized unless @current_user.admin_in? @roster
render file: 'public/401.html', status: :unauthorized unless Current.user.admin_in? @roster
end

def set_current_user
if session.key? :user_id
@current_user = User.find_by id: session[:user_id]
Current.user = User.find_by id: session[:user_id]
else
@current_user = User.find_by spire: request.env['fcIdNumber']
if @current_user.present?
session[:user_id] = @current_user.id
Current.user = User.find_by spire: request.env['fcIdNumber']
if Current.user.present?
session[:user_id] = Current.user.id
else
redirect_to unauthenticated_session_path
end
Expand All @@ -66,7 +64,7 @@ def set_current_user
# rubocop:disable Naming/MemoizedInstanceVariableName
def set_roster
@roster = Roster.friendly.find(params[:roster_id], allow_nil: true)
@roster ||= @current_user&.rosters&.first
@roster ||= Current.user&.rosters&.first
@roster ||= Roster.first
end
# rubocop:enable Naming/MemoizedInstanceVariableName
Expand Down
23 changes: 10 additions & 13 deletions app/controllers/assignments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def generate_rotation
end
@roster.generate_assignments(user_ids, start_date,
end_date, start_user).each do |assignment|
assignment.notify :owner, of: :new_assignment, by: @current_user
assignment.notify :owner, of: :new_assignment, by: Current.user
end
flash[:message] = 'Rotation has been generated.'
redirect_to roster_assignments_path(@roster, date: start_date)
Expand Down Expand Up @@ -72,7 +72,7 @@ def create

if assignment.save
confirm_change(assignment)
assignment.notify :owner, of: :new_assignment, by: @current_user
assignment.notify :owner, of: :new_assignment, by: Current.user
redirect_to roster_assignments_path(@roster)
else
report_errors(assignment, fallback_location: roster_assignments_path)
Expand All @@ -99,8 +99,8 @@ def rotation_generator
end

def destroy
if @current_user.admin_in?(@roster)
@assignment.notify :owner, of: :deleted_assignment, by: @current_user
if Current.user.admin_in?(@roster)
@assignment.notify :owner, of: :deleted_assignment, by: Current.user
@assignment.destroy
confirm_change(@assignment)
redirect_to roster_assignments_path(@roster)
Expand Down Expand Up @@ -134,9 +134,7 @@ def set_roster_users
end

def index_html
@assignments = @current_user.assignments.in(@roster)
.upcoming
.order :start_date
@assignments = Current.user.assignments.in(@roster).upcoming.order :start_date
@current_assignment = @roster.assignments.current
@fallback_user = @roster.fallback_user
end
Expand All @@ -153,11 +151,10 @@ def index_json
# and telling the new owner that they're newly responsible now.
def notify_appropriate_users
if @assignment.user == @previous_owner
@assignment.notify :owner, of: :changed_assignment, by: @current_user
@assignment.notify :owner, of: :changed_assignment, by: Current.user
else
@assignment.notify :owner, of: :new_assignment, by: @current_user
@assignment.notify @previous_owner, of: :deleted_assignment,
by: @current_user
@assignment.notify :owner, of: :new_assignment, by: Current.user
@assignment.notify @previous_owner, of: :deleted_assignment, by: Current.user
end
end

Expand All @@ -167,7 +164,7 @@ def render_ics_feed
end

def require_taking_ownership
return true if @current_user.admin_in?(@roster) || taking_ownership?
return true if Current.user.admin_in?(@roster) || taking_ownership?

flash[:errors] = t('.not_an_admin')
redirect_back fallback_location: roster_assignments_path(@roster)
Expand All @@ -176,7 +173,7 @@ def require_taking_ownership

def taking_ownership?
new_user_id = params.require(:assignment).require(:user_id)
new_user_id == @current_user.id.to_s
new_user_id == Current.user&.id.to_s
end

def generate_by_weekday_params
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/changes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class ChangesController < ApplicationController
def undo
version = PaperTrail::Version.find params.require(:id)
original_user = version.whodunnit.to_i == @current_user.id
original_user = version.whodunnit.to_i == Current.user&.id
head :unauthorized and return unless original_user

# Reify only returns false when the thing didn't exist beforehand.
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/rosters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def edit
def create
roster = Roster.new roster_params
# Current user becomes admin in new roster
roster.users << @current_user
roster.users << Current.user
roster.memberships.first.update admin: true
if roster.save
confirm_change(roster)
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def update
membership_params = user_params[:membership]
if @user.update(user_params.except(:membership)) && update_membership(membership_params)
confirm_change(@user)
if @current_user.admin_in? @roster
if Current.user.admin_in? @roster
redirect_to roster_users_path(@roster)
else
redirect_to roster_assignments_path(@roster)
Expand Down Expand Up @@ -75,8 +75,8 @@ def user_params

given_roster_ids = params[:roster_ids].map(&:to_i)
params[:roster_ids] = (@user&.roster_ids || []).then do |roster_ids|
roster_ids.reject! { |roster_id| !roster_id.in?(given_roster_ids) && @current_user.admin_in?(roster_id) }
roster_ids | (given_roster_ids & @current_user.memberships.where(admin: true).map(&:roster_id))
roster_ids.reject! { |roster_id| !roster_id.in?(given_roster_ids) && Current.user.admin_in?(roster_id) }
roster_ids | (given_roster_ids & Current.user.memberships.where(admin: true).map(&:roster_id))
end
end
end
Expand All @@ -86,7 +86,7 @@ def find_user
end

def update_membership(membership_params)
return true unless membership_params.present? && @current_user.admin_in?(@roster)
return true unless membership_params.present? && Current.user.admin_in?(@roster)

membership = @user.membership_in @roster
return true if membership.nil?
Expand All @@ -98,7 +98,7 @@ def update_membership(membership_params)
end

def require_admin_in_roster_or_self
return if @current_user == @user || @current_user.admin_in?(@roster)
return if Current.user == @user || Current.user.admin_in?(@roster)

render file: 'public/401.html', status: :unauthorized
end
Expand Down
5 changes: 5 additions & 0 deletions app/models/current.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class Current < ActiveSupport::CurrentAttributes
attribute :user
end
2 changes: 1 addition & 1 deletion app/views/assignments/_feed_address_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
%i.fas.fa-info-circle
%span#clip-info.sr-only= calendar_instructions
- roster_name = @roster.name.parameterize
- token = @current_user.calendar_access_token
- token = Current.user.calendar_access_token
= text_field_tag :access_token,
feed_url(roster: roster_name, token: token, format: :ics),
readonly: true, class: 'form-control copy-text',
Expand Down
8 changes: 4 additions & 4 deletions app/views/assignments/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
.col-lg
.mb-3
= f.label :user_id, class: 'form-label'
- if @current_user.admin_in?(@roster)
- if Current.user.admin_in?(@roster)
= f.collection_select :user_id, @users, :id, :last_name,
{ selected: @assignment.user&.id || @current_user.id }, class: 'form-select'
{ selected: @assignment.user&.id || Current.user.id }, class: 'form-select'
- else
= f.hidden_field :user_id, value: @current_user.id
= f.text_field :user_name, value: @current_user.last_name, disabled: true, class: 'form-control'
= f.hidden_field :user_id, value: Current.user.id
= f.text_field :user_name, value: Current.user.last_name, disabled: true, class: 'form-control'
.col-lg
.mb-3
= f.label :start_date, class: 'form-label'
Expand Down
2 changes: 1 addition & 1 deletion app/views/assignments/edit.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%h1 Edit assignment
.container-fluid
- if @current_user.admin_in?(@roster)
- if Current.user.admin_in?(@roster)
= button_to 'Delete assignment',
roster_assignment_path(@roster, @assignment),
method: :delete, class: 'btn btn-danger mb-3'
Expand Down
2 changes: 1 addition & 1 deletion app/views/assignments/index.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ json.array! @assignments do |assignment|
json.allDay true
json.start assignment.start_date.to_fs(:iso8601)
json.end 1.day.after(assignment.end_date).to_fs(:iso8601)
json.color("var(--#{assignment.user == @current_user ? 'bs-info' : 'bs-secondary'})")
json.color("var(--#{assignment.user == Current.user ? 'bs-info' : 'bs-secondary'})")
end
10 changes: 5 additions & 5 deletions app/views/layouts/_nav.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
%ul.navbar-nav.me-auto
- rosters.each do |roster|
= nav_link_item roster.name, roster_assignments_path(roster)
- if @current_user&.admin?
- if Current.user&.admin?
= nav_link_item 'Manage Rosters', rosters_path
- if @current_user&.admin_in? @roster
- if Current.user&.admin_in? @roster
= nav_link_item 'Manage Users', roster_users_path(@roster)
%li.nav-item.dropdown
= link_to 'Generate', '#', role: 'button',
Expand All @@ -20,7 +20,7 @@
= link_to 'Rotation', rotation_generator_roster_assignments_path(@roster), class: 'dropdown-item'
= link_to 'By Weekday', generate_by_weekday_roster_assignments_path(@roster), class: 'dropdown-item'
%ul.navbar-nav
- if @current_user.present?
= nav_link_item @current_user.full_name,
edit_roster_user_path(@roster, @current_user)
- if Current.user.present?
= nav_link_item Current.user.full_name,
edit_roster_user_path(@roster, Current.user)
= nav_link_item 'Logout', destroy_session_path
2 changes: 1 addition & 1 deletion app/views/layouts/application.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
= csrf_meta_tags
%body
= render partial: 'layouts/nav',
locals: { rosters: @current_user&.rosters || Roster.none }
locals: { rosters: Current.user&.rosters || Roster.none }
.container
- if flash[:message].present?
.alert.alert-success{ role: 'alert' }
Expand Down
2 changes: 1 addition & 1 deletion app/views/rosters/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
No fallback user!
%td.align-middle= roster.switchover_time.strftime '%-I:%M %P'
%td.align-middle= formatted_phone(roster.phone)
- if @current_user.admin_in? roster
- if Current.user.admin_in? roster
%td.align-middle= link_to 'Setup Twilio Number', setup_roster_path(roster)
%td.align-middle= link_to 'Edit', edit_roster_url(roster)
%td.align-middle= button_to 'Destroy', roster_path(roster), method: :delete, class: 'btn btn-danger'
Expand Down
4 changes: 2 additions & 2 deletions app/views/users/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
= f.hidden_field :'roster_ids[]', value: '', id: nil
.list-group
= f.collection_check_boxes :roster_ids, Roster.all.order(:name), :id, :name, include_hidden: false do |c|
- user_roster_permitted = @current_user.admin_in? c.object
- user_roster_permitted = Current.user.admin_in? c.object
= c.label class: ['m-0', 'list-group-item', user_roster_permitted ? 'list-group-item-action' : nil] do
.form-check
= c.check_box checked: @user.membership_in(c.object).present?,
Expand All @@ -42,7 +42,7 @@
= f.check_box :change_notifications_enabled, class: 'form-check-input'
= f.label :change_notifications_enabled,
'Receive notifications when your assignments are changed?', class: 'form-check-label'
- if @current_user.admin_in? @roster
- if Current.user.admin_in? @roster
- membership = @user.membership_in @roster
= f.fields_for membership || :membership do |g|
.mb-3
Expand Down
Loading

0 comments on commit 5cc3690

Please sign in to comment.