Intenté https://github.com/huacnlee/rails-settings-cached, pero no funciona según lo descrito. Obviamente, el autor olvidó mencionar algunos ajustes adicionales en la descripción del uso de gemas. No pude escribir un controlador para la manipulación de configuraciones.
En su lugar, tuve éxito en utilizar https://github.com/paulca/configurable_engine - a pesar de algunos problemas menores, esta gema es mucho más razonable que rails-settings-cached
.
La gema configurable_engine
tiene un inconveniente: tiene las rutas codificadas que son oscuras y no convenientes. El autor de la gema promised to correct it, pero dijo que actualmente no tenía tiempo para eso.
Así que este problema se solucionó fácilmente simplemente creando mis propias rutas. Aquí está mi código (añadido para hacer esta joya realmente el trabajo):
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 %>
Debido a la modificable rutas mi controlador no es completamente REST-obediente, b ut es bastante cercano a. Mi acción new
realmente crea una configuración (almacenada en la base de datos) (solo para anular su valor de archivo yml).
Por lo tanto, este código agregado al código de descripción de gemas le permite utilizar prácticamente las configuraciones de RoR cambiables en tiempo de ejecución.
La gema requiere que establezca previamente algunos valores predeterminados en un archivo yml, que puede anular más adelante en el tiempo de ejecución. Pero no puede crear una nueva configuración en tiempo de ejecución (no-yml-file-existente); solo modifique una existente (en archivo yml), lo cual es bastante lógico.
O puede restaurar (en el tiempo de ejecución) el valor predeterminado de cualquier configuración (borrando su valor sobrescrito de la base de datos).
Este código se comprobó que trabajar con rieles 5.
Acabo de publicar una respuesta con un enfoque que mantiene la configuración en la base de datos, las actualiza según sea necesario y no sufre problemas de rendimiento sin recurrir al almacenamiento en caché de material de BD. – silverdr