Skip to content

Coursemology/settings_on_rails

Repository files navigation

Settings on Rails

Build Status Coverage Status

Installation

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

Getting Started

Add database column

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

Declare in model

class Blog < ActiveRecord::Base
  has_settings_on :settings_column
end

Set settings

@blog.settings.title = 'My Space'
@blog.settings(:theme).background_color = 'blue'

@blog.save

Get settings

@blog.settings(:theme).background_color
=> 'blue'

# returns nil if not set
@blog.settings(:post).pagination
=> nil

Defining Default Values

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'

Nested Keys

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'

Multiple Keys

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'

Method Name Customization

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

Contributing

  1. Fork it ( https://github.com/allenwq/settings_on_rails/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages