diff --git a/Gemfile b/Gemfile index 8824e1b..169f39e 100644 --- a/Gemfile +++ b/Gemfile @@ -38,6 +38,7 @@ gem 'net-ldap' # Utilities gem "rqrcode", "~> 2.0" +gem 'rails-settings-cached', '~> 2.8.3' # HTTP requests gem 'faraday' diff --git a/Gemfile.lock b/Gemfile.lock index d80ab81..5a658b5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -206,6 +206,9 @@ GEM nokogiri (>= 1.6) rails-html-sanitizer (1.4.3) loofah (~> 2.3) + rails-settings-cached (2.8.3) + activerecord (>= 5.0.0) + railties (>= 5.0.0) railties (7.0.4) actionpack (= 7.0.4) activesupport (= 7.0.4) @@ -327,6 +330,7 @@ DEPENDENCIES pg (~> 1.2.3) puma (~> 4.1) rails (~> 7.0.2) + rails-settings-cached (~> 2.8.3) rqrcode (~> 2.0) rspec-rails sidekiq (< 7) diff --git a/app/controllers/admin/lightning_controller.rb b/app/controllers/admin/lightning_controller.rb index 10e6267..2b3dfe7 100644 --- a/app/controllers/admin/lightning_controller.rb +++ b/app/controllers/admin/lightning_controller.rb @@ -13,7 +13,7 @@ class Admin::LightningController < Admin::BaseController end def check_feature_enabled - if ENV["LNDHUB_ADMIN_UI"].empty? + if !Setting.lndhub_admin_enabled? flash[:alert] = "Lightning Admin UI not enabled" redirect_to admin_root_path and return end diff --git a/app/models/setting.rb b/app/models/setting.rb new file mode 100644 index 0000000..2a80940 --- /dev/null +++ b/app/models/setting.rb @@ -0,0 +1,7 @@ +# RailsSettings Model +class Setting < RailsSettings::Base + cache_prefix { "v1" } + + field :lndhub_enabled, default: (ENV["LNDHUB_API_URL"].present?.to_s || "false"), type: :boolean + field :lndhub_admin_enabled, default: (ENV["LNDHUB_ADMIN_UI"] || "false"), type: :boolean +end diff --git a/app/views/shared/_admin_nav.html.erb b/app/views/shared/_admin_nav.html.erb index 00a0fe8..dfba699 100644 --- a/app/views/shared/_admin_nav.html.erb +++ b/app/views/shared/_admin_nav.html.erb @@ -6,7 +6,7 @@ class: main_nav_class(@current_section, :invitations) %> <%= link_to "Donations", admin_donations_path, class: main_nav_class(@current_section, :donations) %> -<% if ENV["LNDHUB_ADMIN_UI"] %> +<% if Setting.lndhub_admin_enabled? %> <%= link_to "Lightning", admin_lightning_path, class: main_nav_class(@current_section, :lightning) %> <% end %> diff --git a/db/migrate/20230217084310_create_settings.rb b/db/migrate/20230217084310_create_settings.rb new file mode 100644 index 0000000..6fc5a0c --- /dev/null +++ b/db/migrate/20230217084310_create_settings.rb @@ -0,0 +1,15 @@ +class CreateSettings < ActiveRecord::Migration[7.0] + def self.up + create_table :settings do |t| + t.string :var, null: false + t.text :value, null: true + t.timestamps + end + + add_index :settings, %i(var), unique: true + end + + def self.down + drop_table :settings + end +end diff --git a/db/schema.rb b/db/schema.rb index 486c465..6b3cf56 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_01_11_113139) do +ActiveRecord::Schema[7.0].define(version: 2023_02_17_084310) do create_table "donations", force: :cascade do |t| t.integer "user_id" t.integer "amount_sats" @@ -34,6 +34,14 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_11_113139) do t.index ["user_id"], name: "index_invitations_on_user_id" end + create_table "settings", force: :cascade do |t| + t.string "var", null: false + t.text "value" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["var"], name: "index_settings_on_var", unique: true + end + create_table "users", force: :cascade do |t| t.string "cn" t.string "ou"