Encapsulate initializer methods in modules (#14507)

* Move top level method definitions to module methods

This removes an ignored rubocop rule for this file, and wraps
these (otherwise unused) builder methods in a module.

* Move storage config methods to initializer module

This removes an rubocop ignore in the file and prevents exposing
global method definitions throughout the app.
This commit is contained in:
Daniel Uber 2021-08-17 08:19:13 -05:00 committed by GitHub
parent 26737d58d3
commit 35b575742b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 82 deletions

View file

@ -1,4 +1,3 @@
# rubocop:disable Style/TopLevelMethodDefinition
require "mini_magick"
# Carrierwave uses MiniMagick for image processing. To prevent server timeouts
@ -7,57 +6,62 @@ MiniMagick.configure do |config|
config.timeout = 10
end
def local_storage_config
CarrierWave.configure do |config|
config.storage = :file
config.enable_processing = !Rails.env.test? # disabled for test
config.asset_host = if Rails.env.production?
"https://#{ApplicationConfig['APP_DOMAIN']}"
end
module CarrierWaveInitializer
def self.local_storage_config
CarrierWave.configure do |config|
config.storage = :file
config.enable_processing = !Rails.env.test? # disabled for test
config.asset_host = if Rails.env.production?
"https://#{ApplicationConfig['APP_DOMAIN']}"
end
end
end
end
def standard_production_config
CarrierWave.configure do |config|
config.storage = :fog
config.fog_directory = ApplicationConfig["AWS_BUCKET_NAME"]
config.fog_provider = "fog/aws"
config.fog_attributes = { cache_control: "public, max-age=#{365.days.to_i}" }
config.fog_credentials = {
provider: "AWS",
aws_access_key_id: ApplicationConfig["AWS_ID"],
aws_secret_access_key: ApplicationConfig["AWS_SECRET"],
region: ApplicationConfig["AWS_UPLOAD_REGION"].presence || ApplicationConfig["AWS_DEFAULT_REGION"]
}
def self.standard_production_config
CarrierWave.configure do |config|
config.storage = :fog
config.fog_directory = ApplicationConfig["AWS_BUCKET_NAME"]
config.fog_provider = "fog/aws"
config.fog_attributes = { cache_control: "public, max-age=#{365.days.to_i}" }
config.fog_credentials = {
provider: "AWS",
aws_access_key_id: ApplicationConfig["AWS_ID"],
aws_secret_access_key: ApplicationConfig["AWS_SECRET"],
region: ApplicationConfig["AWS_UPLOAD_REGION"].presence || ApplicationConfig["AWS_DEFAULT_REGION"]
}
end
end
end
def forem_cloud_config
CarrierWave.configure do |config|
config.storage = :fog
config.fog_directory = ApplicationConfig["AWS_BUCKET_NAME"]
config.fog_provider = "fog/aws"
config.asset_host = "https://#{ApplicationConfig['APP_DOMAIN']}/remoteimages"
config.fog_public = false
config.fog_credentials = {
provider: "AWS",
use_iam_profile: true,
region: "us-east-2"
}
def self.forem_cloud_config
CarrierWave.configure do |config|
config.storage = :fog
config.fog_directory = ApplicationConfig["AWS_BUCKET_NAME"]
config.fog_provider = "fog/aws"
config.asset_host = "https://#{ApplicationConfig['APP_DOMAIN']}/remoteimages"
config.fog_public = false
config.fog_credentials = {
provider: "AWS",
use_iam_profile: true,
region: "us-east-2"
}
end
end
def self.initialize!
if Rails.env.production? && ENV["FILE_STORAGE_LOCATION"] != "file"
if ENV["FOREM_CONTEXT"] == "forem_cloud"
forem_cloud_config
elsif ApplicationConfig["AWS_ID"].present?
standard_production_config
else
local_storage_config
end
else
local_storage_config
end
end
end
Rails.application.reloader.to_prepare do
if Rails.env.production? && ENV["FILE_STORAGE_LOCATION"] != "file"
if ENV["FOREM_CONTEXT"] == "forem_cloud"
forem_cloud_config
elsif ApplicationConfig["AWS_ID"].present?
standard_production_config
else
local_storage_config
end
else
local_storage_config
end
CarrierWaveInitializer.initialize!
end
# rubocop:enable Style/TopLevelMethodDefinition

View file

@ -1,4 +1,3 @@
# rubocop:disable Style/TopLevelMethodDefinition
Rails.application.config.to_prepare do
Dir.glob(Rails.root.join("lib/slack/notifier/util/*.rb")).each do |filename|
require_dependency filename
@ -11,43 +10,44 @@ class NoOpHTTPClient
end
end
def create_test_channel_notifier
return create_stubbed_notifier if ApplicationConfig["SLACK_WEBHOOK_URL"].blank?
module SlackNotifierInitializer
def self.create_test_channel_notifier
return create_stubbed_notifier if ApplicationConfig["SLACK_WEBHOOK_URL"].blank?
Slack::Notifier.new(
ApplicationConfig["SLACK_WEBHOOK_URL"],
channel: "#test",
username: "development_test_bot",
)
end
Slack::Notifier.new(
ApplicationConfig["SLACK_WEBHOOK_URL"],
channel: "#test",
username: "development_test_bot",
)
end
def create_stubbed_notifier
Slack::Notifier.new "WEBHOOK_URL" do
http_client NoOpHTTPClient
def self.create_stubbed_notifier
Slack::Notifier.new "WEBHOOK_URL" do
http_client NoOpHTTPClient
end
end
def self.init_slack_client
default_options = if Rails.env.production?
{
channel: ApplicationConfig["SLACK_CHANNEL"],
username: "activity_bot"
}
elsif Rails.env.test?
{
channel: "#test",
username: "development_test_bot"
}
end
webhook_url = ApplicationConfig["SLACK_WEBHOOK_URL"] || ""
use_no_op_client = Rails.env.test? || webhook_url.blank?
Slack::Notifier.new(webhook_url) do
defaults(default_options)
http_client(NoOpHTTPClient) if use_no_op_client
end
end
end
def init_slack_client
default_options = if Rails.env.production?
{
channel: ApplicationConfig["SLACK_CHANNEL"],
username: "activity_bot"
}
elsif Rails.env.test?
{
channel: "#test",
username: "development_test_bot"
}
end
webhook_url = ApplicationConfig["SLACK_WEBHOOK_URL"] || ""
use_no_op_client = Rails.env.test? || webhook_url.blank?
Slack::Notifier.new(webhook_url) do
defaults(default_options)
http_client(NoOpHTTPClient) if use_no_op_client
end
end
SlackClient = init_slack_client
# rubocop:enable Style/TopLevelMethodDefinition
SlackClient = SlackNotifierInitializer.init_slack_client