Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rubocop fixes #19

Merged
merged 3 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
source "https://rubygems.org"
# frozen_string_literal: true

source 'https://rubygems.org'

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.2.1"
gem 'rails', '~> 7.2.1'
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"
gem 'sprockets-rails'
# Use sqlite3 as the database for Active Record
# Use the Puma web server [https://github.com/puma/puma]
gem "puma", ">= 5.0"
gem 'puma', '>= 5.0'
# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem "importmap-rails"
gem 'importmap-rails'
# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"
gem 'turbo-rails'
# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"
gem 'stimulus-rails'
# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder"
gem 'jbuilder'
# Use Redis adapter to run Action Cable in production
# gem "redis", ">= 4.0.1"

Expand All @@ -25,10 +27,10 @@ gem "jbuilder"
# gem "bcrypt", "~> 3.1.7"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ windows jruby ]
gem 'tzinfo-data', platforms: %i[windows jruby]

# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false
gem 'bootsnap', require: false

# OmniAuth for Google login
gem 'omniauth'
Expand All @@ -40,31 +42,30 @@ gem 'omniauth-rails_csrf_protection'

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri windows ], require: "debug/prelude"
gem 'debug', platforms: %i[mri windows], require: 'debug/prelude'

# Static analysis for security vulnerabilities [https://brakemanscanner.org/]
gem "brakeman", require: false
gem 'brakeman', require: false

# Omakase Ruby styling [https://github.com/rails/rubocop-rails-omakase/]
gem "rubocop-rails-omakase", require: false
gem 'rubocop-rails-omakase', require: false

gem "sqlite3", "~> 1.4"
gem 'sqlite3', '~> 1.4'
end

group :development do
# Use console on exceptions pages [https://github.com/rails/web-console]
gem "web-console"
gem 'web-console'
end

group :test do
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
gem "capybara"
gem "selenium-webdriver"
gem 'capybara'
gem 'cucumber-rails', require: false
gem 'database_cleaner'
gem 'rails-controller-testing'
gem 'rspec-rails'
gem 'selenium-webdriver'
gem 'simplecov', require: false
gem 'ZenTest'
end

4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require_relative "config/application"
require_relative 'config/application'

Rails.application.load_tasks
34 changes: 20 additions & 14 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
# frozen_string_literal: true

# ApplicationController serves as the base controller for all other controllers in the application.
# It enforces user authentication for all actions unless overridden, and supports browser version
# restrictions for modern features like webp images, web push, badges, etc.
class ApplicationController < ActionController::Base
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
allow_browser versions: :modern
# Require users to be logged in
before_action :require_login

private
def current_user
# if @current _user is undefined or falsy, evaluate the RHS
# Look up user by id if user id is in the session hash
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end

def logged_in?
current_user
end
def current_user
# if @current _user is undefined or falsy, evaluate the RHS
# Look up user by id if user id is in the session hash
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end

def logged_in?
current_user
end

def require_login
# redirect to the welcome page unless user is logged in
return if logged_in?

def require_login
# redirect to the welcome page unless user is logged in
unless logged_in?
redirect_to welcome_path, alert: 'You must be logged in to access this section.'
end
end
redirect_to welcome_path, alert: 'You must be logged in to access this section.'
end
end
31 changes: 20 additions & 11 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
# frozen_string_literal: true

# SessionsController handles user authentication with OmniAuth for Google OAuth2.
class SessionsController < ApplicationController
# Don't require login for login page
skip_before_action :require_login, only: [:omniauth]

# GET /logout
def logout
reset_session
redirect_to welcome_path, notice: 'You are logged out'
end

# GET /auth/google_oauth2/callback
def omniauth
auth = request.env['omniauth.auth']
@user = User.find_or_create_by(uid: auth['uid'], provider: auth['provider']) do |u|
u.email = auth['info']['email']
names = auth['info']['name'].split
u.first_name = names[0]
u.last_name = names[1..].join(' ')
end

@user = find_or_create_user_from_auth(auth_info)

if @user.valid?
session[:user_id] = @user.id
redirect_to user_path(@user), notice: 'You are logged in'
else
redirect_to welcome_path, alert: 'Login failed'
end
end

private

def auth_info
request.env['omniauth.auth']
end

def find_or_create_user_from_auth(auth)
User.find_or_create_by(uid: auth['uid'], provider: auth['provider']) do |user|
user.email = auth['info']['email']
names = auth['info']['name'].split
user.first_name = names[0]
user.last_name = names[1..].join(' ')
end
end
end
4 changes: 4 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# frozen_string_literal: true

# UsersController handles displaying user-related data.
# The show action retrieves and displays the current user based on the ID provided in the parameters.
class UsersController < ApplicationController
def show
@current_user = User.find(params[:id])
Expand Down
11 changes: 8 additions & 3 deletions app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# frozen_string_literal: true

