diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..ee6f28f --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,37 @@ +# The behavior of RuboCop can be controlled via the .rubocop.yml +# configuration file. It makes it possible to enable/disable +# certain cops (checks) and to alter their behavior if they accept +# any parameters. The file can be placed either in your home +# directory or in some project directory. +# +# RuboCop will start looking for the configuration file in the directory +# where the inspected file is and continue its way up to the root directory. +# +# See https://docs.rubocop.org/rubocop/configuration + +plugins: + - rubocop-rails + +AllCops: + NewCops: enable + +Style/StringLiterals: + Exclude: + - Gemfile # Mostly because of auto-generated files + +Bundler/OrderedGems: + Enabled: false + +Style/Documentation: + Enabled: false + +Rails/ActionOrder: + Enabled: false + +Metrics/AbcSize: + Exclude: + - db/migrate/*.rb + +Metrics/MethodLength: + Exclude: + - db/migrate/*.rb diff --git a/Gemfile b/Gemfile index 8b083d2..911e1b7 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source "https://rubygems.org" # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" @@ -21,7 +23,7 @@ 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] # Use the database-backed adapters for Rails.cache, Active Job, and Action Cable gem "solid_cache" @@ -41,7 +43,7 @@ gem 'slim' 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 @@ -50,4 +52,7 @@ end group :development do # Use console on exceptions pages [https://github.com/rails/web-console] gem "web-console" + + gem 'rubocop', require: false + gem 'rubocop-rails', require: false end diff --git a/Gemfile.lock b/Gemfile.lock index 9e6f93b..7bb4195 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -72,6 +72,7 @@ GEM securerandom (>= 0.3) tzinfo (~> 2.0, >= 2.0.5) uri (>= 0.13.1) + ast (2.4.3) base64 (0.3.0) bcrypt (3.1.20) benchmark (0.4.1) @@ -124,6 +125,9 @@ GEM jbuilder (2.13.0) actionview (>= 5.0.0) activesupport (>= 5.0.0) + json (2.12.2) + language_server-protocol (3.17.0.5) + lint_roller (1.1.0) logger (1.7.0) loofah (2.24.1) crass (~> 1.0.2) @@ -166,9 +170,14 @@ GEM racc (~> 1.4) nokogiri (1.18.8-x86_64-linux-musl) racc (~> 1.4) + parallel (1.27.0) + parser (3.3.8.0) + ast (~> 2.4.1) + racc pp (0.6.2) prettyprint prettyprint (0.2.0) + prism (1.4.0) propshaft (1.1.0) actionpack (>= 7.0.0) activesupport (>= 7.0.0) @@ -218,12 +227,35 @@ GEM rake (>= 12.2) thor (~> 1.0, >= 1.2.2) zeitwerk (~> 2.6) + rainbow (3.1.1) rake (13.3.0) rdoc (6.14.1) erb psych (>= 4.0.0) + regexp_parser (2.10.0) reline (0.6.1) io-console (~> 0.5) + rubocop (1.78.0) + json (~> 2.3) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.1.0) + parallel (~> 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 2.9.3, < 3.0) + rubocop-ast (>= 1.45.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.45.1) + parser (>= 3.3.7.2) + prism (~> 1.4) + rubocop-rails (2.32.0) + activesupport (>= 4.2.0) + lint_roller (~> 1.1) + rack (>= 1.1) + rubocop (>= 1.75.0, < 2.0) + rubocop-ast (>= 1.44.0, < 2.0) + ruby-progressbar (1.13.0) ruby-vips (2.2.4) ffi (~> 1.12) logger @@ -265,6 +297,9 @@ GEM railties (>= 7.1.0) tzinfo (2.0.6) concurrent-ruby (~> 1.0) + unicode-display_width (3.1.4) + unicode-emoji (~> 4.0, >= 4.0.4) + unicode-emoji (4.0.4) uri (1.0.3) useragent (0.16.11) web-console (4.2.1) @@ -300,6 +335,8 @@ DEPENDENCIES propshaft puma (>= 5.0) rails (~> 8.0.2) + rubocop + rubocop-rails slim solid_cable solid_cache diff --git a/app/controllers/passwords_controller.rb b/app/controllers/passwords_controller.rb index 0c4b4a8..fe697eb 100644 --- a/app/controllers/passwords_controller.rb +++ b/app/controllers/passwords_controller.rb @@ -1,16 +1,17 @@ +# frozen_string_literal: true + class PasswordsController < ApplicationController allow_unauthenticated_access - before_action :set_user_by_token, only: %i[ edit update ] + before_action :set_user_by_token, only: %i[edit update] - def new - end + def new; end def create if user = User.find_by(email_address: params[:email_address]) PasswordsMailer.reset(user).deliver_later end - redirect_to new_session_path, notice: "Password reset instructions sent (if user with that email address exists)." + redirect_to new_session_path, notice: 'Password reset instructions sent (if user with that email address exists).' end def edit @@ -18,16 +19,17 @@ class PasswordsController < ApplicationController def update if @user.update(params.permit(:password, :password_confirmation)) - redirect_to new_session_path, notice: "Password has been reset." + redirect_to new_session_path, notice: 'Password has been reset.' else - redirect_to edit_password_path(params[:token]), alert: "Passwords did not match." + redirect_to edit_password_path(params[:token]), alert: 'Passwords did not match.' end end private - def set_user_by_token - @user = User.find_by_password_reset_token!(params[:token]) - rescue ActiveSupport::MessageVerifier::InvalidSignature - redirect_to new_password_path, alert: "Password reset link is invalid or has expired." - end + + def set_user_by_token + @user = User.find_by_password_reset_token!(params[:token]) + rescue ActiveSupport::MessageVerifier::InvalidSignature + redirect_to new_password_path, alert: 'Password reset link is invalid or has expired.' + end end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 8be156c..25219ed 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class ProjectsController < ApplicationController before_action :fetch_project!, only: %w[show edit update destroy] @@ -5,8 +7,7 @@ class ProjectsController < ApplicationController @projects = Project.all end - def show - end + def show; end def new @project = Project.new @@ -21,8 +22,7 @@ class ProjectsController < ApplicationController end end - def edit - end + def edit; end def update if @project.update(project_params) @@ -32,8 +32,7 @@ class ProjectsController < ApplicationController end end - def destroy - end + def destroy; end private diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 9785c92..19ad437 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,16 +1,19 @@ -class SessionsController < ApplicationController - allow_unauthenticated_access only: %i[ new create ] - rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_url, alert: "Try again later." } +# frozen_string_literal: true - def new - end +class SessionsController < ApplicationController + allow_unauthenticated_access only: %i[new create] + rate_limit to: 10, within: 3.minutes, only: :create, with: lambda { + redirect_to new_session_url, alert: 'Try again later.' + } + + def new; end def create if user = User.authenticate_by(params.permit(:email_address, :password)) start_new_session_for user redirect_to after_authentication_url else - redirect_to new_session_path, alert: "Try another email address or password." + redirect_to new_session_path, alert: 'Try another email address or password.' end end diff --git a/app/models/project.rb b/app/models/project.rb index 834b3f0..dcf7328 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,23 +1,18 @@ +# frozen_string_literal: true + class Project < ApplicationRecord validates :name, :code, presence: true validates :code, exclusion: { in: %w[new] }, uniqueness: true - has_many :tasks + has_many :tasks, dependent: :restrict_with_exception has_rich_text :description - before_validation :lowercase_code + normalizes :code, with: ->(code) { code.strip.downcase } def to_param return unless id + code end - - private - - def lowercase_code - return if code.blank? - - self.code = self.code.downcase - end end diff --git a/bin/rubocop b/bin/rubocop new file mode 100755 index 0000000..369a05b --- /dev/null +++ b/bin/rubocop @@ -0,0 +1,27 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'rubocop' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) + +bundle_binstub = File.expand_path("bundle", __dir__) + +if File.file?(bundle_binstub) + if File.read(bundle_binstub, 300).include?("This file was generated by Bundler") + load(bundle_binstub) + else + abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. +Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") + end +end + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("rubocop", "rubocop")