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

Use Zeitwerk to autoload files #5425

Merged
merged 18 commits into from
Apr 11, 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
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ AllCops:
DisplayStyleGuide: true
Exclude:
- "db/schema.rb"
- "lib/ckeditor/backend/active_storage.rb"
- "app/lib/ckeditor/backend/active_storage.rb"
DisabledByDefault: true

Bundler/DuplicatedGem:
Expand Down Expand Up @@ -480,7 +480,7 @@ Rails/SkipsModelValidations:
- update_attribute
Exclude:
- app/models/tenant.rb
- lib/acts_as_paranoid_aliases.rb
- app/lib/acts_as_paranoid_aliases.rb

Rails/TimeZone:
Enabled: true
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ gem "wkhtmltopdf-binary", "~> 0.12.6"

group :development, :test do
gem "bullet", "~> 7.1.6"
gem "byebug", "~> 11.1.3"
gem "debug", "~> 1.9.2"
gem "factory_bot_rails", "~> 6.4.3"
gem "faker", "~> 3.2.3"
gem "i18n-tasks", "~> 0.9.37"
Expand Down
17 changes: 15 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ GEM
bullet (7.1.6)
activesupport (>= 3.0.0)
uniform_notifier (~> 1.11)
byebug (11.1.3)
cancancan (3.5.0)
capistrano (3.18.1)
airbrussh (>= 1.0.0)
Expand Down Expand Up @@ -171,6 +170,9 @@ GEM
daemons (1.4.1)
dalli (3.2.8)
date (3.3.4)
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
delayed_job (4.1.11)
activesupport (>= 3.0, < 8.0)
delayed_job_active_record (4.1.8)
Expand Down Expand Up @@ -282,6 +284,10 @@ GEM
ruby-vips (>= 2.0.17, < 3)
invisible_captcha (2.3.0)
rails (>= 5.2)
io-console (0.7.2)
irb (1.12.0)
rdoc
reline (>= 0.4.2)
json (2.7.1)
jwt (2.7.1)
kaminari (1.2.2)
Expand Down Expand Up @@ -438,6 +444,8 @@ GEM
pronto-stylelint (0.10.3)
pronto (>= 0.10, < 0.12)
rugged (>= 0.24, < 2.0)
psych (5.1.2)
stringio
public_suffix (4.0.7)
puma (5.6.8)
nio4r (~> 2.0)
Expand Down Expand Up @@ -486,10 +494,14 @@ GEM
rainbow (3.1.1)
rake (13.1.0)
rbtree3 (0.7.1)
rdoc (6.6.3.1)
psych (>= 4.0.0)
recipient_interceptor (0.3.1)
mail
redcarpet (3.6.0)
regexp_parser (2.9.0)
reline (0.5.1)
io-console (~> 0.5)
request_store (1.6.0)
rack (>= 1.4)
responders (3.1.1)
Expand Down Expand Up @@ -615,6 +627,7 @@ GEM
net-scp (>= 1.1.2)
net-sftp (>= 2.1.2)
net-ssh (>= 2.8.0)
stringio (3.1.0)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
terrapin (0.6.0)
Expand Down Expand Up @@ -683,7 +696,6 @@ DEPENDENCIES
autoprefixer-rails (~> 10.4.16)
bing_translator (~> 6.2.0)
bullet (~> 7.1.6)
byebug (~> 11.1.3)
cancancan (~> 3.5.0)
capistrano (~> 3.18.1)
capistrano-bundler (~> 2.1.0)
Expand All @@ -699,6 +711,7 @@ DEPENDENCIES
cocoon (~> 1.2.15)
daemons (~> 1.4.1)
dalli (~> 3.2.8)
debug (~> 1.9.2)
delayed_job_active_record (~> 4.1.8)
devise (~> 4.9.3)
devise-security (~> 0.18.0)
Expand Down
2 changes: 1 addition & 1 deletion app/components/layout/common_html_attributes_component.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Layout::CommonHTMLAttributesComponent < ApplicationComponent
class Layout::CommonHtmlAttributesComponent < ApplicationComponent
use_helpers :rtl?

private
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "application_responder"

class ApplicationController < ActionController::Base
include TenantVariants
include GlobalizeFallbacks
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/management/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "manager_authenticator"

class Management::SessionsController < ActionController::Base
include TenantVariants
include GlobalizeFallbacks
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/layouts_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ def layout_menu_link_to(text, path, is_active, options)
end

