Provides a nice UI interface for rails-settings-cached gem in Active Admin.
Version 3 supports rails-settings-cached v2 (>= 2.8.2). For rails-settings-cached < 0.6.7 use version 2.x of this gem.
Add this line to your application's Gemfile:
gem 'activeadmin_settings_cached'And then execute:
$ bundle
Create your settings model:
$ rails g settings:install
$ bundle exec rake db:migrate
Create your settings page:
# From generators
$ rails g active_admin:settings Setting
# Or manual
# app/admin/setting.rb
ActiveAdmin.register_page 'Setting' do
title = 'Settings'
menu label: title
active_admin_settings_page(
title: title
)
endAnd define your fields in your Setting model:
class Setting < RailsSettings::Base
field :app_name, default: 'My app'
scope :mail do
field :admin_emails, type: :array, default: %w[admin@example.com]
field :notifications_enabled, type: :boolean, default: true
field :default_locale, default: 'en', option_values: %w[en de ru], help_text: 'Language of emails'
end
field :api_key, default: ENV['API_KEY'], readonly: true
endIn your application's admin interface, there will now be a new page with these settings.
Every editable field is shown. Readonly fields are not shown. Fields are grouped by scope.
The input depends on the field type:
| Type | Input |
|---|---|
:string |
text field |
:integer, :float, :big_decimal |
number field |
:boolean |
checkbox |
:array |
textarea, one value per line |
:hash |
textarea with YAML |
A field with option_values is shown as a select. A field with help_text shows it as a hint.
Validation errors from validates are shown in the flash message.
Only changed values are saved, so unchanged fields keep their default value.
ActiveAdmin.register_page 'Settings' do
menu label: 'Settings', priority: 99
active_admin_settings_page options
end| Tool | Description |
|---|---|
| :model_name | String, settings model name override (default: uses name from global config.) |
| :scope | Symbol, Array of Symbols, show only fields from these scopes (default: nil, all fields) |
| :template | String, custom template rendering (default: 'admin/settings/index') |
| :template_object | object passing to view (default: ActiveadminSettingsCached::Model instance) |
| :display | Hash, display settings override (default: {}) |
| :title | String, title value override (default: I18n.t('settings.menu.label')) |
| :after_save | Proc, callback for action after POST request, (default: nil) |
ActiveAdmin.register_page 'Mail Settings' do
active_admin_settings_page(scope: :mail, title: 'Mail Settings')
endYou can localize settings keys in local file
en:
settings:
attributes:
my_awesome_settings: 'My Awesome Localized Setting'
scopes:
mail: 'Mail'By default the name of the mode is Setting. If you want to use a different name for the model, you can specify your that in config/initializers/active_admin_settings_cached.rb:
ActiveadminSettingsCached.configure do |config|
config.model_name = 'AdvancedSetting'
endIf you need to override the input for a field, eg textarea, url or :timestamp and etc., you can set display option in initializer.
ActiveadminSettingsCached.configure do |config|
config.display = {
my_awesome_setting_name: :text,
my_awesome_setting_name_2: :timestamp,
my_awesome_setting_name_3: :select
}
endAvailable options see here
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
make bash
make setup
cd spec/rails/rails-8.0.*/
BUNDLE_GEMFILE=/app/gemfiles/rails8.0.gemfile bundle exec rails s -b 0.0.0.0
- Update rails-settings-cached to v2 and define your settings with
field. starting_withandkeyoptions are removed. Use rails-settings-cachedscopeand thescopeoption.- Values are coerced by rails-settings-cached field types.
displayonly changes the input.