If you are using Bundler, add this line to your application's Gemfile:
gem 'settings_on_rails'
And then execute:
$ bundle
Alternatively, install it by running:
$ gem install settings_on_rails
Start by adding a text field to the model on which you want settings:
rails g migration add_settings_column_to_blogs settings_column:text
class Blog < ActiveRecord::Base
has_settings_on :settings_column
end
@blog.settings.title = 'My Space'
@blog.settings(:theme).background_color = 'blue'
@blog.save
@blog.settings(:theme).background_color
=> 'blue'
# returns nil if not set
@blog.settings(:post).pagination
=> nil
class Blog < ActiveRecord::Base
has_settings_on :column
has_settings do |s|
s.key :theme, defaults:{ background_color: 'red', text_size: 50 }
s.attr :title, default: 'My Space'
end
end
OR
class Blog < ActiveRecord::Base
has_settings_on :column do |s|
s.key :theme, defaults: { background_color: 'red', text_size: 50 }
s.attr :title, default: 'My Space'
end
end
You can get these defaults by:
@blog.settings(:theme).background_color
=> 'red'
@blog.settings(:theme).text_size
=> 50
@blog.settings.title
=> 'My Space'
Settings on Rails supports nested keys by chaining calls to the settings
method:
# Set
@blog.settings(:theme).settings(:homepage).background_color = 'white'
# Get
@blog.settings(:theme).settings(:homepage).background_color
=> 'white'
You can also define multiple keys in the following way, this is equivalent to nested keys:
# Set
@blog.settings(:theme, :homepage).background_color = 'white'
# Get
@blog.settings(:theme, :homepage).background_color
=> 'white'
You can customize the name of the settings
method:
class Blog < ActiveRecord::Base
has_settings_on :settings_column, method: :preferences
end
Which allows you to do:
@blog.preferences(:theme).background_color
- Fork it ( https://github.com/allenwq/settings_on_rails/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request