Merge pull request #5425 from consuldemocracy/zeitwerk

Use Zeitwerk to autoload files
This commit is contained in:
Javi Martín
2024-04-11 20:26:44 +02:00
committed by GitHub
69 changed files with 144 additions and 133 deletions

View File

@@ -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:
@@ -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

View File

@@ -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"

View File

@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)

View File

@@ -1,4 +1,4 @@
class Layout::CommonHTMLAttributesComponent < ApplicationComponent
class Layout::CommonHtmlAttributesComponent < ApplicationComponent
use_helpers :rtl?
private

View File

@@ -1,5 +1,3 @@
require "application_responder"
class ApplicationController < ActionController::Base
include TenantVariants
include GlobalizeFallbacks

View File

@@ -1,5 +1,3 @@
require "manager_authenticator"
class Management::SessionsController < ActionController::Base
include TenantVariants
include GlobalizeFallbacks

View File

@@ -11,6 +11,6 @@ module LayoutsHelper
end
def common_html_attributes
render Layout::CommonHTMLAttributesComponent.new
render Layout::CommonHtmlAttributesComponent.new
end
end

View File

@@ -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)

View File

@@ -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|

View File

@@ -67,7 +67,5 @@ module Ckeditor
end
end
end
autoload :ActiveStorage, "ckeditor/backend/active_storage"
end
end

View File

@@ -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|

View File

@@ -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

View File

@@ -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|

View File

@@ -1,5 +1,5 @@
class RemoteTranslations::Microsoft::Client
include SentencesParser
include RemoteTranslations::Microsoft::SentencesParser
CHARACTERS_LIMIT_PER_REQUEST = 5000
PREVENTING_TRANSLATION_KEY = "notranslate".freeze

View File

@@ -1,5 +1,5 @@
require "open-uri"
class SMSApi
class SmsApi
attr_accessor :client
def initialize

View File

@@ -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

View File

@@ -1,5 +1,3 @@
require "numeric"
class Debate < ApplicationRecord
include Flaggable
include Taggable

View File

@@ -24,7 +24,7 @@ class Verification::Sms
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?

View File

@@ -32,10 +32,6 @@ module Consul
# 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
@@ -125,8 +121,6 @@ module Consul
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
@@ -134,12 +128,19 @@ module Consul
# * 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"))

View File

@@ -72,6 +72,7 @@ Rails.application.configure do
# 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

View File

@@ -12,11 +12,6 @@ Rails.application.configure do
# 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

View File

@@ -20,7 +20,7 @@ Rails.application.configure do
# 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

View File

