Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

No commit message

  • Loading branch information...
commit fee107853f5edb51f11661a4c05be214597b47cd 0 parents
jonathan authored
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2006 Jonathan Viney
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16 README
@@ -0,0 +1,16 @@
+ActiveRecordBaseWithoutTable
+============================
+
+Get the power of ActiveRecord models, including validation, without having a table in the database.
+
+ class Contact < ActiveRecord::BaseWithoutTable
+ column :name, :string
+ column :email_address, :string
+ column :message, :text
+
+ validates_presence_of :name, :email_address, :string
+ end
+
+This model can be used just like a regular model based on a table, except it will never be saved to the database.
+
+Any bugs, questions, comments please feel free to email me: jonathan.viney@gmail.com
22 Rakefile
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the active_record_base_without_table plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the active_record_base_without_table plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'ActiveRecordBaseWithoutTable'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
19 lib/active_record/base_without_table.rb
@@ -0,0 +1,19 @@
+module ActiveRecord
+ class BaseWithoutTable < Base
+ self.abstract_class = true
+
+ def create_or_update
+ errors.empty?
+ end
+
+ class << self
+ def columns()
+ @columns ||= []
+ end
+
+ def column(name, sql_type = nil, default = nil, null = true)
+ columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
+ end
+ end
+ end
+end
15 test/abstract_unit.rb
@@ -0,0 +1,15 @@
+require 'test/unit'
+
+begin
+ require File.dirname(__FILE__) + '/../../../../config/boot'
+ require 'active_record'
+rescue LoadError
+ require 'rubygems'
+ require_gem 'activerecord'
+end
+
+require File.dirname(__FILE__) + '/../lib/active_record/base_without_table'
+
+config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
+ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
+ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'mysql'])
27 test/active_record_base_without_table_test.rb
@@ -0,0 +1,27 @@
+require File.dirname(__FILE__) + '/abstract_unit'
+
+class Person < ActiveRecord::BaseWithoutTable
+ column :name, :string
+ column :lucky_number, :integer, 4
+
+ validates_presence_of :name
+end
+
+class ActiveRecordBaseWithoutTableTest < Test::Unit::TestCase
+ def test_default_value
+ assert_equal 4, Person.new.lucky_number
+ end
+
+ def test_validation
+ p = Person.new
+
+ assert !p.save
+ assert p.errors[:name]
+
+ assert p.update_attributes(:name => 'Name')
+ end
+
+ def test_typecast
+ assert_equal 1, Person.new(:lucky_number => "1").lucky_number
+ end
+end
6 test/database.yml
@@ -0,0 +1,6 @@
+mysql:
+ :adapter: mysql
+ :host: localhost
+ :username: rails
+ :password:
+ :database: active_record_base_without_table_plugin_test
Please sign in to comment.
Something went wrong with that request. Please try again.