Druthers
is the simplest performant solution to storing application-specific settings via
ActiveRecord.
When you use an off-the-shelf Rails application, frequently you'll need to store configuration information specific to your installation.
If you store that information in a flat file, and you subsequently upgrade the rails application, you'll need to make sure your settings don't get overwritten. If you're running multiple servers, you'll need to store those settings in a shared datastore. If you're already using ActiveRecord, we might as well use ActiveRecord to store this.
Druthers is a simple enhancement to a key/value model that adds caching and the ability to override behavior for specific settings.
As opposed to rails-settings
and forks, method_missing
isn't used anywhere. HURRAY!
The latest versions of
- Rails 4.1 & 4.2
- Ruby 2.2
- SQLite, MySQL, and PostgreSQL
Every valid combination is tested via Travis.
Assuming your configuration is stored via a Setting
model:
class Setting < ActiveRecord::Base
# Add class-level getter/setters for your settings:
def_druthers :quest, :favourite_colour
# This is not required, but allows for more than just string values:
serialize :value
# This will be used as the value of `Setting.quest` if it is not set.
def self.default_quest
"to find the holy grail"
end
# This validation will be only run for instances whose key == "favourite_color"
# Note that this isn't a class method, as the validation needs access to the
# instance value.
def validate_favourite_colour
add_error("we're right out of teal, sorry") if value == "teal"
end
end
You can get values:
Setting.quest
=> "to find the holy grail" # <- because we defined default_quest above
Setting.favourite_color
=> nil # <- no default
You can set values:
Setting.favourite_color = "red"
If you want to store more than just strings, use serialize, which is in the above example, and is not required.
Add this line to your application's Gemfile:
gem 'druthers'
And then execute:
$ bundle
Add a model:
rails g model Setting key:string value:text
Edit the resulting migration to add an index. Here's a complete example:
class CreateSettings < ActiveRecord::Migration
def change
create_table :settings do |t|
t.string :key
t.text :value
t.timestamps
end
add_index :settings, [:key], :unique => true, :name => 'key_udx'
end
end
Finally, edit your model, adding has_druthers
and the appropriate defaults and validations.
See the Usage section above for more details.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Add tests and code
- Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request