Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
= Settings Plugin
Settings is a plugin that makes managing a table of global key, value pairs easy. Think of it like a global Hash stored in you database, that uses simple ActiveRecord like methods for manipulation. Keep track of any global setting that you dont want to hard code into your rails app. You can store any kind of object. Strings, numbers, arrays, or any object.
== Setup
Requirements: Rails 3.0.x
You can add this gem in your Gemfile gem 'rails3-settings', :require => 'settings', :git => 'git://github.com/Magicdream/rails-settings.git'
You must create the table used by the Settings model. Simply run this command: rails generate settings_migration
Now just put that migration in the database with: rake db:migrate
If you use some older version of this fork, beware that the database schema needs to be changed ("object_id" renamed to "target_id", "object_type" renamed to "target_type"). You can do this with this migration:
class RenameSettingsFields < ActiveRecord::Migration
def self.up
remove_index :settings, [ :object_type, :object_id, :var ]
rename_column :settings, :object_id, :target_id
rename_column :settings, :object_type, :target_type
add_index :settings, [ :target_type, :target_id, :var ], :unique => true
end
def self.down
# ...
end
end
== Usage
The syntax is easy. First, lets create some settings to keep track of:
Settings.admin_password = 'supersecret' Settings.date_format = '%m %d, %Y' Settings.cocktails = ['Martini', 'Screwdriver', 'White Russian'] Settings.foo = 123 Settings.credentials = { :username => 'tom', :password => 'secret' }
Now lets read them back:
Settings.foo # returns 123
Changing an existing setting is the same as creating a new setting:
Settings.foo = 'super duper bar'
For changing an existing setting which is a Hash, you can merge new values with existing ones: Settings.merge! :credentials, :password => 'topsecret' Settings.credentials # returns { :username => 'tom', :password => 'topsecret' }
Decide you dont want to track a particular setting anymore?
Settings.destroy :foo Settings.foo # returns nil
Want a list of all the settings?
Settings.all # returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'}
You need name spaces and want a list of settings for a give name space? Just choose your prefered named space delimiter and use Settings.all like this:
Settings['preferences.color'] = :blue Settings['preferences.size'] = :large Settings['license.key'] = 'ABC-DEF' Settings.all('preferences.') # returns { 'preferences.color' => :blue, 'preferences.size' => :large }
Set defaults for certain settings of your app. This will cause the defined settings to return with the Specified value even if they are not in the database. Make a new file in config/initializers/settings.rb with the following:
Settings.defaults[:some_setting] = 'footastic'
Now even if the database is completely empty, you app will have some intelligent defaults:
Settings.some_setting # returns 'footastic'
Settings may be bound to any existing ActiveRecord object. Define this association like this:
class User < ActiveRecord::Base has_settings end
Then you can set/get a setting for a given user instance just by doing this:
user = User.find(123) user.settings.color = :red user.settings.color # returns :red user.settings.all # { "color" => :red }
I you want to find users having or not having some settings, there are named scopes for this:
User.with_settings # => returns a scope of users having any setting User.with_settings_for('color') # => returns a scope of users having a 'color' setting
User.without_settings # returns a scope of users having no setting at all (means user.settings.all == {}) User.without_settings('color') # returns a scope of users having no 'color' setting (means user.settings.color == nil)
That's all there is to it! Enjoy!
FAQs
Unknown package
We found that rails3-settings demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.