polymorphic_model 0.0.1 → 0.0.2
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/.gitignore +1 -1
- data/VERSION +1 -1
- data/lib/polymorphic_model.rb +4 -8
- data/polymorphic_model.gemspec +2 -4
- data/spec/lib/database.rb +20 -12
- data/spec/polymorphic_model_spec.rb +53 -17
- data/spec/spec_helper.rb +1 -1
- metadata +2 -4
- data/spec/lib/job_model.rb +0 -2
data/.gitignore
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/lib/polymorphic_model.rb
CHANGED
@@ -15,21 +15,17 @@ module PolymorphicModel
|
|
15
15
|
send(column) == t.to_s
|
16
16
|
end
|
17
17
|
|
18
|
+
condition_hash = {column => t.to_s}
|
18
19
|
if options[:singleton] == true
|
19
20
|
validates_uniqueness_of column, :if => :"#{t}?"
|
20
21
|
self.class.instance_eval do
|
21
22
|
define_method t do
|
22
|
-
|
23
|
-
|
24
|
-
existing || create!(column => t.to_s)
|
25
|
-
else
|
26
|
-
existing
|
27
|
-
end
|
23
|
+
scope = scoped(:conditions => condition_hash)
|
24
|
+
scope.first || (options[:autocreate] ? create!(condition_hash) : scope)
|
28
25
|
end
|
29
|
-
|
30
26
|
end
|
31
27
|
else
|
32
|
-
named_scope(t, :conditions =>
|
28
|
+
named_scope(t, :conditions => condition_hash)
|
33
29
|
end
|
34
30
|
end
|
35
31
|
end
|
data/polymorphic_model.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{polymorphic_model}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Artur Roszczyk"]
|
12
|
-
s.date = %q{2009-11-
|
12
|
+
s.date = %q{2009-11-21}
|
13
13
|
s.description = %q{Alternative for ActiveRecord's Single Table Inheritance}
|
14
14
|
s.email = %q{artur.roszczyk@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -28,7 +28,6 @@ Gem::Specification.new do |s|
|
|
28
28
|
"polymorphic_model.gemspec",
|
29
29
|
"rails/init.rb",
|
30
30
|
"spec/lib/database.rb",
|
31
|
-
"spec/lib/job_model.rb",
|
32
31
|
"spec/polymorphic_model_spec.rb",
|
33
32
|
"spec/spec.opts",
|
34
33
|
"spec/spec_helper.rb"
|
@@ -40,7 +39,6 @@ Gem::Specification.new do |s|
|
|
40
39
|
s.summary = %q{Alternative for ActiveRecord's Single Table Inheritance}
|
41
40
|
s.test_files = [
|
42
41
|
"spec/lib/database.rb",
|
43
|
-
"spec/lib/job_model.rb",
|
44
42
|
"spec/polymorphic_model_spec.rb",
|
45
43
|
"spec/spec_helper.rb"
|
46
44
|
]
|
data/spec/lib/database.rb
CHANGED
@@ -6,20 +6,28 @@ DATABASE_CONFIG = {
|
|
6
6
|
:adapter => 'sqlite3',
|
7
7
|
:database => ':memory:'
|
8
8
|
}.freeze
|
9
|
-
|
9
|
+
|
10
|
+
def set_database(table_names)
|
10
11
|
ActiveRecord::Base.establish_connection(DATABASE_CONFIG)
|
11
12
|
#ActiveRecord::Base.logger = Logger.new(STDERR)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
13
|
+
table_names.each do |table_name|
|
14
|
+
ActiveRecord::Schema.define do
|
15
|
+
suppress_messages do
|
16
|
+
create_table :"#{table_name.pluralize}" do |t|
|
17
|
+
t.string :"#{table_name}_type"
|
18
|
+
|
19
|
+
t.string :title
|
20
|
+
t.string :url
|
21
|
+
t.text :description
|
22
|
+
|
23
|
+
t.timestamps
|
24
|
+
end
|
25
|
+
end
|
22
26
|
end
|
27
|
+
|
28
|
+
eval %{
|
29
|
+
class #{table_name.capitalize} < ActiveRecord::Base
|
30
|
+
end
|
31
|
+
}
|
23
32
|
end
|
24
33
|
end
|
25
|
-
require 'lib/job_model'
|
@@ -1,8 +1,7 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
require 'lib/job_model'
|
3
2
|
|
4
3
|
describe "PolymorphicModel" do
|
5
|
-
before
|
4
|
+
before do
|
6
5
|
Job.instance_eval do
|
7
6
|
polymorphic_model :with_type_column => :job_type
|
8
7
|
end
|
@@ -18,23 +17,28 @@ describe "PolymorphicModel" do
|
|
18
17
|
define_type :some_type
|
19
18
|
end
|
20
19
|
end
|
21
|
-
|
20
|
+
|
22
21
|
it "should create check methods" do
|
23
22
|
Job.instance_methods.should include("some_type?")
|
24
23
|
end
|
24
|
+
|
25
|
+
it "should create named scope/accessor" do
|
26
|
+
Job.methods.should include("some_type")
|
27
|
+
end
|
25
28
|
end
|
26
29
|
end
|
27
30
|
|
28
31
|
describe "When normal collection types are defined" do
|
29
32
|
before do
|
30
33
|
Job.instance_eval do
|
34
|
+
polymorphic_model :with_type_column => :job_type
|
31
35
|
define_type :internal
|
32
36
|
define_type :external
|
33
37
|
end
|
34
38
|
2.times { @external = Job.create(:job_type => "external") }
|
35
39
|
3.times { @internal = Job.create(:job_type => "internal") }
|
36
40
|
end
|
37
|
-
|
41
|
+
|
38
42
|
describe "check methods" do
|
39
43
|
it "should return true if object is in kind of method" do
|
40
44
|
@external.should be_external
|
@@ -43,46 +47,78 @@ describe "When normal collection types are defined" do
|
|
43
47
|
@internal.should_not be_external
|
44
48
|
end
|
45
49
|
end
|
46
|
-
|
50
|
+
|
47
51
|
describe "named scopes" do
|
48
52
|
it "should return correct collections" do
|
49
53
|
Job.external.count.should == 2
|
50
54
|
Job.internal.count.should == 3
|
51
55
|
end
|
52
56
|
end
|
53
|
-
|
54
|
-
after do
|
55
|
-
Job.destroy_all
|
56
|
-
end
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
describe "When singleton type is defined" do
|
60
60
|
describe "with autocreate" do
|
61
61
|
before do
|
62
62
|
Job.instance_eval do
|
63
|
+
polymorphic_model :with_type_column => :job_type
|
63
64
|
define_type :basic, :singleton => true, :autocreate => true
|
64
65
|
end
|
65
66
|
end
|
66
|
-
|
67
|
-
describe "when object doesn't exist yet" do
|
67
|
+
|
68
|
+
describe "when object doesn't exist yet", "accessor" do
|
68
69
|
before do
|
69
70
|
Job.destroy_all
|
70
71
|
end
|
71
|
-
|
72
72
|
it "should create object" do
|
73
73
|
Job.basic.should be_basic
|
74
|
+
Job.basic.should_not be_new_record
|
74
75
|
end
|
75
76
|
end
|
76
|
-
|
77
|
-
describe "when object exists" do
|
77
|
+
|
78
|
+
describe "when object exists", "accessor" do
|
78
79
|
before do
|
79
|
-
Job.basic
|
80
|
+
Job.create!(:job_type => "basic")
|
81
|
+
end
|
82
|
+
it "should return object" do
|
83
|
+
Job.basic.should be_instance_of(Job)
|
80
84
|
end
|
81
|
-
|
82
85
|
it "should not allow to create another instance of object" do
|
83
86
|
Job.new(:job_type => "basic").should_not be_valid
|
84
87
|
end
|
85
88
|
end
|
86
89
|
end
|
87
90
|
|
91
|
+
describe "without autocreate" do
|
92
|
+
before do
|
93
|
+
Job.instance_eval do
|
94
|
+
polymorphic_model :with_type_column => :job_type
|
95
|
+
define_type :basic, :singleton => true, :autocreate => false
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "when object doesn't exist yet", "accessor" do
|
100
|
+
before do
|
101
|
+
Job.destroy_all
|
102
|
+
end
|
103
|
+
it "should not create object" do
|
104
|
+
Job.basic.should_not be_instance_of(Job)
|
105
|
+
end
|
106
|
+
it "should return empty scope" do
|
107
|
+
Job.basic.class.should == ActiveRecord::NamedScope::Scope
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe "when object exists", "accessor" do
|
112
|
+
before do
|
113
|
+
Job.create!(:job_type => "basic")
|
114
|
+
end
|
115
|
+
it "should return object" do
|
116
|
+
Job.basic.should be_instance_of(Job)
|
117
|
+
end
|
118
|
+
it "should not allow to create another instance of object" do
|
119
|
+
Job.new(:job_type => "basic").should_not be_valid
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
88
124
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polymorphic_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Artur Roszczyk
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-21 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -63,7 +63,6 @@ files:
|
|
63
63
|
- polymorphic_model.gemspec
|
64
64
|
- rails/init.rb
|
65
65
|
- spec/lib/database.rb
|
66
|
-
- spec/lib/job_model.rb
|
67
66
|
- spec/polymorphic_model_spec.rb
|
68
67
|
- spec/spec.opts
|
69
68
|
- spec/spec_helper.rb
|
@@ -97,6 +96,5 @@ specification_version: 3
|
|
97
96
|
summary: Alternative for ActiveRecord's Single Table Inheritance
|
98
97
|
test_files:
|
99
98
|
- spec/lib/database.rb
|
100
|
-
- spec/lib/job_model.rb
|
101
99
|
- spec/polymorphic_model_spec.rb
|
102
100
|
- spec/spec_helper.rb
|
data/spec/lib/job_model.rb
DELETED