Git Product home page Git Product logo

constantrecord's Introduction

ConstantRecord

A tiny ActiveRecord substitute for small, never changing database tables.

*** Works with Rails 2 and Rails 3.0, but not with Rails 3.1 and above! ***

To get this gem working with the new versions of Rails (which means integration into the ARel mechanism), a complete rewrite would be necessary. Apologies, but please understand that I don’t find the time for this. Thanks!

Usage

class Currency < ConstantRecord::Base
  data 'EUR', 'USD', 'CAD', 'GBP', 'CHF'
end

or

class MoreDetailedCurrency < ConstantRecord::Base
  columns :name, :description
  data ['EUR', 'Euro'],
       ['USD', 'United States dollar'],
       ['CAD', 'Canadian dollar'],
       ['GBP', 'British pound sterling'],
       ['CHF', 'Swiss franc']
end

Inside ActiveRecord, it works just like a “normal” ActiveRecord association:

class Invoice < ActiveRecord::Base
  belongs_to :currency   # via currency_id database column
  validates_inclusion_of :currency_id, :in => Currency.ids

  def print_currency
    # Currency#name is the default attribute
    puts self.currency.name if self.currency
  end
end

ConstantRecord is meant as a substitute for ActiveRecord for tables, that will never change in the lifespan of a mongrel process. I.e. an application might let the user select a state of the US. Most applications will never have a feature where a user/admin can edit the list of US states. They are considered constant.

In my current application, my main/parent class has about 35 child classes, that use over 40 different constant classes. The use of ConstantRecord meant an estimated 15 to 20 per cent speed gain for my application.

ConstantRecord classes don’t need associations:

class CanadianProvince < ConstantRecord::Base
  data 'Alberta', ...
  has_many :people  # is not necessary, will not work
end

If you want to fetch all people living in Alberta, use:

Person.find(:all, :conditions =>
  {:province_id => CanadianProvince.find_by_name('Alberta').id}

or, in Rails 3:

Person.where(:province_id => CanadianProvince.find_by_name('Alberta').id)

If you ever think, ConstantRecord lacks key features of ActiveRecord, (submit a patch or) create the table and derive the class from ActiveRecord instead. This is a key design objective for ConstantRecord.

Features

Currency.find(1)              >>  #<Currency:0x332cbf4 @id=1, @name="EUR">

Just as in a database table, #id starts with 1. When data is a simple Array, #name is the default attribute.

MoreDetailedCurrency.find(3)  >>  #<MoreDetailedCurrency:0x2c8d94c 
                                    @description="Canadian Dollar", 
                                    @id=3, @name="CAD">

Currency.find_by_name('CHF')  >>  #<Currency:0x2c86ef8 @id=3, @name="CHF">

MoreDetailedCurrency.find_by_name('CAD')
                              >>  #<MoreDetailedCurrency:0x2c8d94c 
                                    @description="Canadian Dollar", 
                                    @id=3, @name="CAD">

Currency.count :all           >>  5

As a matter of fact, a ConstantRecord will often be used as a select field in an HTML form. Therefore it comes with a special feature, that is not ActiveRecord compatible. In a Form you can use:

<%= f.select :currency_id, Currency.options_for_select %>

or

<%= f.select :currency_id, Currency.options_for_select(:include_null => true) %>

If you want to store the name instead of the ID, then you can use:

<%= f.select :currency_id, Currency.options_for_select(:value => :name) %>

In this case, you must also change your validation:

class Invoice < ActiveRecord::Base
  validates_inclusion_of :currency_id, :in => Currency.names
end

Sample Data

This Gem features sample data for U.S. states, Canadian provinces and territories and German Bundesländer. Check out the sample directory!

Copyright © 2009 Christoph Petschnig. See LICENSE for details.

Thanks to Till Salzer, Torsten Schönebaum, Nate Wiger and Eric Lindvall for contributions!

constantrecord's People

Contributors

eric avatar tsalzer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.