Skip to content

patrickespake/tableless_model

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tableless_model

Ruby on Rails model without database.

With all the power of ActiveRecord and validations.

The main utility is used in forms that do not need a database, but needs the validation of ActiveRecord, form_for and RESTful routes. For example contact form.

Instalation


./script/plugin install git://github.com/patrickespake/tableless_model.git

Generators

tableless_model Generator

Stubs out a new tableless model. Pass the tableless model name, either CamelCased or under_scored, and an optional list of attribute pairs as arguments.

Attribute pairs are column_name:sql_type arguments specifying the tableless model’s attributes.

You don’t have to think up every attribute up front, but it helps to sketch out a few so you can start working with the tableless model immediately.

This generates a tableless model class in app/models, a unit test in test/unit, a test fixture in test/fixtures/singular_name.yml.

Examples:


./script/generate tableless_model contact

creates an Contact model, test, and fixture:

  • Model: app/models/contact.rb
  • Test: test/unit/contact_test.rb
  • Fixtures: test/fixtures/contacts.yml

./script/generate tableless_model invite name:string body:text email:string

creates a Invite tableless model with a string name, text body, and string email.

tableless_scaffold Generator

Tableless scaffolds an entire resource, from model, controller and views, along with a full test suite. The resource is ready to use as a starting point for your RESTful, resource-oriented application.

Pass the name of the model (in singular form), either CamelCased or under_scored, as the first argument, and an optional list of attribute pairs.

Attribute pairs are column_name:sql_type arguments specifying the model’s attributes.

You don’t have to think up every attribute up front, but it helps to sketch out a few so you can start working with the resource immediately.

For example, ‘tableless_scaffold contact name:string telephone:string firm:string email:string message:text’
gives you a model with those three attributes, a controller that handle the create, form to create your contacts, as well as a map.resources :contacts declaration in config/routes.rb.

If you want to remove all the generated files, run ‘script/destroy scaffold ModelName’.

Examples:


./script/generate tableless_scaffold invite


./script/generate tableless_scaffold contact name:string telephone:string firm:string email:string message:text

Types of columns available in the tableless_model


class ModelName < TablelessModel
  column :column_name1, :column_type
  column :column_name2, :column_type
  column :column_name3, :column_type
  column :column_nameN, :column_type
  ...
end

Types of columns (column_type) available:

  • :string
  • :text
  • :integer
  • :float
  • :decimal
  • :datetime
  • :timestamp
  • :time
  • :date
  • :binary
  • :boolean

ActiveRecord validations working perfectly


class Contact < TablelessModel
  column :name,      :string
  column :telephone, :string
  column :firm,      :string
  column :email,     :string
  column :message,   :text

  validates_presence_of :name, :email, :message
  validates_format_of   :email, :with => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
  validates_length_of   :message, :minimum => 3
end

Full Example Using tableless_scaffold Generator

Create tableless scaffold


./script/generate tableless_scaffold contact name:string telephone:string firm:string email:string message:text

Add validations in the model

Open the file app/models/contact.rb and add the validations:


class Contact < TablelessModel
  column :name, :string
  column :telephone, :string
  column :firm, :string
  column :email, :string
  column :message, :text

  # Validations
  validates_presence_of :name, :email, :message
  validates_format_of   :email, :with => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
  validates_length_of   :message, :minimum => 3
end

Run


./script/server

Open your browser in: http://localhost:3000/contacts/new

Full Example Using tableless_model Generator

Create tableless model


./script/generate tableless_model contact name:string telephone:string firm:string email:string message:text

Add validations in the model

Open the file app/models/contact.rb and add the validations:


class Contact < TablelessModel
  column :name, :string
  column :telephone, :string
  column :firm, :string
  column :email, :string
  column :message, :text

  # Validations
  validates_presence_of :name, :email, :message
  validates_format_of   :email, :with => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
  validates_length_of   :message, :minimum => 3
end

Create the controller


./script/generate controller contacts new create

Add the code for the actions:


class ContactController < ApplicationController
  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(params[:contact])

    if @contact.save # or @contact.valid?
      flash[:notice] = "Contact email sent successfully."

      # Send contact email
      #MyMailer.deliver_contact(@contact)

      redirect_to new_contact_path
    else
      render :action => "new"
    end
  end
end

Create the routes of contacts

Open the file config/routes.rb and add the line:


ActionController::Routing::Routes.draw do |map|
  map.resources :contacts, :only => [:new, :create]
  ...
end

Create a contact form

Open the file app/views/contacts/new.html.erb and add the content:


<% form_for(@contact) do |f| %>
  <%= f.error_messages %>

  <%= f.label :name %>
  <%= f.text_field :name %>

  <%= f.label :telephone %>
  <%= f.text_field :telephone %>

  <%= f.label :firm %>
  <%= f.text_field :firm %>

  <%= f.label :email %>
  <%= f.text_field :email %>

  <%= f.label :message %>
  <%= f.text_area :message %>

  <%= f.submit "Send" %>
<% end %>

Delete the file app/views/contacts/create.html.erb.

Run


./script/server

Open your browser in: http://localhost:3000/contacts/new

Documentation

http://lab.patrickespake.com/tableless_model

Copyright © 2009 Patrick Espake, released under the MIT license

Site: http://www.patrickespake.com
Blog: http://blog.patrickespake.com
Email: patrickespake@gmail.com

Releases

No releases published

Packages

No packages published

Languages