# WelcomeController handles displaying the public welcome page.
# It skips the login requirement for the index action and redirects logged-in users
# to their user page with a personalized welcome message.
class WelcomeController < ApplicationController
# Don't require login for the public welcome page
skip_before_action :require_login, only: [:index]
def index
if logged_in?
redirect_to user_path(@current_user), notice: "Welcome back, #{@current_user.first_name}!"
end
return unless logged_in?

redirect_to user_path(@current_user), notice: "Welcome back, #{@current_user.first_name}!"
end
end
3 changes: 3 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# frozen_string_literal: true

# ApplicationHelper is used to store helper methods that can be used across views.
module ApplicationHelper
end
3 changes: 3 additions & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# frozen_string_literal: true

# ApplicationRecord serves as the base class for all models in the application.
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
end
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# frozen_string_literal: true

# The User model represents users in the application.
class User < ApplicationRecord
validates :email, presence: true
end
10 changes: 6 additions & 4 deletions bin/brakeman
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
# frozen_string_literal: true

ARGV.unshift("--ensure-latest")
require 'rubygems'
require 'bundler/setup'

load Gem.bin_path("brakeman", "brakeman")
ARGV.unshift('--ensure-latest')

load Gem.bin_path('brakeman', 'brakeman')
76 changes: 43 additions & 33 deletions bin/bundle
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,62 @@
# this file is here to facilitate running it.
#

require "rubygems"
require 'rubygems'

m = Module.new do
module_function

def invoked_as_script?
File.expand_path($0) == File.expand_path(__FILE__)
File.expand_path($PROGRAM_NAME) == File.expand_path(__FILE__)
end

def env_var_version
ENV["BUNDLER_VERSION"]
ENV['BUNDLER_VERSION']
end

def cli_arg_version
return unless invoked_as_script? # don't want to hijack other binstubs
return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update`
return unless invoked_as_script? && 'update'.start_with?(ARGV.first || ' ')

extract_bundler_version
end

def extract_bundler_version
bundler_version = nil
update_index = nil
ARGV.each_with_index do |a, i|
if update_index && update_index.succ == i && a.match?(Gem::Version::ANCHORED_VERSION_PATTERN)
bundler_version = a

ARGV.each_with_index do |arg, index|
if update_index && update_index.succ == index && arg.match?(Gem::Version::ANCHORED_VERSION_PATTERN)
bundler_version = arg
end

if arg =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
bundler_version = Regexp.last_match(1)
update_index = index
end
next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
bundler_version = $1
update_index = i
end

bundler_version
end

def gemfile
gemfile = ENV["BUNDLE_GEMFILE"]
return gemfile if gemfile && !gemfile.empty?

File.expand_path("../Gemfile", __dir__)
gemfile = ENV['BUNDLE_GEMFILE']
gemfile && !gemfile.empty? ? gemfile : File.expand_path('../Gemfile', __dir__)
end

def lockfile
lockfile =
case File.basename(gemfile)
when "gems.rb" then gemfile.sub(/\.rb$/, ".locked")
else "#{gemfile}.lock"
end
lockfile = case File.basename(gemfile)
when 'gems.rb' then gemfile.sub(/\.rb$/, '.locked')
else "#{gemfile}.lock"
end
File.expand_path(lockfile)
end

def lockfile_version
return unless File.file?(lockfile)

lockfile_contents = File.read(lockfile)
return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/

Regexp.last_match(1)
end

Expand All @@ -71,29 +78,34 @@ m = Module.new do
return "#{Gem::Requirement.default}.a" unless version

bundler_gem_version = Gem::Version.new(version)

bundler_gem_version.approximate_recommendation
end

def load_bundler!
ENV["BUNDLE_GEMFILE"] ||= gemfile

ENV['BUNDLE_GEMFILE'] ||= gemfile
activate_bundler
end

def activate_bundler
gem_error = activation_error_handling do
gem "bundler", bundler_requirement
end
gem_error = activation_error_handling { gem 'bundler', bundler_requirement }
return if gem_error.nil?
require_error = activation_error_handling do
require "bundler/version"

require_error = activation_error_handling { require 'bundler/version' }
if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?(Gem::Version.new(Bundler::VERSION))
return
end
return if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?(Gem::Version.new(Bundler::VERSION))
warn "Activating bundler (#{bundler_requirement}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_requirement}'`"

warn_bundler_activation_error(gem_error)
exit 42
end

def warn_bundler_activation_error(gem_error)
warn "Activating bundler (#{bundler_requirement}) failed:\n" \
"#{gem_error.message}\n\n" \
'To install the version of bundler this project requires, run ' \
"`gem install bundler -v '#{bundler_requirement}'`"
end

def activation_error_handling
yield
nil
Expand All @@ -104,6 +116,4 @@ end

m.load_bundler!

if m.invoked_as_script?
load Gem.bin_path("bundler", "bundle")
end
load Gem.bin_path('bundler', 'bundle') if m.invoked_as_script?
5 changes: 3 additions & 2 deletions bin/importmap
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative "../config/application"
require "importmap/commands"
require_relative '../config/application'
require 'importmap/commands'
Loading