From 624faa10d0fef27d1cb04fa40ed3d48f97766c48 Mon Sep 17 00:00:00 2001 From: Tony Vincent Date: Fri, 28 Feb 2025 15:35:00 +0100 Subject: [PATCH] fix: Don't show Billings on settings navbar when self-hosted (#1912) * Do not show billing settings navbar item when self hosted * Do not show billing settings navbar item when self hosted * Add condition to settings helper * Let Stripe::AuthenticationError bubble up --- app/controllers/subscriptions_controller.rb | 6 ++++++ app/helpers/settings_helper.rb | 7 ++++++- app/views/settings/_settings_nav.html.erb | 9 +++++---- config/locales/views/subscriptions/en.yml | 3 +++ test/controllers/subscriptions_controller_test.rb | 13 ++++++++++--- test/system/settings_test.rb | 6 ++++++ 6 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 config/locales/views/subscriptions/en.yml diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb index e2c19535..64552508 100644 --- a/app/controllers/subscriptions_controller.rb +++ b/app/controllers/subscriptions_controller.rb @@ -1,4 +1,6 @@ class SubscriptionsController < ApplicationController + before_action :redirect_to_root_if_self_hosted + def new if Current.family.stripe_customer_id.blank? customer = stripe_client.v1.customers.create( @@ -44,4 +46,8 @@ class SubscriptionsController < ApplicationController def stripe_client @stripe_client ||= Stripe::StripeClient.new(ENV["STRIPE_SECRET_KEY"]) end + + def redirect_to_root_if_self_hosted + redirect_to root_path, alert: I18n.t("subscriptions.self_hosted_alert") if self_hosted? + end end diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rb index 0eaecd85..e15414a5 100644 --- a/app/helpers/settings_helper.rb +++ b/app/helpers/settings_helper.rb @@ -4,7 +4,7 @@ module SettingsHelper { name: I18n.t("settings.settings_nav.preferences_label"), path: :settings_preferences_path }, { name: I18n.t("settings.settings_nav.security_label"), path: :settings_security_path }, { name: I18n.t("settings.settings_nav.self_hosting_label"), path: :settings_hosting_path, condition: :self_hosted? }, - { name: I18n.t("settings.settings_nav.billing_label"), path: :settings_billing_path }, + { name: I18n.t("settings.settings_nav.billing_label"), path: :settings_billing_path, condition: :not_self_hosted? }, { name: I18n.t("settings.settings_nav.accounts_label"), path: :accounts_path }, { name: I18n.t("settings.settings_nav.imports_label"), path: :imports_path }, { name: I18n.t("settings.settings_nav.tags_label"), path: :tags_path }, @@ -45,4 +45,9 @@ module SettingsHelper concat(next_setting) end end + + private + def not_self_hosted? + !self_hosted? + end end diff --git a/app/views/settings/_settings_nav.html.erb b/app/views/settings/_settings_nav.html.erb index 1934d921..632e774c 100644 --- a/app/views/settings/_settings_nav.html.erb +++ b/app/views/settings/_settings_nav.html.erb @@ -32,10 +32,11 @@ <%= render "settings/settings_nav_item", name: t(".self_hosting_label"), path: settings_hosting_path, icon: "database" %> <% end %> - -
  • - <%= render "settings/settings_nav_item", name: t(".billing_label"), path: settings_billing_path, icon: "circle-dollar-sign" %> -
  • + <% unless self_hosted? %> +
  • + <%= render "settings/settings_nav_item", name: t(".billing_label"), path: settings_billing_path, icon: "circle-dollar-sign" %> +
  • + <% end %>
  • <%= render "settings/settings_nav_item", name: t(".accounts_label"), path: accounts_path, icon: "layers" %> diff --git a/config/locales/views/subscriptions/en.yml b/config/locales/views/subscriptions/en.yml new file mode 100644 index 00000000..72ab67a9 --- /dev/null +++ b/config/locales/views/subscriptions/en.yml @@ -0,0 +1,3 @@ +en: + subscriptions: + self_hosted_alert: "Maybe+ is not available in self-hosted mode." diff --git a/test/controllers/subscriptions_controller_test.rb b/test/controllers/subscriptions_controller_test.rb index 3fda28d4..fe1b38d7 100644 --- a/test/controllers/subscriptions_controller_test.rb +++ b/test/controllers/subscriptions_controller_test.rb @@ -1,7 +1,14 @@ require "test_helper" class SubscriptionsControllerTest < ActionDispatch::IntegrationTest - # test "the truth" do - # assert true - # end + setup do + sign_in @user = users(:family_admin) + end + + test "redirects to settings if self hosting" do + Rails.application.config.app_mode.stubs(:self_hosted?).returns(true) + get subscription_path + assert_redirected_to root_path + assert_equal I18n.t("subscriptions.self_hosted_alert"), flash[:alert] + end end diff --git a/test/system/settings_test.rb b/test/system/settings_test.rb index 32f2b8c8..9d0338b0 100644 --- a/test/system/settings_test.rb +++ b/test/system/settings_test.rb @@ -46,6 +46,12 @@ class SettingsTest < ApplicationSystemTestCase assert_selector 'span[data-clipboard-target="iconSuccess"]', visible: true, count: 1 # text copied and icon changed to checkmark end + test "does not show billing link if self hosting" do + Rails.application.config.app_mode.stubs(:self_hosted?).returns(true) + open_settings_from_sidebar + assert_no_selector "li", text: I18n.t("settings.settings_nav.billing_label") + end + private def open_settings_from_sidebar