Skip to content

goldenio/golden-setting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Golden Setting

The golden-setting persists settings in database and keeps alive in cache.

A web interface is provied to let users can update easily.

Requirement

For Rails 4 project, Gemfile should have

gem 'devise', '~> 3.0'
gem 'cancancan'
gem 'simple_form', '~> 3.0'
gem 'bootstrap-sass'
gem 'will_paginate'
gem 'golden-theme'

If you add gem 'protected_attributes' in your Gemfile, remember to add

attr_accessible :name, :field_type, :field_values, :group

to your Setting class.

For Rails 3 project, Gemfile should have

gem 'devise', '>= 2.0', '< 3.0'
gem 'cancancan'
gem 'simple_form', '>= 2.0', '< 3.0'
gem 'bootstrap-sass'
gem 'will_paginate'
gem 'golden-theme'

Please make sure these gems are installed and configured properly.

Installation

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 cancancan.

can :index, Setting
can :batch_update, Setting

Configuration

You will need to add these basic configures of golden-setting for your needs.

  • groups for grouping settings with a name
  • default_group for saving settings with a group
  • default_tab for a group of settings shown in tab views
  • before_actions for setting up authentication filters

Edit config/initializers/golden_setting.rb for more detail.

Usage

Create/Read/Update/Destroy settings

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

List settings

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.')

Default settings

You can add virtaul settings for your application by setting defaults.

Add config/initializers/golden_setting_defaults.rb.

Rails.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.

Rails.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'

For Rails 3 project, use YourApp::Application.config.after_initialize instead.

Resourced settings

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

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

MIT

About

Golden Setting persists settings in database and keep alive in cache.

Resources

License

Stars

Watchers

Forks

Packages

No packages published