iain / root_table

Easy manageable root tables for Rails

This URL has Read+Write access

name age message
file .gitignore Sat Jul 25 06:49:59 -0700 2009 ignore svn [iain]
file README.rdoc Mon Jul 20 11:53:14 -0700 2009 Remove magic for development env to work [iain]
file Rakefile Wed Jul 15 17:01:15 -0700 2009 Initial commit [iain]
directory app/ Sat Jul 25 07:06:34 -0700 2009 fix unloadable module problem [iain]
directory config/ Sun Jul 19 03:28:01 -0700 2009 more refactoring out of namespace [iain]
file defaults.reek Thu Jul 16 05:02:23 -0700 2009 refactor to use a class to be referenced by views [iain]
file install.rb Wed Jul 15 17:01:15 -0700 2009 Initial commit [iain]
directory lib/ Sat Jul 25 08:05:11 -0700 2009 Add specs [iain]
file locale.yml Sun Jul 19 04:23:08 -0700 2009 edit and new page are per table overridable [iain]
directory rails/ Sun Jul 19 06:11:57 -0700 2009 Add root_table_select [iain]
directory spec/ Sat Jul 25 08:05:11 -0700 2009 Add specs [iain]
directory tasks/ Wed Jul 15 17:01:15 -0700 2009 Initial commit [iain]
file uninstall.rb Wed Jul 15 17:01:15 -0700 2009 Initial commit [iain]
README.rdoc

Root Table

Specifies so-called "root-tables" for inclusion with other models. Handy if you need to have a user manageable list in a dropdown.

Integrates well with acts_as_list.

Has an Rails enging interface to manage all defined root tables.

See also the blogpost about the release: iain.nl/2009/07/root-table/

Installation

  • Install root_table:
      ./script/plugin install git://github.com/iain/root_table.git
    
  • Install acts_as_list (optional):
      ./script/plugin install git://github.com/rails/acts_as_list.git
    

Usage

In short:

  • Create the model that has an item to be selected from a list. Add the foreign key as if you were to define a normal belongs_to-relation.
  • Create a model which will be the list to choose from, include a name field and optionally a position field, when working with ActsAsList.
  • Add to that model root_table_for.
  • Add to the receiving model has_root_table.
  • Use "f.root_table_select" in you’re forms.

Example

We’ll create products that can have a category to choose from.

  • Create the target model (Product):
      ./script/generate model Product name:string category_id:integer
    
  • Create the root table (Category):
      ./script/generate model Category name:string position:integer
    
  • Migrate
  • Edit the category model:
      class Category < ActiveRecord::Base
        root_table_for :product
      end
    
  • Edit the product model:
      class Product < ActiveRecord::Base
        has_root_table :category
      end
    

This has the following results:

  • The field name is now validated for presence and uniqueness
  • "Category has many products" and "Product belongs to category" relations are added
  • Product#category_name delegates to Category#name
  • Category acts as list
  • Default scope of category is :order => :position

Now, let’s create that drop down in your product form:

  <% form_for @product do |f| %>
    <p>
      <%= f.label :category_id %><br />
      <%= f.root_table_select :category, :include_blank => true %>
    </p>
  <% end %>

And display it, through it’s delegated method:

  <%= @product.categegory_name %>

Rails engine

There is an interface for managing all you’re root tables. Go to the url localhost:3000/root_tables to see a list of all root tables defined. Clicking on one of these tables takes you to a scaffold like page where you can also drag and drop the list if you have installed it.

Also it is fully internationalized. Check out locale.yml on how which keys are used.

You can of course override these files to you’re liking, by creating them in your app as well.

Any view is overridable per table as well. To have a different form for new categories, for example, define app/views/root_tables/new_category.html.erb and fill it up. Do have a look at the original code for inspiration.

Configuration

The definition takes a number of options, which are all optional:

  • field - Which field of the root table is the displayed value. The default is :name.
  • to - How is the relation called? On the target model you would normally say "belongs_to :foo, :foreign_key => "bar". Specify what :foo would be, if different from the convention.
  • foreign_key - Just as you would specify the foreign_key in the belongs_to relation, if different from the relation name.
  • order - Default order of the root table. Will be used for acts_as_list, if installed. Without acts_as_list, this defaults to :name (or whatever you set field to), with acts_as_list installed it will default to :position.
  • validate - Set to false to disable automatic validations. Defaults to validating presence and uniqueness of field
  • acts_as_list - Set to false if you have acts_as_list installed, but don’t want to use it in this case.

Visit iain.nl/ for more information.


Copyright © 2009 Iain Hecker. Released under the MIT License.