@@ -7,6 +7,12 @@ ActsAsTaggableOn.setup do |config|
# 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
@@ -39,8 +45,6 @@ module ActsAsTaggableOn
kind == "category"
end
include Graphqlable
scope :public_for_api, -> do
where(
kind: [nil, "category"],

View File

@@ -1 +0,0 @@
require "age"

View File

@@ -1,4 +1,4 @@
Audited.config do |config|
config.audit_class = ::Audit
config.audit_class = "::Audit"
config.ignored_default_callbacks = [:touch]
end

View File

@@ -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.
@@ -270,23 +270,22 @@ Devise.setup do |config|
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

View File

@@ -1,11 +1,13 @@
ActiveStorage::DirectUploadsController.class_eval do
def create
head :unauthorized
Rails.application.reloader.to_prepare do
ActiveStorage::DirectUploadsController.class_eval do
def create
head :unauthorized
end
end
end
ActiveStorage::DiskController.class_eval do
def update
head :unauthorized
ActiveStorage::DiskController.class_eval do
def update
head :unauthorized
end
end
end

View File

@@ -1,3 +1,9 @@
Rails.application.reloader.to_prepare do
Globalize::ActiveRecord::Translation.class_eval do
include SkipValidation
end
end
module Globalize
module ActiveRecord
module InstanceMethods
@@ -9,10 +15,6 @@ module Globalize
end
end
end
class Translation
include SkipValidation
end
end
end

View File

@@ -18,5 +18,6 @@
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.plural(/^(\d+)$/i, '\1')
inflect.acronym "SDG"
inflect.acronym "WYSIWYG"
inflect.irregular "organización", "organizaciones"
end

View File

@@ -1,6 +1,10 @@
ActsAsVotable::Vote.class_eval do
include Graphqlable
Rails.application.reloader.to_prepare do
ActsAsVotable::Vote.class_eval do
include Graphqlable
end
end
ActsAsVotable::Vote.class_eval do
belongs_to :signature
belongs_to :budget_investment, foreign_key: "votable_id", class_name: "Budget::Investment"

View File

@@ -1,5 +0,0 @@
class Numeric
def percent_of(n)
(to_f / n * 100).to_i
end
end

View File

@@ -5,7 +5,7 @@ require "omniauth-oauth2"
module OmniAuth
module Strategies
class Wordpress < OmniAuth::Strategies::OAuth2
option :name, "wordpress_oauth2"
option :name, :wordpress_oauth2
option :client_options, {}

View File

@@ -1,7 +1,7 @@
require "rails_helper"
describe Layout::CommonHTMLAttributesComponent do
let(:component) { Layout::CommonHTMLAttributesComponent.new }
describe Layout::CommonHtmlAttributesComponent do
let(:component) { Layout::CommonHtmlAttributesComponent.new }
context "with multitenancy disabled" do
before { allow(Rails.application.config).to receive(:multitenancy).and_return(false) }

View File

@@ -1,35 +1,44 @@
require "rails_helper"
include DocumentParser
describe DocumentParser do
before do
dummy_class = Class.new do
include DocumentParser
end
stub_const("DummyClass", dummy_class)
end
let(:dummy) { DummyClass.new }
describe "#get_document_number_variants" do
it "returns no variants when document_number is not defined" do
expect(DocumentParser.get_document_number_variants("1", "")).to be_empty
expect(dummy.get_document_number_variants("1", "")).to be_empty
end
it "trims and cleans up entry" do
expect(DocumentParser.get_document_number_variants(2, " 1 2@ 34")).to eq(["1234"])
expect(dummy.get_document_number_variants(2, " 1 2@ 34")).to eq(["1234"])
end
it "returns only one try for passports & residence cards" do
expect(DocumentParser.get_document_number_variants(2, "1234")).to eq(["1234"])
expect(DocumentParser.get_document_number_variants(3, "1234")).to eq(["1234"])
expect(dummy.get_document_number_variants(2, "1234")).to eq(["1234"])
expect(dummy.get_document_number_variants(3, "1234")).to eq(["1234"])
end
it "takes only the last 8 digits for dnis and resicence cards" do
expect(DocumentParser.get_document_number_variants(1, "543212345678")).to eq(["12345678"])
expect(dummy.get_document_number_variants(1, "543212345678")).to eq(["12345678"])
end
it "tries all the dni variants padding with zeroes" do
expect(DocumentParser.get_document_number_variants(1, "0123456"))
expect(dummy.get_document_number_variants(1, "0123456"))
.to eq(["123456", "0123456", "00123456"])
expect(DocumentParser.get_document_number_variants(1, "00123456"))
expect(dummy.get_document_number_variants(1, "00123456"))
.to eq(["123456", "0123456", "00123456"])
end
it "adds upper and lowercase letter when the letter is present" do
expect(DocumentParser.get_document_number_variants(1, "1234567A"))
expect(dummy.get_document_number_variants(1, "1234567A"))
.to eq(%w[1234567 01234567 1234567a 1234567A 01234567a 01234567A])
end
end

View File

@@ -378,6 +378,10 @@ describe MachineLearning do
end
describe "#run_machine_learning_scripts" do
let!(:original_fork_mode) { DEBUGGER__::CONFIG[:fork_mode] }
before { DEBUGGER__::CONFIG[:fork_mode] = "parent" }
after { DEBUGGER__::CONFIG[:fork_mode] = original_fork_mode }
it "returns true if python script executed correctly" do
machine_learning = MachineLearning.new(job)

View File

@@ -178,16 +178,15 @@ describe "Homepage", :admin do
link_text: "Link1 text",
link_url: "consul1.dev")
# TODO: uncomment again after switching to zeitwerk
# card2 = create(:widget_card, label: "Card2 label",
# title: "Card2 text",
# description: "Card2 description",
# link_text: "Link2 text",
# link_url: "consul2.dev")
card2 = create(:widget_card, label: "Card2 label",
title: "Card2 text",
description: "Card2 description",
link_text: "Link2 text",
link_url: "consul2.dev")
visit root_path
expect(page).to have_css(".card", count: 1) # TODO: change to `count: 2 after switching to zeitwerk
expect(page).to have_css(".card", count: 2)
within("#widget_card_#{card1.id}") do
expect(page).to have_content("CARD1 LABEL")
@@ -198,15 +197,14 @@ describe "Homepage", :admin do
expect(page).to have_css("img[alt='#{card1.image.title}']")
end
# TODO: uncomment again after switching to zeitwerk
# within("#widget_card_#{card2.id}") do
# expect(page).to have_content("CARD2 LABEL")
# expect(page).to have_content("CARD2 TEXT")
# expect(page).to have_content("Card2 description")
# expect(page).to have_content("Link2 text")
# expect(page).to have_link(href: "consul2.dev")
# expect(page).to have_css("img[alt='#{card2.image.title}']")
# end
within("#widget_card_#{card2.id}") do
expect(page).to have_content("CARD2 LABEL")
expect(page).to have_content("CARD2 TEXT")
expect(page).to have_content("Card2 description")
expect(page).to have_content("Link2 text")
expect(page).to have_link(href: "consul2.dev")
expect(page).to have_css("img[alt='#{card2.image.title}']")
end
end
scenario "Recomendations" do

