Skip to content
fjfish edited this page Sep 13, 2010 · 5 revisions

What is it for?

Rather than storing a list of things in the database as strings, e.g. Mr, Mrs and so on, instead we store an integer value that we can look up to get the true value. This is done for a number of reasons, e.g. it is easy to change the meaning of a code without having to update large tables, storing an integer is a lot less space than a string and so on. It is also easy to translate a site if needed, but this plugin isn’t sophisticated enough to support multiple languages. To do that you would need to know the user’s language and have a country code in the lookup table.

It does have some disadvantages in that you need to know what the magic number means and it can make writing queries a pain. The plugin has methods that will help with this.

How to install

script/plugin install git://github.com/fjfish/has_lookup_extension.git

How is it implemented

You have a lookup codes table with a domain column that defines what the lookup is for. The plugin lets you do this:


class Applicant < ActiveRecord::Base
# ...
  has_lookup_code( {
    :title                => 'Person Title',
    :occupation           => 'Occupation',
    :employment_type      => 'Employment Status',
    :credit_rating        => 'Credit Status',
    :residential_status   => 'Residential Status',
    :marital_status       => 'Marital Status',
    :property_style       => 'Property Style',
    :phone_one_type       => 'Phone Type',
    :phone_two_type       => 'Phone Type',
  } )
# ...
end

The symbol is the column name, the string is the arbitrary domain name (can be anything).

This adds some class-level methods, for example there will be Applicant.title_list, Applicant.sorted_title_list and Applicant.title_lookup.

The first two let you do this in your views:


<label for="applicant_title">Title <span class="required">*</span></label> <%= f.select :title, Applicant.sorted_title_list, {}, :class => 'select' %>

The last one lets you turn the integer into the string by using a hash. The plugin adds a convenience method, e.g. title_str, that gives you the string value for an instance.


app = Applicant.find :first app.title_str "mr"

By convention id 0 is ‘Please select’ and this is always first in the sorted list. The other list method gives the codes in whatever the order come out of the hash. If you are creating human-readable exports of you data and don’t want “please select” then use formatted_title_str.

At the class level I also now have


Applicant.title_dr             Applicant.title_mrs
Applicant.title_list           Applicant.title_ms
Applicant.title_lookup         Applicant.title_other
Applicant.title_miss           Applicant.title_please_select
Applicant.title_mr             Applicant.title_rev

Ignoring the xxx_lookup and xxx_list methods, this lets you refer to the codes by name without having to do any magic number hacking.

Set up

The initialisation code will create a migration for you and copy in the model files. It also creates app/helpers/admin/lookupcodes_helper.rb.

Review the codes defined in this file and add any as appropriate.
Run the migration
run rake codes:load
This last can be put into your capistrano file to reload any codes that you may have changed.

Warnings and caveats

Be very careful to add new codes at the end of lists or you will mess up the encoding.

Dependencies

If you have a type and subtype it can be accommodated, but you will need to do do some extra coding yourself. For example if you have employment type and subtype you can do this:


  class << self
    def get_subtype_list_for_id(type_id)
      type_str = LookupCode.get_domain_lookup('Employment Type')[type_id.to_i]
      LookupCode.get_sorted_domain_list("Employment Type: #{type_str} subtype")
    end
    def get_subtype_strings_for_id(type_id)
      type_str = LookupCode.get_domain_lookup('Employment Type')[type_id.to_i]
      LookupCode.get_domain_lookup("Employment Type: #{type_str} subtype")
    end
  end

This means you have to pass in the parent id to get the list, and is a hack, but it does work.