splattael-activerecord_base_without_table 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +15 -0
- data/README.rdoc +78 -0
- data/Rakefile +30 -0
- data/lib/active_record/base_without_table.rb +42 -0
- data/lib/activerecord_base_without_table.rb +1 -0
- data/test/abstract_unit.rb +15 -0
- data/test/active_record_base_without_table_test.rb +68 -0
- data/test/database.yml +3 -0
- metadata +63 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
[13 March 2009]
|
2
|
+
|
3
|
+
* "Now we can have inheritance!" [Patch by Peter Abrahamsen]
|
4
|
+
|
5
|
+
[12 March 2009]
|
6
|
+
|
7
|
+
* Prevent AR from associating to this record by ID; we should be serialized instead. [Patch by Peter Abrahamsen]
|
8
|
+
|
9
|
+
[28 February 2009]
|
10
|
+
|
11
|
+
* Works with Rails 2.2.2.
|
12
|
+
|
13
|
+
[27 April 2007]
|
14
|
+
|
15
|
+
* Correctly cache class instance variables containing column information [Reported by Nils Jonsson]
|
data/README.rdoc
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
= ActiveRecordBaseWithoutTable
|
2
|
+
|
3
|
+
Get the power of ActiveRecord models, including validation, without having a table in the database.
|
4
|
+
This plugin now works with Rails 2.2.2.
|
5
|
+
|
6
|
+
== Installation
|
7
|
+
|
8
|
+
=== Gem
|
9
|
+
|
10
|
+
Rails::Initializer.run do |config|
|
11
|
+
# ...
|
12
|
+
config.gem 'splattael-activerecord_base_without_table', :lib => 'activerecord_base_without_table',
|
13
|
+
:source => 'http://gems.github.com'
|
14
|
+
|
15
|
+
# ...
|
16
|
+
end
|
17
|
+
|
18
|
+
=== Rails plugin
|
19
|
+
|
20
|
+
script/plugin install git://github.com/splattael/activerecord_base_without_table.git
|
21
|
+
|
22
|
+
== Usage
|
23
|
+
|
24
|
+
class Contact < ActiveRecord::BaseWithoutTable
|
25
|
+
column :name, :string
|
26
|
+
column :email_address, :string
|
27
|
+
column :message, :text
|
28
|
+
|
29
|
+
validates_presence_of :name, :email_address, :string
|
30
|
+
end
|
31
|
+
|
32
|
+
This model can be used just like a regular model based on a table, except it will never be saved to the database.
|
33
|
+
|
34
|
+
== Bugs & more
|
35
|
+
|
36
|
+
Please fork this repository send me push requests :)
|
37
|
+
|
38
|
+
== Authors:
|
39
|
+
|
40
|
+
* Jonathan Viney (initial version)
|
41
|
+
* Peter Abrahamsen (serialization & inheritance)
|
42
|
+
* Peter Suschlik (patch for Rails 2.2.2)
|
43
|
+
|
44
|
+
== Changelog
|
45
|
+
|
46
|
+
See CHANGELOG file.
|
47
|
+
|
48
|
+
---
|
49
|
+
|
50
|
+
= Old Infos
|
51
|
+
|
52
|
+
== ActiveRecordBaseWithoutTable
|
53
|
+
|
54
|
+
If you find this plugin useful, please consider a donation to show your support!
|
55
|
+
|
56
|
+
http://www.paypal.com/cgi-bin/webscr?cmd=_send-money
|
57
|
+
Email address: jonathan.viney@gmail.com
|
58
|
+
|
59
|
+
== Instructions
|
60
|
+
|
61
|
+
For edge Rails r7315 or above use `http://svn.viney.net.nz/things/branches/active_record_base_without_table`
|
62
|
+
|
63
|
+
Get the power of ActiveRecord models, including validation, without having a table in the database.
|
64
|
+
|
65
|
+
class Contact < ActiveRecord::BaseWithoutTable
|
66
|
+
column :name, :string
|
67
|
+
column :email_address, :string
|
68
|
+
column :message, :text
|
69
|
+
|
70
|
+
validates_presence_of :name, :email_address, :string
|
71
|
+
end
|
72
|
+
|
73
|
+
This model can be used just like a regular model based on a table, except it will never be saved to the database.
|
74
|
+
|
75
|
+
There is a good blog post available on the plugin:
|
76
|
+
`http://www.kangarooit.com/developer_blog/2007/02/email-form-validation-in-ruby-on-rails.php`
|
77
|
+
|
78
|
+
Any bugs, questions, comments please feel free to email me: jonathan.viney@gmail.com
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the active_record_base_without_table plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the active_record_base_without_table plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'ActiveRecordBaseWithoutTable'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README.rdoc')
|
21
|
+
rdoc.rdoc_files.include('CHANGELOG')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
24
|
+
|
25
|
+
GEMSPEC_FILE = File.join(File.dirname(__FILE__), 'activerecord_base_without_table.gemspec')
|
26
|
+
|
27
|
+
desc "Build gem"
|
28
|
+
task "gem:build" do
|
29
|
+
system "gem", "build", GEMSPEC_FILE
|
30
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
# Get the power of ActiveRecord models, including validation, without having a table in the database.
|
3
|
+
#
|
4
|
+
# == Usage
|
5
|
+
#
|
6
|
+
# class Contact < ActiveRecord::BaseWithoutTable
|
7
|
+
# column :name, :string
|
8
|
+
# column :email_address, :string
|
9
|
+
# column :message, :text
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# validates_presence_of :name, :email_address, :string
|
13
|
+
#
|
14
|
+
# This model can be used just like a regular model based on a table, except it will never be saved to the database.
|
15
|
+
#
|
16
|
+
class BaseWithoutTable < Base
|
17
|
+
self.abstract_class = true
|
18
|
+
|
19
|
+
class_inheritable_array :columns
|
20
|
+
self.columns = []
|
21
|
+
|
22
|
+
def create_or_update # :nodoc:
|
23
|
+
errors.empty?
|
24
|
+
end
|
25
|
+
|
26
|
+
# Prevent AR from associating to this record by ID; we should be serialized instead.
|
27
|
+
private :quoted_id
|
28
|
+
|
29
|
+
class << self
|
30
|
+
def column(name, sql_type = nil, default = nil, null = true) # :nodoc:
|
31
|
+
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
|
32
|
+
reset_column_information
|
33
|
+
end
|
34
|
+
|
35
|
+
# Do not reset @columns
|
36
|
+
def reset_column_information # :nodoc:
|
37
|
+
generated_methods.each { |name| undef_method(name) }
|
38
|
+
@column_names = @columns_hash = @content_columns = @dynamic_methods_hash = @generated_methods = nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'active_record/base_without_table'
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require File.dirname(__FILE__) + '/../../../../config/boot'
|
5
|
+
require 'active_record'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'activerecord'
|
9
|
+
end
|
10
|
+
|
11
|
+
require File.dirname(__FILE__) + '/../lib/active_record/base_without_table'
|
12
|
+
|
13
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
14
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
|
15
|
+
ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite_memory'])
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/abstract_unit'
|
2
|
+
|
3
|
+
class Person < ActiveRecord::BaseWithoutTable
|
4
|
+
column :name, :string
|
5
|
+
column :lucky_number, :integer, 4
|
6
|
+
|
7
|
+
validates_presence_of :name
|
8
|
+
end
|
9
|
+
|
10
|
+
class Employee < Person
|
11
|
+
column :salary, :integer
|
12
|
+
end
|
13
|
+
|
14
|
+
class House < ActiveRecord::BaseWithoutTable
|
15
|
+
column :person, :string
|
16
|
+
serialize :person
|
17
|
+
end
|
18
|
+
|
19
|
+
class ActiveRecordBaseWithoutTableTest < Test::Unit::TestCase
|
20
|
+
|
21
|
+
def test_default_value
|
22
|
+
assert_equal 4, Person.new.lucky_number
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_validation
|
26
|
+
p = Person.new
|
27
|
+
|
28
|
+
assert !p.save
|
29
|
+
assert p.errors[:name]
|
30
|
+
|
31
|
+
assert p.update_attributes(:name => 'Name')
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_typecast
|
35
|
+
assert_equal 1, Person.new(:lucky_number => "1").lucky_number
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_cached_column_variables_reset_when_column_defined
|
39
|
+
cached_variables = %w(column_names columns_hash content_columns dynamic_methods_hash generated_methods)
|
40
|
+
|
41
|
+
Person.column_names
|
42
|
+
Person.columns_hash
|
43
|
+
Person.content_columns
|
44
|
+
Person.column_methods_hash
|
45
|
+
Person.generated_methods
|
46
|
+
|
47
|
+
cached_variables.each { |v| assert_not_nil Person.instance_variable_get("@#{v}") }
|
48
|
+
Person.column :new_column, :string
|
49
|
+
cached_variables.each { |v| assert_nil Person.instance_variable_get("@#{v}") }
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_serialization
|
53
|
+
h = House.new(:person => Person.new)
|
54
|
+
|
55
|
+
assert_instance_of Person, h.person
|
56
|
+
quoted = h.connection.quote h.person, h.column_for_attribute('person')
|
57
|
+
assert_kind_of String, quoted
|
58
|
+
assert_match /---/, quoted
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_inheritance
|
62
|
+
e = Employee.new(:salary => 5000, :name => "Enoch Root")
|
63
|
+
|
64
|
+
assert_equal "Enoch Root", e.name
|
65
|
+
assert_equal 5000, e.salary
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
data/test/database.yml
ADDED
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: splattael-activerecord_base_without_table
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonathan Viney
|
8
|
+
- Peter Abrahamsen
|
9
|
+
- Peter Suschlik
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
|
14
|
+
date: 2009-03-13 00:00:00 -07:00
|
15
|
+
default_executable:
|
16
|
+
dependencies: []
|
17
|
+
|
18
|
+
description: Get the power of ActiveRecord models, including validation, without having a table in the database.
|
19
|
+
email: peter-arwbt@suschlik.de
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- CHANGELOG
|
28
|
+
- README.rdoc
|
29
|
+
- Rakefile
|
30
|
+
- lib/active_record/base_without_table.rb
|
31
|
+
- lib/activerecord_base_without_table.rb
|
32
|
+
- test/abstract_unit.rb
|
33
|
+
- test/active_record_base_without_table_test.rb
|
34
|
+
- test/database.yml
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/splattael/activerecord_base_without_table
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --inline-source
|
40
|
+
- --charset=UTF-8
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.2.0
|
59
|
+
signing_key:
|
60
|
+
specification_version: 2
|
61
|
+
summary: Get the power of ActiveRecord models, including validation, without having a table in the database.
|
62
|
+
test_files: []
|
63
|
+
|