The golden-setting
persists settings in database and keeps alive in cache.
A web interface is provied to let users can update easily.
For Rails 4 project, Gemfile
should have
gem 'protected_attributes'
gem 'devise', '~> 3.0.0.rc'
gem 'cancan'
gem 'simple_form', '~> 3.0.0.rc'
gem 'anjlab-bootstrap-rails', require: 'bootstrap-rails'
gem 'will_paginate'
gem 'rails-theme-helper'
For Rails 3 project, Gemfile
should have
gem 'devise', '>= 2.0', '< 3.0'
gem 'cancan'
gem 'simple_form', '>= 2.0', '< 3.0'
gem 'anjlab-bootstrap-rails', require: 'bootstrap-rails'
gem 'will_paginate'
gem 'rails-theme-helper'
Please make sure these gems are installed and configured properly.
Add golden-setting
to your application's Gemfile.
gem 'golden-setting'
Then bundle gems, generate settings and migrate database.
$ bundle
$ rails generate golden:setting:install setting
$ rake db:migrate
Remember add setting abilities for cancan
.
can :index, Setting
can :batch_update, Setting
You will need to add these basic configures of golden-setting
for your needs.
groups
for grouping settings with a namedefault_group
for saving settings with a groupdefault_tab
for a group of settings shown in tab views
Edit config/initializers/golden_setting.rb
for more detail.
You can add setting with named group.
Setting.add 'google', 'google_analytics_token', 12345
Setting.add 'api', 'golden_api_credentials', { username: 'tsechingho', password: 'secret' }
Setting.add 'theme', 'theme_color', 'golden', :select, "%w(golden white)"
or add setting with blank group.
Setting.google_analytics_token = 12345
Setting.golden_api_credentials = { username: 'tsechingho', password: 'secret' }
Setting.theme_color = 'golden'
Read settings (group regardless).
Setting.google_analytics_token
Setting.golden_api_credentials
Setting.theme_color
Update settings (group regardless).
Setting.google_analytics_token = 'abced'
Setting.merge!(:golden_api_credentials, password: 'topsecret')
Setting.theme_color = 'white'
Destroy settings.
Setting.destroy :google_analytics_token
Setting.destroy :golden_api_credentials
Setting.destroy :theme_color
You can add setting with complex name with []=
method, and retrieve with []
method.
And list settings as key-value pair with/without name prefix.
Setting['preferences.color'] = :blue
Setting['preferences.size'] = :large
Setting['license.key'] = 'ABC-DEF'
Setting['license.key']
Setting.list
Setting.list('preferences.')
You can add virtaul settings for your application by setting defaults.
Add config/initializers/golden_setting_defaults.rb
.
YourApp::Application.config.after_initialize do
Setting.defaults[:administrator] = 'tsechingho'
end
These virtaul settings were not saved in database.
Setting.named('administrator').count # => 0
Setting.administrator # => 'tsechingho'
You can also save default settings in database after initializing your application.
YourApp::Application.config.after_initialize do
Setting.save_default :manager, 'tsechingho'
end
These default settings were saved in database with default group.
Setting.named('manager').count # => 1
Setting.object('manager').group # => 'site'
Setting.manager # => 'tsechingho'
Settings may be bound to any existing ActiveRecord object.
class User < ActiveRecord::Base
include Golden::Setting::Resourced
end
Then you can set or get settings.
user = User.first
user.settings.theme_color = 'golden'
user.settings.theme_color # => 'golden'
user.settings.list # => { "theme_color" => 'golden' }
You can find users having or not having some settings by (experimental) scopes.
User.with_settings.to_a.size # => 1
User.with_settings_for('theme_color').count # => 1
User.without_settings.count
- 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
MIT