active_record-tableless_model 0.0.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/.autotest +5 -0
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +45 -0
- data/Rakefile +56 -0
- data/VERSION +1 -0
- data/lib/active_record-tableless_model.rb +49 -0
- data/test/helper.rb +12 -0
- data/test/test_active_record-tableless_model.rb +69 -0
- metadata +78 -0
data/.autotest
ADDED
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Keith Mc Donnell
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
= active_record-tableless_model
|
2
|
+
|
3
|
+
Simplify creating ActiveRecord clases which don't need a database table. Useful for:
|
4
|
+
|
5
|
+
* Factory method, ie creating other classes
|
6
|
+
* Wizard (multi-step) forms
|
7
|
+
* Presenter style forms handling mutiple models
|
8
|
+
|
9
|
+
Thanks to {Ryan Bates Tableless Model Railscast}[http://railscasts.com/episodes/193-tableless-model]
|
10
|
+
and the {Tableless Model asciicast}[http://asciicasts.com/episodes/193-tableless-model.html for the inspiration] .
|
11
|
+
|
12
|
+
There is an {active_record-tableless_model introduction}[http://www.keithmcdonnell.net/activerecord_tableless_model_gem.html]
|
13
|
+
on {Keith McDonnells ruby blog}[http://www.keithmcdonnell.net] .
|
14
|
+
|
15
|
+
== Install
|
16
|
+
|
17
|
+
@$ gem install active_record-tableless_model@
|
18
|
+
|
19
|
+
== Usage
|
20
|
+
|
21
|
+
require 'active_record-tableless_model'
|
22
|
+
|
23
|
+
class Task < ActiveRecord::Base
|
24
|
+
no_table
|
25
|
+
|
26
|
+
column :description, :text
|
27
|
+
column :priority, :integer
|
28
|
+
column :project_id, :string
|
29
|
+
end
|
30
|
+
|
31
|
+
Note that your class must still inherit from ActiveRecord if you need to use validations and associations.
|
32
|
+
|
33
|
+
== Note on Patches/Pull Requests
|
34
|
+
|
35
|
+
* Fork the project.
|
36
|
+
* Make your feature addition or bug fix.
|
37
|
+
* Add tests for it. This is important so I don't break it in a
|
38
|
+
future version unintentionally.
|
39
|
+
* Commit, do not mess with rakefile, version, or history.
|
40
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
41
|
+
* Send me a pull request. Bonus points for topic branches.
|
42
|
+
|
43
|
+
== Copyright
|
44
|
+
|
45
|
+
Copyright (c) 2011 {Keith Mc Donnell}[http://www.keithmcdonnell.net] . See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "active_record-tableless_model"
|
8
|
+
gem.summary = %Q{Create ActiveRecord clases without database tables}
|
9
|
+
gem.description = %Q{Simplify creating ActiveRecord clases which don't need a database table}
|
10
|
+
gem.email = "keith@dancingtext.com"
|
11
|
+
gem.homepage = "http://github.com/kmcd/active_record-tableless_model"
|
12
|
+
gem.authors = ["Keith Mc Donnell"]
|
13
|
+
gem.rubyforge_project = "active_record-tableless_model"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
Jeweler::RubyforgeTasks.new do |rubyforge|
|
18
|
+
rubyforge.doc_task = "rdoc"
|
19
|
+
end
|
20
|
+
rescue LoadError
|
21
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
Rake::TestTask.new(:test) do |test|
|
26
|
+
test.libs << 'lib' << 'test'
|
27
|
+
test.pattern = 'test/**/test_*.rb'
|
28
|
+
test.verbose = true
|
29
|
+
end
|
30
|
+
|
31
|
+
begin
|
32
|
+
require 'rcov/rcovtask'
|
33
|
+
Rcov::RcovTask.new do |test|
|
34
|
+
test.libs << 'test'
|
35
|
+
test.pattern = 'test/**/test_*.rb'
|
36
|
+
test.verbose = true
|
37
|
+
end
|
38
|
+
rescue LoadError
|
39
|
+
task :rcov do
|
40
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
task :test => :check_dependencies
|
45
|
+
|
46
|
+
task :default => :test
|
47
|
+
|
48
|
+
require 'rake/rdoctask'
|
49
|
+
Rake::RDocTask.new do |rdoc|
|
50
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
51
|
+
|
52
|
+
rdoc.rdoc_dir = 'rdoc'
|
53
|
+
rdoc.title = "active_record-tableless_model #{version}"
|
54
|
+
rdoc.rdoc_files.include('README*')
|
55
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module ActiveRecord::Base::TablelessModel
|
4
|
+
# When included, the current class will use no database tables
|
5
|
+
# You can declare ActiveRecord style attributes using #column
|
6
|
+
def self.included(klass)
|
7
|
+
klass.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
# Returns an array of column objects associated with this class.
|
12
|
+
def columns
|
13
|
+
@columns ||= []
|
14
|
+
end
|
15
|
+
|
16
|
+
# Creates an attribute corresponding to a database column.
|
17
|
+
# N.B No table is created in the database
|
18
|
+
#
|
19
|
+
# == Arguments
|
20
|
+
#
|
21
|
+
# <tt>name</tt> :: column name, such as supplier_id in supplier_id int(11).
|
22
|
+
# <tt>default</tt> :: type-casted default value, such as new in sales_stage varchar(20) default 'new'.
|
23
|
+
# <tt>sql_type</tt> :: used to extract the column length, if necessary. For example 60 in company_name varchar(60). null determines if this column allows NULL values.
|
24
|
+
#
|
25
|
+
# == Usage
|
26
|
+
# class Task < ActiveRecord::Base
|
27
|
+
# no_table
|
28
|
+
# column :description, :text
|
29
|
+
# column :description, :string, 'foo', false
|
30
|
+
# end
|
31
|
+
def column(name, sql_type = :text, default = nil, null = true)
|
32
|
+
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class ActiveRecord::Base
|
38
|
+
# Declares the current class to have no table
|
39
|
+
# You can declare ActiveRecord style attributes using TablelessModel#column
|
40
|
+
#
|
41
|
+
# == Usage
|
42
|
+
# class Task < ActiveRecord::Base
|
43
|
+
# no_table
|
44
|
+
# column :description, :text
|
45
|
+
# end
|
46
|
+
def self.no_table
|
47
|
+
include TablelessModel
|
48
|
+
end
|
49
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
require 'active_record-tableless_model'
|
7
|
+
|
8
|
+
require 'active_support/testing/declarative'
|
9
|
+
|
10
|
+
class Test::Unit::TestCase
|
11
|
+
extend ActiveSupport::Testing::Declarative
|
12
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'active_record'
|
3
|
+
|
4
|
+
class Task < ActiveRecord::Base
|
5
|
+
no_table
|
6
|
+
column :description
|
7
|
+
end
|
8
|
+
|
9
|
+
class Project < ActiveRecord::Base
|
10
|
+
no_table
|
11
|
+
column :name
|
12
|
+
end
|
13
|
+
|
14
|
+
class TestActiveRecordTablelessModel < Test::Unit::TestCase
|
15
|
+
def setup
|
16
|
+
@column = Task.columns.first
|
17
|
+
@task = Task.new
|
18
|
+
end
|
19
|
+
|
20
|
+
test "should declare a column" do
|
21
|
+
assert_kind_of ActiveRecord::ConnectionAdapters::Column, @column
|
22
|
+
assert_equal 'description', @column.name
|
23
|
+
end
|
24
|
+
|
25
|
+
test "should have text as the default column type" do
|
26
|
+
assert_equal 'text', @column.sql_type
|
27
|
+
end
|
28
|
+
|
29
|
+
test "should be able to specify a default column value" do
|
30
|
+
Task.column :description, :string, 'foo'
|
31
|
+
assert_equal 'foo', Task.new.description
|
32
|
+
end
|
33
|
+
|
34
|
+
test "should be able to specify wheter the column is nullable" do
|
35
|
+
Task.column :foo, :string, 'bar', false
|
36
|
+
column = Task.columns.detect {|column| column.name == 'foo' }
|
37
|
+
|
38
|
+
assert_equal false, column.null
|
39
|
+
end
|
40
|
+
|
41
|
+
test "should be able to set integer" do
|
42
|
+
Task.column :priority, :integer
|
43
|
+
|
44
|
+
@task.priority = 1
|
45
|
+
assert @task.valid?
|
46
|
+
end
|
47
|
+
|
48
|
+
test "should have ActiveRecord associations available" do
|
49
|
+
Task.column :project_id, :integer
|
50
|
+
Task.belongs_to :project
|
51
|
+
Project.has_one :task
|
52
|
+
|
53
|
+
project = Project.new
|
54
|
+
task = Task.new
|
55
|
+
task.project_id = project.id
|
56
|
+
|
57
|
+
assert_equal [task], project.task
|
58
|
+
end
|
59
|
+
|
60
|
+
test "should have ActiveRecord validations available" do
|
61
|
+
Task.validates_presence_of :project_id
|
62
|
+
|
63
|
+
task = Task.new
|
64
|
+
assert_equal false, task.valid?
|
65
|
+
|
66
|
+
task.project_id = 1
|
67
|
+
assert_equal true, task.valid?
|
68
|
+
end
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record-tableless_model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Keith Mc Donnell
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-04-11 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Simplify creating ActiveRecord clases which don't need a database table
|
23
|
+
email: keith@dancingtext.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- LICENSE
|
30
|
+
- README.rdoc
|
31
|
+
files:
|
32
|
+
- .autotest
|
33
|
+
- .document
|
34
|
+
- .gitignore
|
35
|
+
- LICENSE
|
36
|
+
- README.rdoc
|
37
|
+
- Rakefile
|
38
|
+
- VERSION
|
39
|
+
- lib/active_record-tableless_model.rb
|
40
|
+
- test/helper.rb
|
41
|
+
- test/test_active_record-tableless_model.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/kmcd/active_record-tableless_model
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project: active_record-tableless_model
|
72
|
+
rubygems_version: 1.3.7
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Create ActiveRecord clases without database tables
|
76
|
+
test_files:
|
77
|
+
- test/test_active_record-tableless_model.rb
|
78
|
+
- test/helper.rb
|