Skip to content

Commit

Permalink
195: setup annotate gem (#197)
Browse files Browse the repository at this point in the history
* 195: setup annotate gem

* 195: fix cops

* 195: remove reek warnings
  • Loading branch information
edimossilva authored May 27, 2024
1 parent bb774ed commit 56d1e37
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 2 deletions.
3 changes: 3 additions & 0 deletions backend/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions backend/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -573,6 +576,7 @@ DEPENDENCIES
active_model_serializers
acts_as_paranoid
administrate
annotate
bootsnap
brakeman
bullet
Expand Down
23 changes: 23 additions & 0 deletions backend/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -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 }

Expand Down
2 changes: 2 additions & 0 deletions backend/config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions backend/lib/tasks/auto_annotate_models.rake
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions backend/spec/factories/users.rb
Original file line number Diff line number Diff line change
@@ -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" }
Expand Down
1 change: 1 addition & 0 deletions backend/spec/support/devise.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 56d1e37

Please sign in to comment.