def common_html_attributes
render Layout::CommonHTMLAttributesComponent.new
render Layout::CommonHtmlAttributesComponent.new
end
end
2 changes: 1 addition & 1 deletion app/helpers/votes_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module VotesHelper
def debate_percentage_of_likes(debate)
debate.likes.percent_of(debate.total_votes)
(debate.likes.to_f * 100 / debate.total_votes).to_i
end

def votes_percentage(vote, debate)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion lib/census_api.rb → app/lib/census_api.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include DocumentParser
class CensusApi
include DocumentParser

def call(document_type, document_number)
response = nil
get_document_number_variants(document_type, document_number).each do |variant|
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,5 @@ def apply_data
end
end
end

autoload :ActiveStorage, "ckeditor/backend/active_storage"
end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion lib/local_census.rb → app/lib/local_census.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include DocumentParser
class LocalCensus
include DocumentParser

def call(document_type, document_number)
record = nil
get_document_number_variants(document_type, document_number).each do |variant|
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 8 additions & 16 deletions lib/omniauth_tenant_setup.rb → app/lib/omniauth_tenant_setup.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
module OmniauthTenantSetup
class << self
def twitter
->(env) do
oauth(env, secrets.twitter_key, secrets.twitter_secret)
end
def twitter(env)
oauth(env, secrets.twitter_key, secrets.twitter_secret)
end

def facebook
->(env) do
oauth2(env, secrets.facebook_key, secrets.facebook_secret)
end
def facebook(env)
oauth2(env, secrets.facebook_key, secrets.facebook_secret)
end

def google_oauth2
->(env) do
oauth2(env, secrets.google_oauth2_key, secrets.google_oauth2_secret)
end
def google_oauth2(env)
oauth2(env, secrets.google_oauth2_key, secrets.google_oauth2_secret)
end

def wordpress_oauth2
->(env) do
oauth2(env, secrets.wordpress_oauth2_key, secrets.wordpress_oauth2_secret)
end
def wordpress_oauth2(env)
oauth2(env, secrets.wordpress_oauth2_key, secrets.wordpress_oauth2_secret)
end

private
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion lib/remote_census_api.rb → app/lib/remote_census_api.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include DocumentParser
class RemoteCensusApi
include DocumentParser

def call(document_type, document_number, date_of_birth, postal_code)
response = nil
get_document_number_variants(document_type, document_number).each do |variant|
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class RemoteTranslations::Microsoft::Client
include SentencesParser
include RemoteTranslations::Microsoft::SentencesParser
CHARACTERS_LIMIT_PER_REQUEST = 5000
PREVENTING_TRANSLATION_KEY = "notranslate".freeze

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/sms_api.rb → app/lib/sms_api.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "open-uri"
class SMSApi
class SmsApi
attr_accessor :client

def initialize
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion app/models/custom/setting.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_dependency Rails.root.join("app", "models", "setting").to_s
load Rails.root.join("app", "models", "setting.rb")

class Setting
class << self
Expand Down
2 changes: 0 additions & 2 deletions app/models/debate.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "numeric"

class Debate < ApplicationRecord
include Flaggable
include Taggable
Expand Down
2 changes: 1 addition & 1 deletion app/models/verification/sms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def update_user_phone_information
end

def send_sms
SMSApi.new.sms_deliver(user.unconfirmed_phone, user.sms_confirmation_code)
SmsApi.new.sms_deliver(user.unconfirmed_phone, user.sms_confirmation_code)
end

def verified?
Expand Down
25 changes: 13 additions & 12 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ class Application < Rails::Application
# in any CONSUL DEMOCRACY installations
config.active_support.use_authenticated_message_encryption = false

# Keep using the classic autoloader until we decide how custom classes
# should work with zeitwerk
config.autoloader = :classic

# Don't enable has_many_inversing because it doesn't seem to currently
# work with the _count database columns we use for caching purposes
config.active_record.has_many_inversing = false
Expand Down Expand Up @@ -125,21 +121,26 @@ class Application < Rails::Application
config.assets.paths << Rails.root.join("node_modules", "jquery-ui", "themes", "base")
config.assets.paths << Rails.root.join("node_modules")

# Add lib to the autoload path
config.autoload_paths << Rails.root.join("lib")
config.active_job.queue_adapter = :delayed_job

