diff --git a/app/liquid_tags/github_tag/github_readme_tag.rb b/app/liquid_tags/github_tag/github_readme_tag.rb index 031dff499..50dd0750e 100644 --- a/app/liquid_tags/github_tag/github_readme_tag.rb +++ b/app/liquid_tags/github_tag/github_readme_tag.rb @@ -1,5 +1,3 @@ -require "nokogiri" - class GithubTag class GithubReadmeTag PARTIAL = "liquids/github_readme".freeze diff --git a/app/liquid_tags/glitch_tag.rb b/app/liquid_tags/glitch_tag.rb index dd45104d9..ae53c9643 100644 --- a/app/liquid_tags/glitch_tag.rb +++ b/app/liquid_tags/glitch_tag.rb @@ -1,5 +1,3 @@ -require "uri" - class GlitchTag < LiquidTagBase attr_accessor :uri diff --git a/app/services/authentication/providers.rb b/app/services/authentication/providers.rb index 83dd07228..d8eb8c1a6 100644 --- a/app/services/authentication/providers.rb +++ b/app/services/authentication/providers.rb @@ -1,10 +1,3 @@ -# We require all authentication modules to make sure providers -# are correctly preloaded both in development and in production and -# ready to be used when needed at runtime -Dir[Rails.root.join("app/services/authentication/**/*.rb")].each do |f| - require_dependency(f) -end - module Authentication module Providers # Retrieves a provider that is both available and enabled diff --git a/app/services/aws/fake_client.rb b/app/services/aws/fake_client.rb deleted file mode 100644 index dd0137fa9..000000000 --- a/app/services/aws/fake_client.rb +++ /dev/null @@ -1,8 +0,0 @@ -# Fake Aws client is used in non-production environments to prevent actual calls to AWS lambda -module Aws - class FakeClient - def invoke(*) - OpenStruct.new(payload: [{ body: { message: 0 }.to_json }.to_json]) - end - end -end diff --git a/config/application.rb b/config/application.rb index f77fb0731..7daee048b 100644 --- a/config/application.rb +++ b/config/application.rb @@ -22,22 +22,25 @@ module PracticalDeveloper # Initialize configuration defaults for originally generated Rails version. config.load_defaults 5.1 # NOTE: [Rails 6] we should at least work towards updating this to 5.2 + # [Rails 6] Zeitwerk is the new autoloader + # As we don't have `load_defaults 6.0` yet, it has to be enabled manually + # See + config.autoloader = :zeitwerk + + # Disable auto adding of default load paths to $LOAD_PATH + # Setting this to false saves Ruby from checking these directories when + # resolving require calls with relative paths, and saves Bootsnap work and + # RAM, since it does not need to build an index for them. + # see https://github.com/rails/rails/blob/6-0-stable/railties/CHANGELOG.md#rails-600rc2-july-22-2019 + config.add_autoload_paths_to_load_path = false + # Settings in config/environments/* take precedence over those specified here. # Application configuration can go into files in config/initializers # -- all .rb files in that directory are automatically loaded after loading # the framework and any gems in your application. - config.autoload_paths += Dir["#{config.root}/app/labor/"] - config.autoload_paths += Dir["#{config.root}/app/decorators/"] - config.autoload_paths += Dir["#{config.root}/app/services/"] - config.autoload_paths += Dir["#{config.root}/app/presenters/"] - config.autoload_paths += Dir["#{config.root}/app/liquid_tags/"] - config.autoload_paths += Dir["#{config.root}/app/black_box/"] - config.autoload_paths += Dir["#{config.root}/app/sanitizers"] - config.autoload_paths += Dir["#{config.root}/app/facades"] - config.autoload_paths += Dir["#{config.root}/app/errors"] - config.autoload_paths += Dir["#{config.root}/app/view_objects"] - config.autoload_paths += Dir["#{config.root}/lib/"] + config.autoload_paths += Dir["#{config.root}/lib"] + config.eager_load_paths += Dir["#{config.root}/lib"] config.active_job.queue_adapter = :sidekiq diff --git a/config/environment.rb b/config/environment.rb index cb82a4a5e..cac531577 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -3,5 +3,3 @@ require_relative "application" # Initialize the Rails application. Rails.application.initialize! - -require "carrierwave/orm/activerecord" diff --git a/config/initializers/authentication.rb b/config/initializers/authentication.rb new file mode 100644 index 000000000..41d103fb7 --- /dev/null +++ b/config/initializers/authentication.rb @@ -0,0 +1,8 @@ +Rails.application.config.to_prepare do + # We require all authentication modules to make sure providers + # are correctly preloaded and ready to be used at this point as the loading + # order is important + Dir[Rails.root.join("app/services/authentication/**/*.rb")].each do |f| + require_dependency(f) + end +end diff --git a/config/initializers/aws_sdk.rb b/config/initializers/aws_sdk.rb index 33f0fe21e..4460eb360 100644 --- a/config/initializers/aws_sdk.rb +++ b/config/initializers/aws_sdk.rb @@ -1,9 +1,18 @@ -AWS_LAMBDA = if Rails.env.production? - Aws::Lambda::Client.new( - region: ApplicationConfig["AWS_DEFAULT_REGION"], - access_key_id: ApplicationConfig["AWS_SDK_KEY"], - secret_access_key: ApplicationConfig["AWS_SDK_SECRET"], - ) - else - Aws::FakeClient.new - end +Rails.application.reloader.to_prepare do + AWS_LAMBDA = if Rails.env.production? + Aws::Lambda::Client.new( + region: ApplicationConfig["AWS_DEFAULT_REGION"], + access_key_id: ApplicationConfig["AWS_SDK_KEY"], + secret_access_key: ApplicationConfig["AWS_SDK_SECRET"], + ) + else + # Fake Aws::Lambda::Client + Class.new do + def invoke(*) + # rubocop:disable Performance/OpenStruct + OpenStruct.new(payload: [{ body: { message: 0 }.to_json }.to_json]) + # rubocop:enable Performance/OpenStruct + end + end.new + end +end diff --git a/config/initializers/datadog_apm.rb b/config/initializers/datadog_apm.rb index c01d1603c..19ad2df9c 100644 --- a/config/initializers/datadog_apm.rb +++ b/config/initializers/datadog_apm.rb @@ -1,6 +1,3 @@ -require "ddtrace" -require "datadog/statsd" - Datadog.configure do |c| c.tracer env: Rails.env c.tracer enabled: Rails.env.production? diff --git a/config/initializers/liquid.rb b/config/initializers/liquid.rb index 94988281d..1095733ab 100644 --- a/config/initializers/liquid.rb +++ b/config/initializers/liquid.rb @@ -1,14 +1,13 @@ -# TODO: [rhymes] [Rails 6] explicitly requiring dependencies in `classic` mode. -# Will move over to `zeitwerk` in a future PR -Dir.glob(Rails.root.join("lib/liquid/*.rb")).sort.each do |filename| - require_dependency filename -end - -# Our custom Liquid tags are registered to Liquid::Template at the bottom of -# each files. Each Liquid tags will need to be loaded/required before the main -# Liquid gem is evoked, hence the need for the fix below. - Rails.application.config.to_prepare do + # Explicitly requiring lib/liquid to make sure that our patches are loaded + # before liquid tags are loaded + Dir.glob(Rails.root.join("lib/liquid/*.rb")).sort.each do |filename| + require_dependency filename + end + + # Our custom Liquid tags are registered to Liquid::Template at the bottom of + # each files. Each Liquid tags will need to be loaded/required before the main + # Liquid gem is evoked, hence the need to pre-require them in order Dir.glob(Rails.root.join("app/liquid_tags/*.rb")).sort.each do |filename| require_dependency filename end diff --git a/config/initializers/pusher.rb b/config/initializers/pusher.rb index ae31e847c..47a2e7858 100644 --- a/config/initializers/pusher.rb +++ b/config/initializers/pusher.rb @@ -1,4 +1,3 @@ -require "pusher" require "pusher/push_notifications" Pusher.app_id = ApplicationConfig["PUSHER_APP_ID"] diff --git a/config/initializers/reverse_markdown.rb b/config/initializers/reverse_markdown.rb deleted file mode 100644 index 0364e4e80..000000000 --- a/config/initializers/reverse_markdown.rb +++ /dev/null @@ -1,13 +0,0 @@ -# This eagerload our custom ReverseMarkdown::Converters and allows it to be autoreloaded -# in development. -# -# Because files are eagerloaded in production, this fix is only -# applicable in development (and test, when needed) - -if Rails.env.development? || Rails.env.test? - Rails.application.config.to_prepare do - Dir.glob(Rails.root.join("app/lib/reverse_markdown/converters/*.rb")).sort.each do |filename| - require_dependency filename - end - end -end diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index df957f723..c3839de77 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -1,22 +1,9 @@ -# Monkey patch `stringify_keys` to make rack 2.1.1 compatible with Sidekiq UI Admin panel. -# Should be removed when 2.1.2 is released. -# https://github.com/rack/rack/pull/1428 -module Rack - module Session - module Abstract - class SessionHash - private - - def stringify_keys(other) - other.to_hash.transform_keys(&:to_s) - end - end - end +Rails.application.config.to_prepare do + Dir.glob(Rails.root.join("lib/sidekiq/*.rb")).sort.each do |filename| + require_dependency filename end end -require Rails.root.join("lib/sidekiq/worker_retries_exhausted_reporter") - Sidekiq.configure_server do |config| sidekiq_url = ApplicationConfig["REDIS_SIDEKIQ_URL"] || ApplicationConfig["REDIS_URL"] # On Heroku this configuration is overridden and Sidekiq will point at the redis diff --git a/config/initializers/zeitwerk.rb b/config/initializers/zeitwerk.rb new file mode 100644 index 000000000..88f3408fa --- /dev/null +++ b/config/initializers/zeitwerk.rb @@ -0,0 +1,15 @@ +# Zeitwerk (Rails 6+) autoloader +# see https://guides.rubyonrails.org/autoloading_and_reloading_constants.html + +# Configures default inflections for the zeitwerk autoloader +# see +Rails.autoloaders.each do |autoloader| + autoloader.inflector.inflect( + "html_rouge" => "HTMLRouge", + "url" => "URL", + ) +end + +# Ignoring folders that don't adhere to the new naming conventions +Rails.autoloaders.main.ignore(Rails.root.join("lib/data_update_scripts")) +Rails.autoloaders.main.ignore(Rails.root.join("lib/generators/data_update")) diff --git a/spec/lib/liquid/raw_spec.rb b/spec/lib/liquid/raw_spec.rb index f5117ac28..0e28f3ec4 100644 --- a/spec/lib/liquid/raw_spec.rb +++ b/spec/lib/liquid/raw_spec.rb @@ -1,8 +1,20 @@ require "rails_helper" -RSpec.describe Liquid::Raw do +RSpec.describe Liquid::Raw, type: :lib do it "uses the correct regexp for invalid tokens" do expected_regexp = /\A(.*)#{Liquid::TagStart}\s*(\w+)\s*#{Liquid::TagEnd}\z/om expect(described_class::FullTokenPossiblyInvalid).to eq(expected_regexp) end + + it "does not allow non whitespace characters in between the tags" do + invalid_markdown = '%}rawafter"onerror=alert(document.domain) ' + expect { Liquid::Template.parse(invalid_markdown) }.to raise_error(StandardError) + end + + it "raise error message when link tag contain non article URL" do + invalid_markdown = "{% link /some-random-link/ %}" + expect { Liquid::Template.parse(invalid_markdown) }.to( + raise_error(StandardError, "This URL is not an article link: {% link /some-random-link/ %}"), + ) + end end diff --git a/spec/lib/liquid/variable_spec.rb b/spec/lib/liquid/variable_spec.rb index a63a650d7..4ea0a44f2 100644 --- a/spec/lib/liquid/variable_spec.rb +++ b/spec/lib/liquid/variable_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -RSpec.describe Liquid::Variable do +RSpec.describe Liquid::Variable, type: :lib do it "does not allow instantiation" do expect { described_class.new("", nil) }.to raise_error(StandardError, /variables are disabled/) end diff --git a/spec/liquid_tags/liquid/raw_spec.rb b/spec/liquid_tags/liquid/raw_spec.rb deleted file mode 100644 index 5e1794c9e..000000000 --- a/spec/liquid_tags/liquid/raw_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require "rails_helper" - -RSpec.describe Liquid::Raw, type: :liquid_tag do - it "does not allow non whitespace characters in between the tags" do - invalid_markdown = '%}rawafter"onerror=alert(document.domain) ' - expect { Liquid::Template.parse(invalid_markdown) }.to raise_error(StandardError) - end - - it "raise error message when link tag contain non article URL" do - invalid_markdown = "{% link /some-random-link/ %}" - expect { Liquid::Template.parse(invalid_markdown) }.to( - raise_error(StandardError, "This URL is not an article link: {% link /some-random-link/ %}"), - ) - end -end