Skip to content

Commit

Permalink
Add bare-bones on-site settings editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Earlopain committed Oct 4, 2023
1 parent aa0731f commit a84a843
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 3 deletions.
19 changes: 19 additions & 0 deletions app/controllers/config_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class ConfigController < ApplicationController
def index
end

def show
@definition = Sites.from_enum(params[:id])
end

def modify
custom_config = params.fetch(:config, {}).permit!
custom_config[:scraper_request_rate_limit] = custom_config[:scraper_request_rate_limit].tr(",", ".").to_f if custom_config[:scraper_request_rate_limit]

Config.write_custom_config(custom_config)
Config.force_reload
redirect_back fallback_location: config_index_path
end
end
14 changes: 13 additions & 1 deletion app/logical/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ def custom_config_path
Rails.root.join("config/reverser_custom_config.yml")
end

def merge_custom_config(new_values)
custom_config.merge(new_values).transform_keys(&:to_s)
end

def write_custom_config(new_values)
data = Psych.safe_dump(merge_custom_config(new_values))
File.write(custom_config_path, data)
end

def force_reload
@default_config = nil
@custom_config = nil
Expand All @@ -27,7 +36,10 @@ def missing_values
def method_missing(method)
raise NoMethodError, "Unknown config #{method}" unless respond_to_missing?(method)

if custom_config.key?(method)
bool_key = method.to_s.delete_suffix("?").to_sym
if custom_config.key?(bool_key) && method.end_with?("?")
custom_config[bool_key] == "true"
elsif custom_config.key?(method)
custom_config[method]
else
default_config[method]
Expand Down
2 changes: 2 additions & 0 deletions app/logical/sites/scraper_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def missing_config_keys
@scraper.required_config_keys.select { |key| Config.send(key).blank? }
end

delegate :all_config_keys, to: :@scraper

def cached_values
@scraper.cached_methods.filter_map do |method|
key = @scraper.cache_key(method)
Expand Down
1 change: 1 addition & 0 deletions app/views/application/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<%= nav_link_to "Submission Files", submission_files_path %>
<%= nav_link_to "Archive Importer", new_archive_import_path %>
<%= nav_link_to "Stats", stats_path %>
<%= nav_link_to "Config", config_index_path %>
<%= nav_link_to "Selenium", "http://localhost:#{DockerEnv.exposed_vnc_port}" %>
<%= nav_link_to "GoodJob", "/good_job" %>
</menu>
Expand Down
1 change: 1 addition & 0 deletions app/views/config/_secondary_links.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= subnav_link_to "Listing", config_index_path %>
13 changes: 13 additions & 0 deletions app/views/config/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1>General Settings</h1>

<%= simple_form_for(:config, url: modify_config_index_path, method: :put) do |f| %>
<%= f.input :app_name, input_html: { value: Config.app_name } %>
<%= f.input :log_scraper_requests, as: :select, selected: Config.log_scraper_requests? %>
<%= f.input :scraper_request_rate_limit, input_html: { value: Config.scraper_request_rate_limit } %>
<%= f.input :time_zone, collection: ActiveSupport::TimeZone.all.map { |tz| [tz.to_s, tz.name] }, selected: Config.time_zone %>
<%= f.submit %>
<% end %>

<% Sites.scraper_definitions.each do |definition| %>
<div><%= link_to "#{definition.display_name} settings", config_path(definition.enum_value) %></div>
<% end %>
9 changes: 9 additions & 0 deletions app/views/config/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h2>Settings for <%= @definition.display_name %></h2>
<%= link_to @definition.homepage, @definition.homepage %><br>
<%= simple_form_for(:config, url: modify_config_index_path, method: :put) do |f| %>
<% @definition.all_config_keys.each do |key| %>
<%= f.input key, label: key.to_s.delete_prefix("#{@definition.enum_value}_"), input_html: { value: Config.send(key) } %>
<% end %>
<%= f.input "#{@definition.enum_value}_disabled", as: :select, label: "disabled", selected: Config.send("#{@definition.enum_value}_disabled?").to_s %>
<%= f.submit %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/stats/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<% @definitions.each do |definition| %>
<% entry = @counts[definition.enum_value] || {} %>
<tr>
<td><%= definition.display_name %></td>
<td><%= link_to_if(definition.enum_value != "manual", definition.display_name, config_path(definition.enum_value)) %></td>
<td><%= link_to(entry["artist_count"] || 0, artists_path(search: { site_type: definition.enum_value })) %></td>
<td><%= link_to(entry["url_count"] || 0, artist_urls_path(search: { site_type: definition.enum_value })) %></td>
<td><%= link_to(entry["submission_count"] || 0, submission_files_path(search: { site_type: definition.enum_value })) %></td>
Expand Down
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@
resources :log_events, only: %i[index show]
resources :archive_imports, only: %i[new create]
resources :stats, only: :index
resources :config, controller: "config", only: %i[index show] do
collection do
put :modify
end
end
root to: "artists#index"
end
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ services:

tests:
image: reverser
environment:
EXPOSED_VNC_PORT: "123"
volumes:
- .:/app
entrypoint: bundle exec rails test
Expand Down
15 changes: 15 additions & 0 deletions test/controllers/config_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require "test_helper"

class ConfigControllerTest < ActionDispatch::IntegrationTest
test "index renders" do
get config_index_path
assert_response :success
end

test "show renders" do
get config_path("twitter")
assert_response :success
end
end
19 changes: 18 additions & 1 deletion test/logical/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class ConfigTest < ActiveSupport::TestCase
setup do
Config.stubs(:default_config).returns(app_name: "DefaultName")
Config.stubs(:default_config).returns(app_name: "DefaultName", bool?: true)
Config.force_reload
end

Expand All @@ -19,6 +19,7 @@ def stub_custom_config(**params, &)
f.flush
Config.unstub(:custom_config)
yield
Config.force_reload
end
end

Expand Down Expand Up @@ -48,4 +49,20 @@ def stub_custom_config(**params, &)
assert_equal("OverwrittenName", Config.app_name)
end
end

it "merges the config correctly" do
stub_custom_config(app_name: "OverwrittenName", other_key: "abc") do
assert_equal({ "app_name" => "NewName", "other_key" => "abc" }, Config.merge_custom_config("app_name" => "NewName"))
assert_equal({ "app_name" => "NewName", "other_key" => "abc" }, Config.merge_custom_config(app_name: "NewName"))
end
end

it "handles booleans" do
stub_custom_config(bool: "true") do
assert_predicate(Config, :bool?)
end
stub_custom_config(bool: "false") do
assert_not_predicate(Config, :bool?)
end
end
end

0 comments on commit a84a843

Please sign in to comment.