-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #245 from getzealot/refactor/store-setting-into-db
配置支持数据库读取和更新
- Loading branch information
Showing
44 changed files
with
2,080 additions
and
3,236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,5 @@ | |
section.content | ||
- if user_signed_in? | ||
== render 'layouts/messages' | ||
.container | ||
.container-fluid | ||
== yield |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.