# CONSUL DEMOCRACY specific custom overrides
# Read more on documentation:
# * English: https://github.com/consuldemocracy/consuldemocracy/blob/master/CUSTOMIZE_EN.md
# * Spanish: https://github.com/consuldemocracy/consuldemocracy/blob/master/CUSTOMIZE_ES.md
#
config.autoload_paths << "#{Rails.root}/app/components/custom"
config.autoload_paths << "#{Rails.root}/app/controllers/custom"
config.autoload_paths << "#{Rails.root}/app/graphql/custom"
config.autoload_paths << "#{Rails.root}/app/mailers/custom"
config.autoload_paths << "#{Rails.root}/app/models/custom"
config.autoload_paths << "#{Rails.root}/app/models/custom/concerns"

[
"app/components/custom",
"app/controllers/custom",
"app/graphql/custom",
"app/lib/custom",
"app/mailers/custom",
"app/models/custom",
"app/models/custom/concerns"
].each do |path|
config.autoload_paths << Rails.root.join(path)
config.eager_load_paths << Rails.root.join(path)
end

config.paths["app/views"].unshift(Rails.root.join("app", "views", "custom"))

Expand Down
1 change: 1 addition & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
# Annotate rendered view with file names.
# config.action_view.annotate_rendered_view_with_filenames = true

config.eager_load_paths << "#{Rails.root}/spec/mailers/previews"
config.action_mailer.preview_path = "#{Rails.root}/spec/mailers/previews"

# Limit size of local logs
Expand Down
5 changes: 0 additions & 5 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
# Rake tasks automatically ignore this option for performance.
config.eager_load = true

# Because autoloading is disabled in production environments with Rails 5,
# using autoload_paths will not load needed classes from specified paths.
# The solution to this, is to ask Rails to eager load classes.
config.eager_load_paths += ["#{config.root}/lib"]

# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
Expand Down
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = false
config.eager_load = ENV["CI"].present?

# Configure public file server for tests with Cache-Control for performance.
config.public_file_server.enabled = true
Expand Down
8 changes: 6 additions & 2 deletions config/initializers/acts_as_taggable_on.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
# config.base_class = "ApplicationRecord"
end

Rails.application.reloader.to_prepare do
ActsAsTaggableOn::Tag.class_eval do
include Graphqlable
end
end

module ActsAsTaggableOn
Tagging.class_eval do
after_create :increment_tag_custom_counter
Expand Down Expand Up @@ -39,8 +45,6 @@ def category?
kind == "category"
end

include Graphqlable

scope :public_for_api, -> do
where(
kind: [nil, "category"],
Expand Down
1 change: 0 additions & 1 deletion config/initializers/age.rb

This file was deleted.

2 changes: 1 addition & 1 deletion config/initializers/audited.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Audited.config do |config|
config.audit_class = ::Audit
config.audit_class = "::Audit"
config.ignored_default_callbacks = [:touch]
end
11 changes: 5 additions & 6 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require Rails.root.join("lib", "omniauth_wordpress")
require Rails.root.join("lib", "omni_auth", "strategies", "wordpress")

# Use this hook to configure devise mailer, warden hooks and so forth.
# Many of these configuration options can be set straight in your model.
Expand Down Expand Up @@ -270,23 +270,22 @@
config.omniauth :twitter,
Rails.application.secrets.twitter_key,
Rails.application.secrets.twitter_secret,
setup: OmniauthTenantSetup.twitter
setup: ->(env) { OmniauthTenantSetup.twitter(env) }
config.omniauth :facebook,
Rails.application.secrets.facebook_key,
Rails.application.secrets.facebook_secret,
scope: "email",
info_fields: "email,name,verified",
setup: OmniauthTenantSetup.facebook
setup: ->(env) { OmniauthTenantSetup.facebook(env) }
config.omniauth :google_oauth2,
Rails.application.secrets.google_oauth2_key,
Rails.application.secrets.google_oauth2_secret,
setup: OmniauthTenantSetup.google_oauth2
setup: ->(env) { OmniauthTenantSetup.google_oauth2(env) }
config.omniauth :wordpress_oauth2,
Rails.application.secrets.wordpress_oauth2_key,
Rails.application.secrets.wordpress_oauth2_secret,
strategy_class: OmniAuth::Strategies::Wordpress,
client_options: { site: Rails.application.secrets.wordpress_oauth2_site },
setup: OmniauthTenantSetup.wordpress_oauth2
setup: ->(env) { OmniauthTenantSetup.wordpress_oauth2(env) }

# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
Expand Down
Loading
Loading