From 56d1e373b0a3c6b2f633f911d8622a08d82a5eeb Mon Sep 17 00:00:00 2001 From: Edimo Silva Date: Mon, 27 May 2024 19:49:51 -0300 Subject: [PATCH] 195: setup annotate gem (#197) * 195: setup annotate gem * 195: fix cops * 195: remove reek warnings --- backend/Gemfile | 3 + backend/Gemfile.lock | 4 ++ backend/app/models/user.rb | 23 +++++++ backend/config/initializers/devise.rb | 2 + backend/lib/tasks/auto_annotate_models.rake | 63 +++++++++++++++++++ .../controllers/hello_world_controller.rb | 4 +- .../app/oauth/actors/find_or_create_user.rb | 1 + backend/spec/factories/users.rb | 23 +++++++ backend/spec/support/devise.rb | 1 + 9 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 backend/lib/tasks/auto_annotate_models.rake diff --git a/backend/Gemfile b/backend/Gemfile index 3d823f0..9bac9c1 100644 --- a/backend/Gemfile +++ b/backend/Gemfile @@ -134,6 +134,9 @@ group :development do # Speed up commands on slow machines / big apps [https://github.com/rails/spring] gem "spring" + + # Annotate Rails classes with schema and routes info + gem "annotate" end group :test do diff --git a/backend/Gemfile.lock b/backend/Gemfile.lock index 3417b9a..ef4eeed 100644 --- a/backend/Gemfile.lock +++ b/backend/Gemfile.lock @@ -106,6 +106,9 @@ GEM kaminari (~> 1.2.2) sassc-rails (~> 2.1) selectize-rails (~> 0.6) + annotate (3.2.0) + activerecord (>= 3.2, < 8.0) + rake (>= 10.4, < 14.0) ast (2.4.2) base64 (0.1.1) bcrypt (3.1.20) @@ -573,6 +576,7 @@ DEPENDENCIES active_model_serializers acts_as_paranoid administrate + annotate bootsnap brakeman bullet diff --git a/backend/app/models/user.rb b/backend/app/models/user.rb index 1ebe037..44ac7d4 100644 --- a/backend/app/models/user.rb +++ b/backend/app/models/user.rb @@ -1,5 +1,28 @@ # frozen_string_literal: true +# == Schema Information +# +# Table name: users +# +# id :bigint not null, primary key +# admin :boolean default(FALSE), not null +# deleted_at :datetime +# email :string default(""), not null +# encrypted_password :string default(""), not null +# oauth_provider :string +# oauth_uid :string +# remember_created_at :datetime +# reset_password_sent_at :datetime +# reset_password_token :string +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_users_on_deleted_at (deleted_at) +# index_users_on_email (email) UNIQUE +# index_users_on_reset_password_token (reset_password_token) UNIQUE +# class User < ApplicationRecord update_index("users") { self } diff --git a/backend/config/initializers/devise.rb b/backend/config/initializers/devise.rb index ad223dc..dff7449 100644 --- a/backend/config/initializers/devise.rb +++ b/backend/config/initializers/devise.rb @@ -311,7 +311,9 @@ # To avoid DEPRECATION WARNING: `application.secrets` is deprecated in favor of `Rails.application.credentials` # and will be removed in Rails 7.2. module Devise + # :reek:InstanceVariableAssumption class SecretKeyFinder + # :reek:UtilityFunction and :reek:InstanceVariableAssumption def find @application.secret_key_base end diff --git a/backend/lib/tasks/auto_annotate_models.rake b/backend/lib/tasks/auto_annotate_models.rake new file mode 100644 index 0000000..6946705 --- /dev/null +++ b/backend/lib/tasks/auto_annotate_models.rake @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +# NOTE: only doing this in development as some production environments (Heroku) +# NOTE: are sensitive to local FS writes, and besides -- it's just not proper +# NOTE: to have a dev-mode tool do its thing in production. +if Rails.env.development? + require "annotate" + # rubocop:disable Metrics/BlockLength + task set_annotation_options: :environment do + # You can override any of these by setting an environment variable of the + # same name. + Annotate.set_defaults( + "active_admin" => "false", + "additional_file_patterns" => [], + "routes" => "false", + "models" => "true", + "position_in_routes" => "before", + "position_in_class" => "before", + "position_in_test" => "before", + "position_in_fixture" => "before", + "position_in_factory" => "before", + "position_in_serializer" => "before", + "show_foreign_keys" => "true", + "show_complete_foreign_keys" => "false", + "show_indexes" => "true", + "simple_indexes" => "false", + "model_dir" => "app/models", + "root_dir" => "", + "include_version" => "false", + "require" => "", + "exclude_tests" => "true", + "exclude_fixtures" => "true", + "exclude_factories" => "false", + "exclude_serializers" => "true", + "exclude_scaffolds" => "true", + "exclude_controllers" => "true", + "exclude_helpers" => "true", + "exclude_sti_subclasses" => "false", + "ignore_model_sub_dir" => "false", + "ignore_columns" => nil, + "ignore_routes" => nil, + "ignore_unknown_models" => "false", + "hide_limit_column_types" => "integer,bigint,boolean", + "hide_default_column_types" => "json,jsonb,hstore", + "skip_on_db_migrate" => "false", + "format_bare" => "true", + "format_rdoc" => "false", + "format_yard" => "false", + "format_markdown" => "false", + "sort" => "false", + "force" => "false", + "frozen" => "false", + "classified_sort" => "true", + "trace" => "false", + "wrapper_open" => nil, + "wrapper_close" => nil, + "with_comment" => "true" + ) + end + # rubocop:enable Metrics/BlockLength + + Annotate.load_tasks +end diff --git a/backend/packs/demo_pack/app/demo_pack/controllers/hello_world_controller.rb b/backend/packs/demo_pack/app/demo_pack/controllers/hello_world_controller.rb index 071ea04..ce14da5 100644 --- a/backend/packs/demo_pack/app/demo_pack/controllers/hello_world_controller.rb +++ b/backend/packs/demo_pack/app/demo_pack/controllers/hello_world_controller.rb @@ -15,8 +15,8 @@ def private_method def search query = search_params[:query] - @users = UsersIndex.query(query_string: { fields: [:email], query: query, default_operator: "and" }) - render json: @users.objects.to_json, status: :ok + users = UsersIndex.query(query_string: { fields: [:email], query: query, default_operator: "and" }) + render json: users.objects.to_json, status: :ok end private diff --git a/backend/packs/oauth/app/oauth/actors/find_or_create_user.rb b/backend/packs/oauth/app/oauth/actors/find_or_create_user.rb index 8b505df..1ee7d3d 100644 --- a/backend/packs/oauth/app/oauth/actors/find_or_create_user.rb +++ b/backend/packs/oauth/app/oauth/actors/find_or_create_user.rb @@ -33,6 +33,7 @@ def find_or_create_with_oauth_provider(auth) end end + # :reek:UtilityFunction def strava_generated_email(oauth_provider) "#{oauth_provider.uid}@strava_unknown_email.com" end diff --git a/backend/spec/factories/users.rb b/backend/spec/factories/users.rb index f2f70b1..1a75b0d 100644 --- a/backend/spec/factories/users.rb +++ b/backend/spec/factories/users.rb @@ -1,5 +1,28 @@ # frozen_string_literal: true +# == Schema Information +# +# Table name: users +# +# id :bigint not null, primary key +# admin :boolean default(FALSE), not null +# deleted_at :datetime +# email :string default(""), not null +# encrypted_password :string default(""), not null +# oauth_provider :string +# oauth_uid :string +# remember_created_at :datetime +# reset_password_sent_at :datetime +# reset_password_token :string +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_users_on_deleted_at (deleted_at) +# index_users_on_email (email) UNIQUE +# index_users_on_reset_password_token (reset_password_token) UNIQUE +# FactoryBot.define do factory :user do sequence(:email) { |n| "user#{n}@email.com" } diff --git a/backend/spec/support/devise.rb b/backend/spec/support/devise.rb index f8e7fb1..b16551c 100644 --- a/backend/spec/support/devise.rb +++ b/backend/spec/support/devise.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true module Devise + # :reek:UtilityFunction def auth_headers_for(user) Devise::Api::TokensService::Create.new(resource_owner: user).call token = Devise::Api::Token.find_by(resource_owner_id: user).access_token