Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Easy manageable root tables for Rails

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 app
Octocat-spinner-32 config
Octocat-spinner-32 lib
Octocat-spinner-32 rails
Octocat-spinner-32 spec
Octocat-spinner-32 tasks
Octocat-spinner-32 .gitignore
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 defaults.reek
Octocat-spinner-32 install.rb
Octocat-spinner-32 locale.yml
Octocat-spinner-32 uninstall.rb
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.

Something went wrong with that request. Please try again.