Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

配置支持数据库读取和更新 #245

Merged
merged 6 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ gem 'webpacker', '~> 5.0'
gem 'app-info', '~> 2.1.4'

# Mobile config
gem 'settingslogic', require: false
gem 'rails-settings-cached', '~> 2.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.4', require: false
Expand Down
8 changes: 6 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ GEM
rails-i18n (6.0.0)
i18n (>= 0.7, < 2)
railties (>= 6.0.0, < 7)
rails-settings-cached (2.2.0)
rails (>= 4.2.0)
request_store
railties (6.0.3.2)
actionpack (= 6.0.3.2)
activesupport (= 6.0.3.2)
Expand All @@ -338,6 +341,8 @@ GEM
ffi (~> 1.0)
redis (4.1.4)
regexp_parser (1.7.1)
request_store (1.5.0)
rack (>= 1.4)
responders (3.0.1)
actionpack (>= 5.0)
railties (>= 5.0)
Expand Down Expand Up @@ -374,7 +379,6 @@ GEM
semantic_range (2.3.0)
sentry-raven (3.0.0)
faraday (>= 1.0)
settingslogic (2.0.9)
shellany (0.0.1)
sidekiq (6.0.7)
connection_pool (>= 2.2.2)
Expand Down Expand Up @@ -491,14 +495,14 @@ DEPENDENCIES
rack-cors (~> 1.1.1)
rails (~> 6.0.3)
rails-i18n (~> 6.0.0)
rails-settings-cached (~> 2.2)
rake (~> 13.0.1)
redis (~> 4.1.4)
rqrcode
rubocop (>= 0.70)
rubocop-rails
ruby-debug-ide
sentry-raven
settingslogic
sidekiq (= 6.0.7)
sidekiq-cron (~> 1.2)
simple_form (~> 5.0)
Expand Down
35 changes: 35 additions & 0 deletions app/controllers/admin/settings_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

class Admin::SettingsController < ApplicationController
before_action :set_setting, only: %i[edit update]

def index
@title = '系统配置'
@settings = Setting.site_configs
end

def edit
@title = '编辑设置'
end

def update
if @setting.value != setting_param[:value]
@setting.value = setting_param[:value]
@setting.save
redirect_to admin_settings_path, notice: "保存成功."
else
redirect_to :edit
end
end

private


def set_setting
@setting = Setting.find_or_default(var: params[:id])
end

def setting_param
params[:setting].permit!
end
end
4 changes: 2 additions & 2 deletions app/controllers/admin/system_info_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def get_version

@version = {
update_available: update_available,
current_version: Zealot::Setting.version,
current_version: Setting.version,
latest_version: latest_version,
release_link: release_link,
}
Expand All @@ -141,6 +141,6 @@ def get_version
def update_available?(new_version)
return true if Rails.env.development?

Gem::Version.new(new_version) > Gem::Version.new(Zealot::Setting.version)
Gem::Version.new(new_version) > Gem::Version.new(Setting.version)
end
end
2 changes: 1 addition & 1 deletion app/controllers/apps_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class AppsController < ApplicationController
before_action :authenticate_user! unless Zealot::Setting.guest_mode
before_action :authenticate_user! unless Setting.guest_mode
before_action :set_app, only: %i[show edit update destroy]

def index
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/dashboards_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class DashboardsController < ApplicationController
before_action :authenticate_user! unless Zealot::Setting.guest_mode
before_action :authenticate_user! unless Setting.guest_mode

def index
@title = '我的控制台'
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/teardowns_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class TeardownsController < ApplicationController
before_action :authenticate_user! unless Zealot::Setting.guest_mode
before_action :authenticate_user! unless Setting.guest_mode

def index
redirect_to new_teardown_path, alert: "链接失效,请重新解析文件"
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
@import 'system_info';
@import 'users';
@import 'visitor';
@import 'web_hooks';
@import 'web_hooks';
@import 'settings';
68 changes: 68 additions & 0 deletions app/javascript/stylesheets/settings.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

.form-group {
.switch {
position: relative;
display: inline-block;

.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}

.slider:before {
position: absolute;
content: "";
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}

.slider.round {
border-radius: 34px;
}

.slider.round:before {
border-radius: 50%;
}
}

/* Hide default HTML checkbox */
.switch input {
opacity: 0;
width: 0;
height: 0;
}

.switch-lg {
width: 48px;
height: 24px;

.slider:before {
height: 14px;
width: 14px;
left: 4px;
bottom: 5px;
}
}

input:checked + .slider {
background-color: #2196F3;
}

input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
}
74 changes: 74 additions & 0 deletions app/models/setting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true

# RailsSettings Model
class Setting < RailsSettings::Base
SITE_KEYS = {
general: %w[
site_title
],
visits: %w[
registrations_mode
guest_mode
],
mail: %w[
mailer_default_from
mailer_default_to
mailer_options
],
archives: %w[keep_uploads]
}

cache_prefix { 'v1' }

# 系统配置
field :site_title, default: 'Zealot', type: :string
field :site_https, default: ENV['ZEALOT_USE_HTTPS'].present?, type: :boolean, readonly: true
field :site_domain, default: (ENV['ZEALOT_DOMAIN'] || site_https ? 'localhost' : "localhost:#{ENV['ZEALOT_PORT'] || 3000}"), type: :string, readonly: true

