From c1fb0e7dc4aae5e90be84dc3a2496bce3cad81a6 Mon Sep 17 00:00:00 2001 From: stephann <3025661+stephannv@users.noreply.github.com> Date: Sun, 4 Sep 2022 09:17:09 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Use=20settings=20class=20instead?= =?UTF-8?q?=20environment=20checks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .rubocop.yml | 4 + .../users/omniauth_callbacks_controller.rb | 6 +- app/operations/admin/import_data.rb | 2 +- .../event_dispatches/send_to_telegram.rb | 2 + app/views/devise/sessions/new.html.erb | 18 +- app/views/layouts/application.html.erb | 2 +- bin/setup_dev_credentials | 4 + config/initializers/01_settings.rb | 37 ++++ config/initializers/devise.rb | 24 ++- config/initializers/sentry.rb | 2 +- .../omniauth_callbacks_controller_spec.rb | 2 +- spec/initializers/settings_spec.rb | 190 ++++++++++++++++++ spec/operations/admin/import_data_spec.rb | 11 - .../event_dispatches/send_to_telegram_spec.rb | 19 ++ 14 files changed, 287 insertions(+), 36 deletions(-) create mode 100644 config/initializers/01_settings.rb create mode 100644 spec/initializers/settings_spec.rb diff --git a/.rubocop.yml b/.rubocop.yml index 7489cef..8b33f7d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -37,6 +37,9 @@ Layout/MultilineMethodCallBraceLayout: Layout/MultilineMethodCallIndentation: EnforcedStyle: indented +Layout/MultilineOperationIndentation: + EnforcedStyle: indented + Lint/MissingSuper: Enabled: false @@ -51,6 +54,7 @@ Metrics/BlockLength: - "spec/**/*" - "config/environments/*" - "config/routes.rb" + - "config/initializers/devise.rb" Metrics/MethodLength: CountAsOne: ["array", "hash", "heredoc"] diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index 29dee1a..a715173 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -2,11 +2,9 @@ module Users class OmniauthCallbacksController < Devise::OmniauthCallbacksController - skip_before_action :verify_authenticity_token, only: :developer unless Rails.env.production? # rubocop:disable Rails/LexicallyScopedActionFilter - - %i[twitter discord developer].each do |provider| - next if provider == :developer && Rails.env.production? + skip_before_action :verify_authenticity_token, only: :developer # rubocop:disable Rails/LexicallyScopedActionFilter + Devise.omniauth_providers.each do |provider| define_method provider do result = Users::GetFromOmniauth.result(auth: request.env["omniauth.auth"]) diff --git a/app/operations/admin/import_data.rb b/app/operations/admin/import_data.rb index c0fd807..f0e6304 100644 --- a/app/operations/admin/import_data.rb +++ b/app/operations/admin/import_data.rb @@ -6,7 +6,7 @@ def call wrap_task("Import items") { RawItems::Import.call } wrap_task("Import prices") { Prices::Import.call } wrap_task("Update flags") { Items::UpdateFlags.call } - wrap_task("Send Telegram notifications") { EventDispatches::SendToTelegram.call } if Rails.env.production? + wrap_task("Send Telegram notifications") { EventDispatches::SendToTelegram.call } end private diff --git a/app/operations/event_dispatches/send_to_telegram.rb b/app/operations/event_dispatches/send_to_telegram.rb index a07f412..3aa1d8d 100644 --- a/app/operations/event_dispatches/send_to_telegram.rb +++ b/app/operations/event_dispatches/send_to_telegram.rb @@ -5,6 +5,8 @@ class SendToTelegram < Actor input :client, type: TelegramClient, default: -> { TelegramClient.new } def call + fail! unless Settings.enable_telegram_notifications? + pending_dispatches_grouped_by_item_id.each_with_index do |(_item_id, dispatches), index| send_message(dispatches, index) sleep 1 diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index 4b9eb98..2cc5677 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -7,17 +7,21 @@

<%= t(".sign_in") %>

- <%= button_to user_twitter_omniauth_authorize_path, method: :post, "data-turbo": false, class: "btn btn-block gap-4 text-white bg-[#1D9BF9] hover:bg-[#1D9BF9]/80" do %> - - <%= t(".sign_in_with", provider: "Twitter") %> + <% if Settings.enable_twitter_sign_in? %> + <%= button_to user_twitter_omniauth_authorize_path, method: :post, "data-turbo": false, class: "btn btn-block gap-4 text-white bg-[#1D9BF9] hover:bg-[#1D9BF9]/80" do %> + + <%= t(".sign_in_with", provider: "Twitter") %> + <% end %> <% end %> - <%= button_to user_discord_omniauth_authorize_path, method: :post, "data-turbo": false, class: "btn btn-block gap-4 text-white bg-[#7289DA] hover:bg-[#7289DA]/80" do %> - - <%= t(".sign_in_with", provider: "Discord") %> + <% if Settings.enable_discord_sign_in? %> + <%= button_to user_discord_omniauth_authorize_path, method: :post, "data-turbo": false, class: "btn btn-block gap-4 text-white bg-[#7289DA] hover:bg-[#7289DA]/80" do %> + + <%= t(".sign_in_with", provider: "Discord") %> + <% end %> <% end %> - <% unless Rails.env.production? %> + <% if Settings.enable_dev_sign_in? %> <%= button_to user_developer_omniauth_authorize_path, method: :post, "data-turbo": false, class: "btn btn-block gap-4" do %> <%= t(".sign_in_with", provider: "Developer") %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index c2fa03a..a7e8fe6 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,7 +11,7 @@ <%= stylesheet_link_tag "application", media: "all", "data-turbo-track": "reload" %> <%= javascript_include_tag "application", defer: true, "data-turbo-track": "reload" %> - <% if Rails.env.production? %> + <% if Settings.enable_analytics? %>