Giter Site home page Giter Site logo

configurator's Introduction

Configurator

Expalanation

Unleash your models and quickly and easily annotate anything. Store booleans, strings, or optionally serialized objects (hashes, custom classes, whatever) without extra migrations.

Configurator is meant to store basic things. There’s no easy way to query for models that match a particular criteria, so don’t go overboard. This satisfies some of my needs, but I don’t recommend relying on it as a replacement for traditional model fields.

Want your users to be able to define custom settings?

class User < ActiveRecord::Base
  include Configurator
end

Basics

Now you can do things like:

@user.config[:receive_email_alerts?] = true

or

@user.config[:notification_address] = '[email protected]'

You can set configurations to either instances or classes. Setting key/value pairs to classes is especially useful for setting up application-wide settings.

Think of it as just a giant hash.

@user.config = { :favorite_animal => 'dog', :favorite_color => 'blue' }

Namespaces

Support for one level of namespacing:

@user.config[:animals, :favorite] = 'cat'

Namespaces within hash assignments:

@user.config = { :animals => { :favorite => 'cat', :likes_elephants? => true }, :artists => { :favorite => 'Radiohead' } }

Querying namespaces:

@user.config = { :animals => { :cat => 'Toby', :dog => 'Gabby' } }
@user.config.namespace(:animals)  # => { :cat => 'Toby', :dog => 'Gabby' }

Form support

Easy to use in views:

<% fields_for :config, @user.config do |c| %>

  <%= c.select :favorite_color, %w(red green blue) %>

<% end %>

Default Options

Databases don’t come filled, so there’s an easy way to set defaults on your models. The default values will be added to the config table used by the plugin when loaded.

class User < ActiveRecord::Base
  include Configurator

  default_configuration :favorite_color => 'red', :receive_email_alerts? => true, :salary => { :default_for_manager => '$55,000', :default_for_employee => '$25,000' }
end

@user.config[:favorite_color] # => 'red'
@user.config[:favorite_color] = 'green'
@user.config[:favorite_color] # => 'green'

Global Settings

Sometimes you don’t want to be restricted to configuring records, and would like to apply Configurator to a class or to something even more global. Well, now you can.

Per Model

User.config[:notification_email] = 'Welcome new user!'

Globally

Configurator[:default_notification_email] = 'Welcome to our website!'

Example of a database driven view layer:

<h1><%= Configurator[:login_page, :headline] %></h1>

<p><%= Configurator[:login_page, :username] %></p>
<%= text_field_tag :username %>

<p><%= Configurator[:login_page, :password] %></p>
...

Simply store the above values, and you’re now able to quickly attach a form to those different values, and satisfy your clients need to have every single aspect of the application. I’m sure there are plugins that do just this, but this is an example of Configurators global reach.

Setup

First, install the gem. If you’re using Rails:

config.gem 'ar-configurator', :lib => 'configurator'

Or just:

gem install configurator
  • Run the config_table generator

  • Migrate your database

  • Include Configurator into the models you need it in, and that’s it.

If you need to be able to store complex objects, or strings greater than 255 characters, change the ‘value’ column to text. You can add to the ConfigurationHash class:

serialize :value

I haven’t tried this yet, but it should work fine.

Contributors

Brennan Dunn, Kyle Bolton, Jacek Becela

Copyright © 2009 Brennan Dunn, released under the MIT license

configurator's People

Contributors

brennandunn avatar kb avatar ncr avatar

Watchers

 avatar

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.