Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Release candidate for 0.1, rough around the edges

  • Loading branch information...
commit 1e1aa8dc23d354ca449d4d5ce524a17867413466 1 parent 8834b07
Kenneth Kalmer authored
2  .gitignore
... ... @@ -0,0 +1,2 @@
  1 +nbproject
  2 +*.gem
4 CHANGELOG
... ... @@ -0,0 +1,4 @@
  1 +ActiveRecord::Tableless CHANGELOG
  2 +---------------------------------
  3 +
  4 +2008-05-01 - Initial code, rough around the edges.
6 README
... ... @@ -0,0 +1,6 @@
  1 +ActiveRecord Tableless Models
  2 +-----------------------------
  3 +
  4 +A single implementation of the ActiveRecord Tableless Model pattern for any
  5 +Rails project or other Ruby project that uses ActiveRecord.
  6 +
14 Rakefile
... ... @@ -0,0 +1,14 @@
  1 +require 'rake/rdoctask'
  2 +
  3 +task :default do
  4 + puts
  5 + puts IO.read( File.dirname(__FILE__) + '/README' )
  6 + puts
  7 +end
  8 +
  9 +Rake::RDocTask.new do |rd|
  10 + rd.main = 'README'
  11 + rd.rdoc_files.include( 'README', 'lib/*.rb', 'lib/**/*.rb' )
  12 + rd.rdoc_dir = 'rdoc'
  13 + rd.title = 'ActiveRecord Tableless Models'
  14 +end
18 activerecord-tableless-models.gemspec
... ... @@ -0,0 +1,18 @@
  1 +require 'rubygems'
  2 +
  3 +spec = Gem::Specification.new do |spec|
  4 + spec.name = 'tablelessmodels'
  5 + spec.summary = %q{A library for implementing tableless ActiveRecord models}
  6 + spec.description = %q{ActiveRecord Tableless Models provides a simple mixin for creating models that are not bound to the database. This approach is mostly useful for capitalizing on the features ActiveRecord::Validation}
  7 + spec.author = "Kenneth Kalmer"
  8 + spec.email = "kenneth.kalmer@gmail.com"
  9 + spec.homepage = "http://www.opensourcery.co.za/activerecordtableless/"
  10 + spec.files = [ "Rakefile", "README", "CHANGELOG" ]
  11 + spec.files + Dir["lib/*.rb"]
  12 + spec.version = "0.1.0"
  13 + spec.add_dependency("activerecord")
  14 + spec.has_rdoc = true
  15 + spec.extra_rdoc_files = %w( README CHANGELOG )
  16 + spec.rdoc_options.concat ['--main', 'README']
  17 + spec.rubyforge_project = 'tablelessmodels'
  18 +end
4 init.rb
... ... @@ -0,0 +1,4 @@
  1 +# For use by Railed and de-Railed applications alike
  2 +
  3 +require 'activerecord'
  4 +require File.dirname(__FILE__) + '/lib/tableless'
74 lib/tableless.rb
... ... @@ -0,0 +1,74 @@
  1 +# See #ActiveRecord::Tableless
  2 +
  3 +module ActiveRecord
  4 +
  5 + # = ActiveRecord::Tableless
  6 + #
  7 + # Allow classes to behave like ActiveRecord models, but without an associated
  8 + # database table. A great way to capitalize on validations. Based on the
  9 + # original post at http://www.railsweenie.com/forums/2/topics/724 (which seems
  10 + # to have disappeared from the face of the earth).
  11 + #
  12 + # = Example usage
  13 + #
  14 + # class ContactMessage < ActiveRecord::Base
  15 + #
  16 + # has_no_table
  17 + #
  18 + # column :name, :string
  19 + # column :email, :string
  20 + # column :message, :string
  21 + #
  22 + # end
  23 + #
  24 + # msg = ContactMessage.new( params[:msg] )
  25 + # if msg.valid?
  26 + # ContactMessageSender.deliver_message( msg )
  27 + # redirect_to :action => :sent
  28 + # end
  29 + #
  30 + module Tableless
  31 +
  32 + def self.included( base ) #:nodoc:
  33 + base.send( :extend, ClassMethods )
  34 + end
  35 +
  36 + module ClassMethods #:nodoc:
  37 +
  38 + # A model that needs to be tableless will call this method to indicate
  39 + # it.
  40 + def has_no_table
  41 + # keep our options handy
  42 + write_inheritable_attribute(
  43 + :tableless_options,
  44 + :columns => []
  45 + )
  46 + class_inheritable_reader :tableless_options
  47 +
  48 + # extend
  49 + extend ActiveRecord::Tableless::SingletonMethods
  50 +
  51 + # setup columns
  52 + end
  53 +
  54 + end
  55 +
  56 + module SingletonMethods
  57 +
  58 + # Return the list of columns registered for the model. Used internally by
  59 + # ActiveRecord
  60 + def columns
  61 + tableless_options[:columns]
  62 + end
  63 +
  64 + # Register a new column.
  65 + def column(name, sql_type = nil, default = nil, null = true)
  66 + tableless_options[:columns] << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
  67 + end
  68 +
  69 + end
  70 +
  71 + end
  72 +end
  73 +
  74 +ActiveRecord::Base.send( :include, ActiveRecord::Tableless )

0 comments on commit 1e1aa8d

Please sign in to comment.
Something went wrong with that request. Please try again.