View File

@@ -59,16 +59,14 @@ describe "Cards", :admin do
scenario "Show" do
card_1 = create(:widget_card, title: "Card homepage large", columns: 8)
# TODO: uncomment after switching to zeitwerk
# card_2 = create(:widget_card, title: "Card homepage medium", columns: 4)
# card_3 = create(:widget_card, title: "Card homepage small", columns: 2)
card_2 = create(:widget_card, title: "Card homepage medium", columns: 4)
card_3 = create(:widget_card, title: "Card homepage small", columns: 2)
visit root_path
expect(page).to have_css("#widget_card_#{card_1.id}.medium-8")
# TODO: uncomment after switching to zeitwerk
# expect(page).to have_css("#widget_card_#{card_2.id}.medium-4")
# expect(page).to have_css("#widget_card_#{card_3.id}.medium-2")
expect(page).to have_css("#widget_card_#{card_2.id}.medium-4")
expect(page).to have_css("#widget_card_#{card_3.id}.medium-2")
end
scenario "Edit" do

View File

@@ -26,7 +26,7 @@ describe "Polls" do
end
scenario "Polls can be listed" do
polls = [create(:poll, :with_image)] # TODO: generate a list again after switching to zeitwerk
polls = create_list(:poll, 3, :with_image)
visit polls_path
@@ -210,24 +210,23 @@ describe "Polls" do
expect("Second question").to appear_before("Third question")
end
# TODO: uncomment after switching to zeitwerk
# scenario "Buttons to slide through images work back and forth" do
# question = create(:poll_question, :yes_no, poll: poll)
# create(:image, imageable: question.question_answers.last, title: "The no movement")
# create(:image, imageable: question.question_answers.last, title: "No movement planning")
scenario "Buttons to slide through images work back and forth" do
question = create(:poll_question, :yes_no, poll: poll)
create(:image, imageable: question.question_answers.last, title: "The no movement")
create(:image, imageable: question.question_answers.last, title: "No movement planning")
# visit poll_path(poll)
visit poll_path(poll)
# within(".orbit-bullets") do
# find("[data-slide='1']").click
within(".orbit-bullets") do
find("[data-slide='1']").click
# expect(page).to have_css ".is-active[data-slide='1']"
expect(page).to have_css ".is-active[data-slide='1']"
# find("[data-slide='0']").click
find("[data-slide='0']").click
# expect(page).to have_css ".is-active[data-slide='0']"
# end
# end
expect(page).to have_css ".is-active[data-slide='0']"
end
end
scenario "Non-logged in users" do
create(:poll_question, :yes_no, poll: poll)