Skip to content

A settings plugin that allow settings to cascade from the system -> account -> user and be overriden at any level.

Notifications You must be signed in to change notification settings

midas/cascading-settings

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cascading Settings Plugin

This is a settings plugin that allows for a system level, account level and user level setting to set. It can then resolve the settings for the system, account or user level, giving user priority, then account and finally system. It works very similar to cascading permissions.

This is an adaption of the rails-settings plugin by Squeegy at: github.com/Squeegy/rails-settings/tree/master. I thought about forking the plugin, however, decided against it due to the major change in functionality. I did not want to add additional complexity to the rather simple base case for the original plugin. So a complete break makes the most sense. I do want to give Squeegy all the credit for the inspiration and much of the code.

Setup

You must create the table used by the Settings model. Simply run this command:

script/generate cascading_settings_migration

Now just put that migration in the database with:

rake db:migrate

Usage

All functionality from the rails-settings works the same, excluding the all method, which is gone, giving the method back to the ActiveRecord version. Please see github.com/Squeegy/rails-settings/tree/master for details.

In your account and user models:

class Account < ActiveRecord::Base
  settingable
end
class User < ActiveRecord::Base
  settingable
end

Creating a scoped setting:

Setting.per_page = 15                  # sets a system level setting (settingable_type and settingable_id is nil)
Setting[:per_page] = 15                # the same as the last line
Setting[@account => :per_page] = 25    # sets an account level setting (settingable_type = 'Account' and settingable_id = @account.id)
Setting[@user => :per_page] = 30       # sets a user level setting (settingable_type = 'User' and settingable_id = @user.id)

Read it back with:

Setting[:per_page]                      # = 15
Setting[@account => :per_page]          # = 25
Setting.resolve( @account, :per_page )  # The same as the line above
Setting[@user => :per_page]             # = 30
Setting.resolve( @user, :per_page )     # The same as the line above
Setting.resolve_all                     # = {"per_page" => 15}
Setting.resolve_all( @account )         # = {"per_page" => 25}
Setting.resolve_all( @user )            # = {"per_page" => 30}

A little more complex example, the inputs:

Setting.per_page = 15
Setting.only_system = 'system'
Setting[@account => :per_page] = 25
Setting[@account => :only_account] = 'account'
Setting[@user => :per_page] = 30
Setting[@user => :only_user] = 'user'

Read it back:

Setting[:per_page]                    # = 15
Setting[@account => :per_page]        # = 25
Setting[@user => :per_page]           # = 30
Setting[:only_system]                 # = 'system'
Setting[:only_account]                # = nil
Setting[:only_user]                   # = nil
Setting[@account => :only_system]     # = 'system'
Setting[@account => :only_account]    # = 'account'
Setting[@account => :only_user]       # = nil
Setting[@user => :only_system]        # = 'system'
Setting[@user => :only_account]       # = 'account'
Setting[@user => :only_user]          # = 'user'
Setting.resolve_all                   # = {"only_system"=>"system", "per_page"=>15}
Setting.resolve_all( @account )       # = {"only_system"=>"system", "only_account"=>"account", "per_page"=>25}
Setting.resolve_all( @user )          # = {"only_system"=>"system", "only_account"=>"account", "per_page"=>30, "only_user"=>"user"}

So that is pretty much it. I plan on implementing some more convenience methods in the future. I also plan to make the name of the account and user models configurable, etc…

About

A settings plugin that allow settings to cascade from the system -> account -> user and be overriden at any level.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages