2014-03-16 02:08:15 +01:00
|
|
|
class Admin::ConfigsController < Admin::BaseController
|
|
|
|
before_action :get_tabs, only: [:show, :list]
|
|
|
|
|
|
|
|
def show
|
|
|
|
@current_tab = @tabs.include?(params[:tab]) ? params[:tab] : @tabs.first
|
|
|
|
@cfg = FoodsoftConfig
|
|
|
|
end
|
|
|
|
|
|
|
|
def list
|
|
|
|
@current_tab = 'list'
|
|
|
|
@cfg = FoodsoftConfig
|
|
|
|
@dfl = FoodsoftConfig.config
|
2021-03-01 15:27:26 +01:00
|
|
|
@keys = FoodsoftConfig.keys.select { |k| FoodsoftConfig.allowed_key?(k) }.sort
|
2014-03-16 02:08:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2014-11-22 00:33:16 +01:00
|
|
|
parse_recurring_selects! params[:config][:order_schedule]
|
2014-03-16 02:08:15 +01:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
# TODO support nested configuration keys
|
|
|
|
params[:config].each do |key, val|
|
2020-03-23 16:18:58 +01:00
|
|
|
FoodsoftConfig[key] = convert_config_value val
|
2014-03-16 02:08:15 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
flash[:notice] = I18n.t('admin.configs.update.notice')
|
|
|
|
redirect_to action: 'show'
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# Set configuration tab names as `@tabs`
|
|
|
|
def get_tabs
|
2020-08-07 01:03:00 +02:00
|
|
|
@tabs = %w(foodcoop payment tasks messages layout language security others)
|
2014-03-16 02:08:15 +01:00
|
|
|
# allow engines to modify this list
|
|
|
|
engines = Rails::Engine.subclasses.map(&:instance).select { |e| e.respond_to?(:configuration) }
|
|
|
|
engines.each { |e| e.configuration(@tabs, self) }
|
|
|
|
@tabs.uniq!
|
|
|
|
end
|
|
|
|
|
2014-11-22 00:33:16 +01:00
|
|
|
# turn recurring rules into something palatable
|
|
|
|
def parse_recurring_selects!(config)
|
|
|
|
if config
|
2015-09-23 22:38:20 +02:00
|
|
|
for k in [:pickup, :boxfill, :ends] do
|
2015-10-23 14:53:01 +02:00
|
|
|
if config[k]
|
|
|
|
# allow clearing it using dummy value '{}' ('' would break recurring_select)
|
|
|
|
if config[k][:recurr].present? && config[k][:recurr] != '{}'
|
|
|
|
config[k][:recurr] = ActiveSupport::JSON.decode(config[k][:recurr])
|
|
|
|
config[k][:recurr] = FoodsoftDateUtil.rule_from(config[k][:recurr]).to_ical if config[k][:recurr]
|
|
|
|
else
|
|
|
|
config[k] = nil
|
|
|
|
end
|
2014-11-22 00:33:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-23 16:18:58 +01:00
|
|
|
def convert_config_value(value)
|
|
|
|
if value.is_a? ActionController::Parameters
|
2021-03-01 15:27:26 +01:00
|
|
|
value.transform_values { |v| convert_config_value(v) }.to_hash
|
2020-03-23 16:18:58 +01:00
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
2014-03-16 02:08:15 +01:00
|
|
|
end
|