Skip to content

Instantly share code, notes, and snippets.

@lmmendes
Created March 25, 2011 15:44
Show Gist options
  • Save lmmendes/887052 to your computer and use it in GitHub Desktop.
Save lmmendes/887052 to your computer and use it in GitHub Desktop.
A enhanced version of a Rails 2 Tableless model
# Demonstration
class Contact < Tabless
column :name, :string
column :address, :string
end
class Tableless < ActiveRecord::Base
def self.columns
@columns ||= [];
end
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
end
def self.inspect
"#<#{ self.to_s } #{ columns.collect{ |c| "#{c.name.to_s}: #{c.type.to_s}" }.join(' ') }>"
end
def save(validate = true)
validate ? valid? : true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment