Tôi đã thử https://github.com/huacnlee/rails-settings-cached, nhưng nó không hoạt động như được mô tả. Rõ ràng là tác giả quên đề cập đến một số điều chỉnh bổ sung trong mô tả sử dụng đá quý. Tôi không thể viết một bộ điều khiển cho thao tác cài đặt.
Thay vào đó tôi đã thành công để sử dụng https://github.com/paulca/configurable_engine - mặc dù một số vấn đề nhỏ, đá quý này là hợp lý hơn nhiều so với rails-settings-cached
.
Đá quý configurable_engine
có một nhược điểm: nó có các tuyến đường được mã hóa cứng không rõ ràng và không thuận tiện. Tác giả của đá quý promised to correct it, nhưng nói rằng ông hiện không có thời gian cho nó.
Vì vậy, vấn đề này đã được sắp xếp dễ dàng bằng cách đơn giản tạo tuyến đường của riêng tôi. Đây là mã của tôi (thêm vào làm cho viên ngọc này thực sự làm việc):
routes.rb
namespace :admin do
resources :configurables, only: [:index, :show, :edit, :update, :destroy]
end
admin/configurables_controller.rb
class Admin::ConfigurablesController < Admin::ApplicationController
# include the engine controller actions
include ConfigurableEngine::ConfigurablesController
before_action :set_configurable, only: [:show, :edit, :update, :destroy]
def index
@configurables = (Configurable.all.size > 0 ? Configurable.all : []) +
(Configurable.defaults.keys - Configurable.all.collect { |c| c.name })
end
def show
end
def edit
new = params[:new]
end
def new
respond_to do |format|
name = params[:name]
if name
@configurable = Configurable.create!(name: name, value: nil)
if @configurable
format.html { redirect_to edit_admin_configurable_path(@configurable, new: true), notice: 'The setting was successfully created.' }
else
format.html { redirect_to admin_configurables_url, notice: 'Failed to create the setting.' }
end
else
format.html { redirect_to admin_configurables_url, notice: 'The name of the new setting was not specified.' }
end
end
end
def update
respond_to do |format|
if @configurable.update(configurable_params)
format.html { redirect_to [:admin, @configurable], notice: 'The setting was successfully updated.' }
format.json { render :show, status: :ok, location: @configurable }
else
format.html { render :edit }
format.json { render json: @configurable.errors, status: :unprocessable_entity }
end
end
end
def destroy
@configurable.destroy
respond_to do |format|
format.html { redirect_to admin_configurables_url, notice: 'The setting was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_configurable
@configurable = Configurable.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def configurable_params
params.require(:configurable).permit(:name, :value)
end
end
index.html.erb
<h1 class="page-header">Settings</h1>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @configurables.each do |configurable| %>
<tr>
<% if configurable.try(:name) %>
<td><%= Configurable.defaults[configurable.name][:name]%></td>
<td></td>
<td><%= link_to 'Show', [:admin, configurable] %></td>
<td><%= link_to 'Edit', edit_admin_configurable_path(configurable) %></td>
<td><%= link_to 'Destroy', [:admin, configurable], method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% else %>
<td><%= Configurable.defaults[configurable][:name] %></td>
<td><%= link_to 'Create', new_admin_configurable_path(name: configurable) %></td>
<td colspan="3"></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
</div>
edit.html.erb
<h1>Editing <%= @new ? "new " : "" %>setting</h1>
<%= render 'form', configurable: @configurable %>
<%= link_to 'Show', [:admin, @configurable] %> |
<%= link_to 'Back', admin_configurables_path %>
show.html.erb
<p>
<strong>Name:</strong>
<%= Configurable.defaults[@configurable.name][:name] %>
</p>
<p>
<strong>Value:</strong>
<%= @configurable.value %>
</p>
<%= link_to 'Edit', edit_admin_configurable_path(@configurable) %> |
<%= link_to 'Back', admin_configurables_path %>
_form.html.erb
<%= form_for([:admin, configurable]) do |f| %>
<div class="field">
<%= f.label "Name" %>
<%= Configurable.defaults[@configurable.name][:name] %>
</div>
<div class="field">
<%= f.label "Value" %>
<%= f.text_area :value %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
Do mã hóa cứng các tuyến điều khiển của tôi không hoàn toàn tuân thủ REST, b ut nó khá gần. Hành động new
của tôi thực sự tạo cài đặt (được lưu trữ cơ sở dữ liệu) (chỉ để ghi đè giá trị tệp yml của nó).
Vì vậy, mã này được thêm vào mã mô tả đá quý cho phép bạn thực tế sử dụng cài đặt RoR có thể thay đổi theo thời gian chạy.
Đá quý yêu cầu bạn đặt một số giá trị mặc định trong tệp yml trước, bạn có thể ghi đè lên sau này tại thời gian chạy. Nhưng bạn không thể tạo một thiết lập mới tại thời gian chạy (không-yml-file-existent) - chỉ sửa đổi một tệp tồn tại (trong tệp yml) - điều này khá hợp lý.
Hoặc bạn có thể khôi phục (tại thời gian chạy) giá trị mặc định của bất kỳ cài đặt nào (bằng cách xóa giá trị ghi đè được lưu trữ cơ sở dữ liệu của nó).
Mã này đã được kiểm tra để hoạt động với Rails 5.
Chỉ cần đăng câu trả lời bằng cách tiếp cận giữ các cài đặt trong cơ sở dữ liệu, cập nhật chúng khi cần và không bị các vấn đề về hiệu năng mà không cần phải lưu vào bộ nhớ đệm DB. – silverdr