Stores and retrieves settings on an ActiveRecord class, with support for application and per record settings.
Add this line to your application's Gemfile:
gem 'smart_settings'
And then execute:
$ bundle
Or install it yourself as:
$ gem install smart_settings
Then run the settings generator that will create a migration and a Setting
model:
rails g smart_settings:install
And finally run the migrations:
rails db:migrate
To create an new setting you can use the setting generator. The format of the generator arguments is name attribute:type:default:group
. To generate a setting with the name email:
rails g smart_settings:setting email sender:string:[email protected] domain:string:website.com:smtp user:string:[email protected]:smtp password:string::smtp
The command above will generate the EmailSettings
class inside the app/settings
folder:
class EmailSettings < SmartSettings::Base
setting :sender, :string, default: '[email protected]'
setting :domain, :string, default: 'website.com', group: :smtp
setting :user, :string, default: '[email protected]', group: :smtp
setting :password, :string, group: :smtp
end
Then you can use the Setting
model or the EmailSettings
class to get and set attributes:
# Get email settings using the Setting model querying
email = Setting.find(:email)
# Get email settings using the Setting model methods
email = Setting.email
# Get email settings using EmailSettings class
email = EmailSettings
# Get all setting attributes
email.all # { sender: "[email protected]", smtp: { domain: "website.com", user: "[email protected]", password: nil } }
# Get all setting group attributes
email.smtp # { domain: "website.com", user: "[email protected]", password: nil }
# Get setting specific attributes
email.sender # "[email protected]"
email.smtp_user # "[email protected]"
# Update setting attributes and save them in the settings table
email.update sender: "[email protected]", smtp_user: "[email protected]"
# Get setting updated attributes
email.sender # "[email protected]"
email.smtp_user # "[email protected]"
The Setting
model that is created with the install generator and the settings classes the are created with the setting generator, use the tableless gem to act like ActiveRecord models. This makes it easy to create CRUD controllers and views like you would do with any model:
class SettingsController < ApplicationController
# Show and edit actions are omitted from the example since they usually are empty
# New and create actions cannot be used since you cannot create new settings
before_action :set_setting, only: [:show, :edit, :update, :destroy]
def index
@setting = Setting.all
end
def update
if @setting.update(setting_params)
redirect_to setting_path(@setting), notice: 'Setting was successfully updated.'
else
render :edit
end
end
def destroy
@setting.destroy
redirect_to request.referrer, notice: 'Setting was successfully reset to defaults.'
end
private
def set_setting
@setting = Setting.find(params[:id])
end
def setting_params
params.require(:setting).permit(@setting.permitted_attributes)
end
end
- Add support for record settings
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
Bug reports and pull requests are welcome on GitHub at https://github.com/hardpixel/smart-settings.
The gem is available as open source under the terms of the MIT License.