# 模式
field :registrations_mode, default: (ENV['ZEALOT_REGISTER_ENABLED'] || 'true'), type: :boolean
field :guest_mode, default: (ENV['ZEALOT_GUEST_MODE'] || 'false'), type: :boolean

# 邮件配置
field :mailer_default_from, default: ENV['ACTION_MAILER_DEFAULT_FROM'], type: :string
field :mailer_default_to, default: ENV['ACTION_MAILER_DEFAULT_TO'], type: :string
field :mailer_options, type: :hash, readonly: true, default: {
address: ENV['SMTP_ADDRESS'],
port: ENV['SMTP_PORT'].to_i,
domain: ENV['SMTP_DOMAIN'],
username: ENV['SMTP_USERNAME'],
password: ENV['SMTP_PASSWORD'],
auth_method: ENV['SMTP_AUTH_METHOD'],
enable_starttls_auto: ENV['SMTP_ENABLE_STARTTLS_AUTO'],
}

# 系统信息(只读)
field :demo_mode, default: (ENV['ZEALOT_DEMO_MODE'] || 'false'), type: :boolean, readonly: true
field :keep_uploads, default: (ENV['ZEALOT_KEEP_UPLOADS'] || 'false'), type: :boolean

field :version, default: (ENV['ZEALOT_VERSION'] || 'development'), type: :string, readonly: true
field :vcs_ref, default: (ENV['ZEALOT_VCS_REF']), type: :string, readonly: true
field :version, default: (ENV['ZEALOT_VERSION'] || 'development'), type: :string, readonly: true
field :build_date, default: ENV['BUILD_DATE'], type: :string, readonly: true

field :backup, type: :hash, readonly: true, default: {
path: 'public/backup',
keep_time: 604800,
pg_schema: 'public',
}

class << self
def find_or_default(var:)
find_by(var: var) || new(var: var)
end

def site_configs
SITE_KEYS.each_with_object({}) do |(section, keys), obj|
obj[section] = {}
keys.each do |key|
obj[section][key] = Setting.send(key.to_sym)
end
end
end
end
end
4 changes: 2 additions & 2 deletions app/policies/application_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ def initialize(user, record)
end

def index?
Zealot::Setting.guest_mode || user_signed_in?
Setting.guest_mode || user_signed_in?
end

def show?
scope.where(id: record.id).exists? || Zealot::Setting.guest_mode || user?
scope.where(id: record.id).exists? || Setting.guest_mode || user?
end

def create?
Expand Down
15 changes: 15 additions & 0 deletions app/views/admin/settings/_form.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ruby:
key = @setting.var
value = @setting.value || Setting.send(key)

.row
.col-md-12
.card
.card-header
h3.card-title = t("admin.settings.#{key}")
= simple_form_for(@setting, url: admin_setting_path(key), method: :patch) do |f|
.card-body
= f.error_notification
= f.text_area :value, value: value, class: 'form-control', rows: 15
.card-footer
= f.button :submit, t('admin.settings.submit.update')
7 changes: 7 additions & 0 deletions app/views/admin/settings/edit.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- content_for :title do
= @title

- content_for :section_title do
= @title

== render 'form'
19 changes: 19 additions & 0 deletions app/views/admin/settings/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- content_for :title do
= @title

- content_for :section_title do
= @title

form.form-horizontal
- @settings.each do |section, keys|
.row
.col-md-12
.card.system_info
.card-header
h3.card-title = t("admin.settings.#{section}")
.card-body
- keys.each do |key, value|
dl.system-info
dt = t("admin.settings.#{key}")
dd
pre = link_to value.to_s, edit_admin_setting_path(key)
2 changes: 1 addition & 1 deletion app/views/devise/registrations/new.html.slim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
- content_for :title do
| 用户注册

- if Zealot::Setting.register_enabled
- if Setting.registrations_mode
.row.mb-3.mt-3
.col-sm-7.order-12.order-sm-1
== render 'devise/shared/intro'
Expand Down
2 changes: 1 addition & 1 deletion app/views/devise/shared/_links.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
p.mb-1
= link_to "登录账户", new_session_path(resource_name)

- if Zealot::Setting.register_enabled && devise_mapping.registerable? && controller_name != 'registrations'
- if Setting.registrations_mode && devise_mapping.registerable? && controller_name != 'registrations'
p.mb-1
= link_to "注册账号", new_registration_path(resource_name)

Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_content.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
section.content
- if user_signed_in?
== render 'layouts/messages'
.container
.container-fluid
== yield
2 changes: 1 addition & 1 deletion app/views/layouts/_content_header.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ruby:
- unless section_title.empty?
section.content-header
.container-fluid
.row
.row.pl-2
.col-sm-6
h1 = yield :section_title
.col-sm-6
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_footer.html.slim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
footer.main-footer
.float-right.d-none.d-sm-block
b Version
= " #{Zealot::Setting.version}"
= " #{Setting.version}"
strong
| Copyright &copy; 2015-2019&nbsp
a href="https://github.com/getzealot/zealot" Zealot
Expand Down
Loading