Git Product home page Git Product logo

guniorobot / rails-settings Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ledermann/rails-settings

1.0 2.0 0.0 154 KB

Settings is a plugin that makes managing a table of key/value pairs easy. Think of it like a Hash stored in you database, that uses simple ActiveRecord like methods for manipulation. Keep track of any setting that you don't want to hard code into your rails app. You can store any kind of object: Strings, numbers, arrays, or any object which can be noted as YAML.

Home Page: http://github.com/ledermann/rails-settings

License: Other

rails-settings's Introduction

Settings Gem/Plugin for Rails

Build Status

Settings is a gem/plugin that makes managing a table of key/value pairs easy. Think of it like a Hash stored in you database, that uses simple ActiveRecord like methods for manipulation. Keep track of any setting that you don't want to hard code into your rails app. You can store any kind of object: Strings, numbers, arrays, or any object which can be noted as YAML.

Requirements

ActiveRecord 2.3.x, 3.0.x or 3.1.x

Tested with Ruby 1.8.7, 1.9.2, 1.9.3 and RBX2.0

Installation

Include the gem in your Gemfile

gem 'ledermann-rails-settings', :require => 'rails-settings'

or install as a plugin:

./script/plugin install git://github.com/ledermann/rails-settings.git

You have to create the table used by the Settings model by using this migration:

class CreateSettingsTable < ActiveRecord::Migration
  def self.up
    create_table :settings, :force => true do |t|
      t.string  :var,         :null => false
      t.text    :value
      t.integer :target_id
      t.string  :target_type, :limit => 30
      t.timestamps
    end

    add_index :settings, [ :target_type, :target_id, :var ], :unique => true
  end

  def self.down
    drop_table :settings
  end
end

Now update your database with:

rake db:migrate

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
# => 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
# => { :username => 'tom', :password => 'topsecret' }

Decide you dont want to track a particular setting anymore?

Settings.destroy :foo
Settings.foo
# => nil

Want a list of all the settings?

Settings.all
# => { '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.')
# => { '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
# => '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
# => :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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.