polymorphic_model 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/polymorphic_model/spec/rails/mocks.rb +27 -0
- data/lib/spec/rails/polymorphic_model.rb +1 -0
- data/polymorphic_model.gemspec +5 -1
- data/spec/polymorphic_model/spec/rails/mocks_spec.rb +35 -0
- data/spec/polymorphic_model_spec.rb +5 -1
- data/spec/spec_helper.rb +0 -1
- metadata +5 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.5
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec'
|
2
|
+
require 'spec/rails/mocks'
|
3
|
+
|
4
|
+
module Spec
|
5
|
+
module Rails
|
6
|
+
module Mocks
|
7
|
+
|
8
|
+
class InvalidPolymorphicModelTypeError < Exception; end
|
9
|
+
|
10
|
+
def mock_polymorphic_model(model_class, model_type, options_and_stubs={})
|
11
|
+
raise InvalidPolymorphicModelTypeError unless model_class.types.include?(model_type)
|
12
|
+
if block_given?
|
13
|
+
m = mock_model(model_class, options_and_stubs, &block)
|
14
|
+
else
|
15
|
+
m = mock_model(model_class, options_and_stubs)
|
16
|
+
end
|
17
|
+
|
18
|
+
model_class.types.each do |t|
|
19
|
+
m.stub!(:"#{t}?").and_return(t == model_type ? true : false)
|
20
|
+
end
|
21
|
+
polymorphic_column = model_class.instance_eval { @_polymorphic_column }
|
22
|
+
m.stub!(polymorphic_column).and_return(model_type.to_s)
|
23
|
+
return m
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'polymorphic_model/spec/rails/mocks'
|
data/polymorphic_model.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
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.5"
|
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"]
|
@@ -25,9 +25,12 @@ Gem::Specification.new do |s|
|
|
25
25
|
"VERSION",
|
26
26
|
"init.rb",
|
27
27
|
"lib/polymorphic_model.rb",
|
28
|
+
"lib/polymorphic_model/spec/rails/mocks.rb",
|
29
|
+
"lib/spec/rails/polymorphic_model.rb",
|
28
30
|
"polymorphic_model.gemspec",
|
29
31
|
"rails/init.rb",
|
30
32
|
"spec/lib/database.rb",
|
33
|
+
"spec/polymorphic_model/spec/rails/mocks_spec.rb",
|
31
34
|
"spec/polymorphic_model_spec.rb",
|
32
35
|
"spec/spec.opts",
|
33
36
|
"spec/spec_helper.rb"
|
@@ -39,6 +42,7 @@ Gem::Specification.new do |s|
|
|
39
42
|
s.summary = %q{Alternative for ActiveRecord's Single Table Inheritance}
|
40
43
|
s.test_files = [
|
41
44
|
"spec/lib/database.rb",
|
45
|
+
"spec/polymorphic_model/spec/rails/mocks_spec.rb",
|
42
46
|
"spec/polymorphic_model_spec.rb",
|
43
47
|
"spec/spec_helper.rb"
|
44
48
|
]
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
|
2
|
+
|
3
|
+
describe "Spec::Rails::Mocks" do
|
4
|
+
require 'spec/rails/polymorphic_model'
|
5
|
+
include Spec::Rails::Mocks
|
6
|
+
|
7
|
+
describe :mock_polymorphic_model do
|
8
|
+
before(:all) do
|
9
|
+
set_database(["task"])
|
10
|
+
class Task < ActiveRecord::Base
|
11
|
+
polymorphic_model :with_type_column => "task_type"
|
12
|
+
define_type :internal
|
13
|
+
define_type :external
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should stub check-methods" do
|
18
|
+
mock = mock_polymorphic_model(Task, :external)
|
19
|
+
mock.should be_external
|
20
|
+
mock.should_not be_internal
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should should stub type-column value" do
|
24
|
+
mock = mock_polymorphic_model(Task, :internal)
|
25
|
+
mock.task_type.should == "internal"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should not allow mock model with incorrect type" do
|
29
|
+
lambda do
|
30
|
+
mock_polymorphic_model(Task, :invalid)
|
31
|
+
end.should raise_error(Spec::Rails::Mocks::InvalidPolymorphicModelTypeError)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -1,7 +1,8 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe "PolymorphicModel" do
|
4
|
-
before do
|
4
|
+
before(:each) do
|
5
|
+
set_database(["job"])
|
5
6
|
Job.instance_eval do
|
6
7
|
polymorphic_model :with_type_column => :job_type
|
7
8
|
end
|
@@ -30,6 +31,7 @@ end
|
|
30
31
|
|
31
32
|
describe "When normal collection types are defined" do
|
32
33
|
before do
|
34
|
+
set_database(["job"])
|
33
35
|
Job.instance_eval do
|
34
36
|
polymorphic_model :with_type_column => :job_type
|
35
37
|
define_type :internal
|
@@ -75,6 +77,7 @@ end
|
|
75
77
|
describe "When singleton type is defined" do
|
76
78
|
describe "with autocreate" do
|
77
79
|
before do
|
80
|
+
set_database(["job"])
|
78
81
|
Job.instance_eval do
|
79
82
|
polymorphic_model :with_type_column => :job_type
|
80
83
|
define_type :basic, :singleton => true, :autocreate => true
|
@@ -106,6 +109,7 @@ describe "When singleton type is defined" do
|
|
106
109
|
|
107
110
|
describe "without autocreate" do
|
108
111
|
before do
|
112
|
+
set_database(["job"])
|
109
113
|
Job.instance_eval do
|
110
114
|
polymorphic_model :with_type_column => :job_type
|
111
115
|
define_type :basic, :singleton => true, :autocreate => false
|
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Artur Roszczyk
|
@@ -60,9 +60,12 @@ files:
|
|
60
60
|
- VERSION
|
61
61
|
- init.rb
|
62
62
|
- lib/polymorphic_model.rb
|
63
|
+
- lib/polymorphic_model/spec/rails/mocks.rb
|
64
|
+
- lib/spec/rails/polymorphic_model.rb
|
63
65
|
- polymorphic_model.gemspec
|
64
66
|
- rails/init.rb
|
65
67
|
- spec/lib/database.rb
|
68
|
+
- spec/polymorphic_model/spec/rails/mocks_spec.rb
|
66
69
|
- spec/polymorphic_model_spec.rb
|
67
70
|
- spec/spec.opts
|
68
71
|
- spec/spec_helper.rb
|
@@ -96,5 +99,6 @@ specification_version: 3
|
|
96
99
|
summary: Alternative for ActiveRecord's Single Table Inheritance
|
97
100
|
test_files:
|
98
101
|
- spec/lib/database.rb
|
102
|
+
- spec/polymorphic_model/spec/rails/mocks_spec.rb
|
99
103
|
- spec/polymorphic_model_spec.rb
|
100
104
|
- spec/spec_helper.rb
|