can_be 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +0 -1
- data/CHANGELOG.md +14 -5
- data/Gemfile.lock +56 -0
- data/README.md +30 -48
- data/can_be.gemspec +1 -0
- data/docs/details.md +94 -0
- data/docs/history.md +71 -0
- data/docs/rspec_matcher.md +11 -0
- data/lib/can_be/builder/can_be.rb +24 -5
- data/lib/can_be/builder/can_be_detail.rb +62 -4
- data/lib/can_be/config.rb +30 -5
- data/lib/can_be/model_extensions.rb +3 -3
- data/lib/can_be/processor/instance.rb +106 -11
- data/lib/can_be/rspec/matchers.rb +9 -0
- data/lib/can_be/rspec/matchers/can_be_detail_matcher.rb +26 -0
- data/lib/can_be/rspec/matchers/can_be_matcher.rb +76 -0
- data/lib/can_be/version.rb +1 -1
- data/spec/can_be/config_spec.rb +42 -0
- data/spec/can_be/model_extensions_spec.rb +14 -187
- data/spec/can_be/rspec/matchers/can_be_detail_matcher_spec.rb +11 -0
- data/spec/can_be/rspec/matchers/can_be_matcher_spec.rb +57 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/support/can_be_detail_history_shared_examples.rb +17 -0
- data/spec/support/can_be_detail_shared_examples.rb +13 -0
- data/spec/support/can_be_history_shared_examples.rb +135 -0
- data/spec/support/can_be_shared_examples.rb +204 -0
- data/spec/support/models.rb +54 -0
- data/spec/support/schema.rb +55 -0
- metadata +43 -3
@@ -0,0 +1,204 @@
|
|
1
|
+
shared_examples "it has details" do |can_be_class, image_class, video_class, details_name = :details|
|
2
|
+
context "details" do
|
3
|
+
context "persistence" do
|
4
|
+
it "persists the details information to the database (create method)" do
|
5
|
+
upload = can_be_class.create
|
6
|
+
upload.send(details_name).format = "jpeg"
|
7
|
+
upload.save
|
8
|
+
image_class.first.format.should == "jpeg"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "persists the details information to the database (create_image_upload method)" do
|
12
|
+
upload = can_be_class.create_image_upload
|
13
|
+
upload.send(details_name).format = "jpeg"
|
14
|
+
upload.save
|
15
|
+
image_class.first.format.should == "jpeg"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "persists the details information to the database (new method)" do
|
19
|
+
upload = can_be_class.new
|
20
|
+
upload.send(details_name).format = "jpeg"
|
21
|
+
upload.save
|
22
|
+
image_class.first.format.should == "jpeg"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "persists the details information to the database (new_image_upload method)" do
|
26
|
+
upload = can_be_class.new_image_upload
|
27
|
+
upload.send(details_name).format = "jpeg"
|
28
|
+
upload.save
|
29
|
+
image_class.first.format.should == "jpeg"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "persists only one details record" do
|
33
|
+
upload = can_be_class.new
|
34
|
+
upload.send(details_name).format = "jpeg"
|
35
|
+
upload.save
|
36
|
+
found_upload = can_be_class.find(upload.id)
|
37
|
+
found_upload.send(details_name).format.should == "jpeg"
|
38
|
+
found_upload.save
|
39
|
+
image_class.count.should == 1
|
40
|
+
end
|
41
|
+
|
42
|
+
it "deletes the details record" do
|
43
|
+
u = can_be_class.create_image_upload
|
44
|
+
image_class.count.should == 1
|
45
|
+
u.destroy
|
46
|
+
image_class.count.should == 0
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "create method" do
|
51
|
+
it "creates the correct details record" do
|
52
|
+
can_be_class.create_image_upload.send(details_name).should be_instance_of(image_class)
|
53
|
+
can_be_class.create_video_upload.send(details_name).should be_instance_of(video_class)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "doesn't create details record if model doesn't call #can_be_detail" do
|
57
|
+
can_be_class.create_thumbnail_upload.send(details_name).should be_nil
|
58
|
+
end
|
59
|
+
|
60
|
+
it "doesn't create details record if model doesn't exist" do
|
61
|
+
can_be_class.create_pdf_upload.send(details_name).should be_nil
|
62
|
+
end
|
63
|
+
|
64
|
+
it "doesn't create details record unless an ActiveRecord model" do
|
65
|
+
can_be_class.create_document_upload.send(details_name).should be_nil
|
66
|
+
end
|
67
|
+
|
68
|
+
it "persists the details record to the database" do
|
69
|
+
can_be_class.create_image_upload
|
70
|
+
image_class.count.should == 1
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "new method" do
|
75
|
+
it "instantiates the correct details record" do
|
76
|
+
can_be_class.new_image_upload.send(details_name).should be_instance_of(image_class)
|
77
|
+
can_be_class.new_video_upload.send(details_name).should be_instance_of(video_class)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "doesn't instantiate details record if model doesn't call #can_be_detail" do
|
81
|
+
can_be_class.new_thumbnail_upload.send(details_name).should be_nil
|
82
|
+
end
|
83
|
+
|
84
|
+
it "doesn't instantiate details record if model doesn't exist" do
|
85
|
+
can_be_class.new_pdf_upload.send(details_name).should be_nil
|
86
|
+
end
|
87
|
+
|
88
|
+
it "doesn't instantiate details record unless an ActiveRecord model" do
|
89
|
+
can_be_class.new_document_upload.send(details_name).should be_nil
|
90
|
+
end
|
91
|
+
|
92
|
+
it "doesn't persist the details record to the database" do
|
93
|
+
can_be_class.new_image_upload
|
94
|
+
image_class.count.should == 0
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "change type via #change_to" do
|
99
|
+
it "changes the details record type" do
|
100
|
+
u = can_be_class.new_image_upload
|
101
|
+
u.change_to_video_upload
|
102
|
+
u.send(details_name).should be_instance_of(video_class)
|
103
|
+
end
|
104
|
+
|
105
|
+
it "changes the details to nil" do
|
106
|
+
u = can_be_class.new_image_upload
|
107
|
+
u.change_to_thumbnail_upload
|
108
|
+
u.send(details_name).should be_nil
|
109
|
+
end
|
110
|
+
|
111
|
+
it "doesn't create a new record in the database" do
|
112
|
+
u = can_be_class.new_image_upload
|
113
|
+
u.change_to_video_upload
|
114
|
+
image_class.count.should == 0
|
115
|
+
video_class.count.should == 0
|
116
|
+
end
|
117
|
+
|
118
|
+
it "has access to the original details if not saved" do
|
119
|
+
u = can_be_class.create_image_upload
|
120
|
+
u.change_to_video_upload
|
121
|
+
can_be_class.find(u.id).send(details_name).should be_instance_of(image_class)
|
122
|
+
end
|
123
|
+
|
124
|
+
it "allows for items to be set on the new details records" do
|
125
|
+
new_encoding = "new encoding"
|
126
|
+
u = can_be_class.new_image_upload
|
127
|
+
u.change_to_video_upload do |details|
|
128
|
+
details.encoding = new_encoding
|
129
|
+
end
|
130
|
+
u.send(details_name).encoding.should == new_encoding
|
131
|
+
end
|
132
|
+
|
133
|
+
it "removes the old details record when saved" do
|
134
|
+
u = can_be_class.new_image_upload
|
135
|
+
u.save
|
136
|
+
u.change_to_video_upload
|
137
|
+
u.save
|
138
|
+
image_class.count.should == 0
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
context "change type via #change_to!" do
|
143
|
+
it "changes the details record type" do
|
144
|
+
u = can_be_class.create_video_upload
|
145
|
+
u.change_to_image_upload!
|
146
|
+
can_be_class.find(u.id).send(details_name).should be_instance_of(image_class)
|
147
|
+
end
|
148
|
+
|
149
|
+
it "changes the details to nil" do
|
150
|
+
u = can_be_class.create_image_upload
|
151
|
+
u.change_to_thumbnail_upload!
|
152
|
+
can_be_class.find(u.id).send(details_name).should be_nil
|
153
|
+
end
|
154
|
+
|
155
|
+
it "doesn't create a new record in the database" do
|
156
|
+
u = can_be_class.create_video_upload
|
157
|
+
u.change_to_image_upload!
|
158
|
+
image_class.count.should == 1
|
159
|
+
end
|
160
|
+
|
161
|
+
it "removes the old database record from the database" do
|
162
|
+
u = can_be_class.create_image_upload
|
163
|
+
u.change_to_video_upload!
|
164
|
+
image_class.count.should == 0
|
165
|
+
end
|
166
|
+
|
167
|
+
it "allows for items to be set on the new details records" do
|
168
|
+
new_format = "new format"
|
169
|
+
u = can_be_class.create_video_upload
|
170
|
+
u.change_to_image_upload! do |details|
|
171
|
+
details.format = new_format
|
172
|
+
end
|
173
|
+
can_be_class.find(u.id).send(details_name).format.should == new_format
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
context "change type setting the model attribute" do
|
178
|
+
it "changes the details record type" do
|
179
|
+
u = can_be_class.new_image_upload
|
180
|
+
u.can_be_type = "video_upload"
|
181
|
+
u.send(details_name).should be_instance_of(video_class)
|
182
|
+
end
|
183
|
+
|
184
|
+
it "changes the details to nil" do
|
185
|
+
u = can_be_class.new_image_upload
|
186
|
+
u.can_be_type = "thumbnail_upload"
|
187
|
+
u.send(details_name).should be_nil
|
188
|
+
end
|
189
|
+
|
190
|
+
it "doesn't create a new record in the database" do
|
191
|
+
u = can_be_class.new_image_upload
|
192
|
+
u.can_be_type = "video_upload"
|
193
|
+
image_class.count.should == 0
|
194
|
+
video_class.count.should == 0
|
195
|
+
end
|
196
|
+
|
197
|
+
it "has access to the original details if not saved" do
|
198
|
+
u = can_be_class.create_image_upload
|
199
|
+
u.can_be_type = "video_upload"
|
200
|
+
can_be_class.find(u.id).send(details_name).should be_instance_of(image_class)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
data/spec/support/models.rb
CHANGED
@@ -11,6 +11,13 @@ class Person < ActiveRecord::Base
|
|
11
11
|
can_be :male, :female, field_name: :gender, default_type: :female
|
12
12
|
end
|
13
13
|
|
14
|
+
class BlockOption < ActiveRecord::Base
|
15
|
+
can_be :option_1, :option_2 do
|
16
|
+
field_name :option_type
|
17
|
+
default_type :option_2
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
14
21
|
class Upload < ActiveRecord::Base
|
15
22
|
can_be :image_upload, :video_upload, :thumbnail_upload, :document_upload, :pdf_upload do
|
16
23
|
add_details_model :image_upload, :image_upload_detail
|
@@ -32,6 +39,53 @@ end
|
|
32
39
|
class DocumentUploadDetail
|
33
40
|
end
|
34
41
|
|
42
|
+
class CustomUpload < ActiveRecord::Base
|
43
|
+
can_be :image_upload, :video_upload, :thumbnail_upload, :document_upload, :pdf_upload do
|
44
|
+
add_details_model :image_upload, :custom_image_upload_detail
|
45
|
+
add_details_model :video_upload, :custom_video_upload_detail
|
46
|
+
details_name :custom_details
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class CustomImageUploadDetail < ActiveRecord::Base
|
51
|
+
can_be_detail :custom_upload, :custom_details
|
52
|
+
end
|
53
|
+
|
54
|
+
class CustomVideoUploadDetail < ActiveRecord::Base
|
55
|
+
can_be_detail :custom_upload, details_name: :custom_details
|
56
|
+
end
|
57
|
+
|
58
|
+
class CustomThumbnailUploadDetail < ActiveRecord::Base
|
59
|
+
end
|
60
|
+
|
61
|
+
class CustomDocumentUploadDetail
|
62
|
+
end
|
63
|
+
|
64
|
+
class HistoryUpload < ActiveRecord::Base
|
65
|
+
can_be :image_upload, :video_upload, :thumbnail_upload, :document_upload, :pdf_upload do
|
66
|
+
add_details_model :image_upload, :history_image_upload_detail
|
67
|
+
add_details_model :video_upload, :history_video_upload_detail
|
68
|
+
keep_history_in :history_upload_history_record
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class HistoryImageUploadDetail < ActiveRecord::Base
|
73
|
+
can_be_detail :history_upload, history_model: :history_upload_history_record
|
74
|
+
end
|
75
|
+
|
76
|
+
class HistoryVideoUploadDetail < ActiveRecord::Base
|
77
|
+
can_be_detail :history_upload, history_model: :history_upload_history_record
|
78
|
+
end
|
79
|
+
|
80
|
+
class HistoryThumbnailUploadDetail < ActiveRecord::Base
|
81
|
+
end
|
82
|
+
|
83
|
+
class HistoryDocumentUploadDetail
|
84
|
+
end
|
85
|
+
|
86
|
+
class HistoryUploadHistoryRecord < ActiveRecord::Base
|
87
|
+
end
|
88
|
+
|
35
89
|
class ConfigSpecModel < ActiveRecord::Base
|
36
90
|
can_be :type1, :type2
|
37
91
|
end
|
data/spec/support/schema.rb
CHANGED
@@ -12,6 +12,32 @@ ActiveRecord::Schema.define do
|
|
12
12
|
t.timestamps
|
13
13
|
end
|
14
14
|
|
15
|
+
create_table :block_options, :force => true do |t|
|
16
|
+
t.string :option_type
|
17
|
+
t.timestamps
|
18
|
+
end
|
19
|
+
|
20
|
+
create_table :custom_uploads, :force => true do |t|
|
21
|
+
t.string :can_be_type
|
22
|
+
t.integer :custom_details_id
|
23
|
+
t.string :custom_details_type
|
24
|
+
t.timestamps
|
25
|
+
end
|
26
|
+
|
27
|
+
create_table :custom_image_upload_details, :force => true do |t|
|
28
|
+
t.string :format
|
29
|
+
t.timestamps
|
30
|
+
end
|
31
|
+
|
32
|
+
create_table :custom_video_upload_details, :force => true do |t|
|
33
|
+
t.string :encoding
|
34
|
+
t.timestamps
|
35
|
+
end
|
36
|
+
|
37
|
+
create_table :custom_thumbnail_upload_details, :force => true do |t|
|
38
|
+
t.timestamps
|
39
|
+
end
|
40
|
+
|
15
41
|
create_table :uploads, :force => true do |t|
|
16
42
|
t.string :can_be_type
|
17
43
|
t.integer :details_id
|
@@ -33,6 +59,35 @@ ActiveRecord::Schema.define do
|
|
33
59
|
t.timestamps
|
34
60
|
end
|
35
61
|
|
62
|
+
create_table :history_uploads, :force => true do |t|
|
63
|
+
t.string :can_be_type
|
64
|
+
t.integer :details_id
|
65
|
+
t.string :details_type
|
66
|
+
t.timestamps
|
67
|
+
end
|
68
|
+
|
69
|
+
create_table :history_image_upload_details, :force => true do |t|
|
70
|
+
t.string :format
|
71
|
+
t.timestamps
|
72
|
+
end
|
73
|
+
|
74
|
+
create_table :history_video_upload_details, :force => true do |t|
|
75
|
+
t.string :encoding
|
76
|
+
t.timestamps
|
77
|
+
end
|
78
|
+
|
79
|
+
create_table :history_thumbnail_upload_details, :force => true do |t|
|
80
|
+
t.timestamps
|
81
|
+
end
|
82
|
+
|
83
|
+
create_table :history_upload_history_records, :force => true do |t|
|
84
|
+
t.integer :can_be_model_id
|
85
|
+
t.string :can_be_type
|
86
|
+
t.integer :can_be_details_id
|
87
|
+
t.string :can_be_details_type
|
88
|
+
t.timestamps
|
89
|
+
end
|
90
|
+
|
36
91
|
create_table :config_spec_models, :force => true do |t|
|
37
92
|
t.string :can_be_type
|
38
93
|
t.timestamps
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: can_be
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -75,6 +75,22 @@ dependencies:
|
|
75
75
|
- - ! '>='
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: pry
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
78
94
|
- !ruby/object:Gem::Dependency
|
79
95
|
name: activerecord
|
80
96
|
requirement: !ruby/object:Gem::Requirement
|
@@ -122,10 +138,14 @@ files:
|
|
122
138
|
- .yardopts
|
123
139
|
- CHANGELOG.md
|
124
140
|
- Gemfile
|
141
|
+
- Gemfile.lock
|
125
142
|
- LICENSE.txt
|
126
143
|
- README.md
|
127
144
|
- Rakefile
|
128
145
|
- can_be.gemspec
|
146
|
+
- docs/details.md
|
147
|
+
- docs/history.md
|
148
|
+
- docs/rspec_matcher.md
|
129
149
|
- gemfiles/3.1.gemfile
|
130
150
|
- gemfiles/3.2.gemfile
|
131
151
|
- lib/can_be.rb
|
@@ -138,12 +158,21 @@ files:
|
|
138
158
|
- lib/can_be/processor/instance.rb
|
139
159
|
- lib/can_be/processor/klass.rb
|
140
160
|
- lib/can_be/railtie.rb
|
161
|
+
- lib/can_be/rspec/matchers.rb
|
162
|
+
- lib/can_be/rspec/matchers/can_be_detail_matcher.rb
|
163
|
+
- lib/can_be/rspec/matchers/can_be_matcher.rb
|
141
164
|
- lib/can_be/version.rb
|
142
165
|
- log/.gitkeep
|
143
166
|
- spec/can_be/config_spec.rb
|
144
167
|
- spec/can_be/model_extensions_spec.rb
|
168
|
+
- spec/can_be/rspec/matchers/can_be_detail_matcher_spec.rb
|
169
|
+
- spec/can_be/rspec/matchers/can_be_matcher_spec.rb
|
145
170
|
- spec/can_be_spec.rb
|
146
171
|
- spec/spec_helper.rb
|
172
|
+
- spec/support/can_be_detail_history_shared_examples.rb
|
173
|
+
- spec/support/can_be_detail_shared_examples.rb
|
174
|
+
- spec/support/can_be_history_shared_examples.rb
|
175
|
+
- spec/support/can_be_shared_examples.rb
|
147
176
|
- spec/support/model_macros.rb
|
148
177
|
- spec/support/models.rb
|
149
178
|
- spec/support/schema.rb
|
@@ -159,12 +188,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
159
188
|
- - ! '>='
|
160
189
|
- !ruby/object:Gem::Version
|
161
190
|
version: '0'
|
191
|
+
segments:
|
192
|
+
- 0
|
193
|
+
hash: -227341645228978432
|
162
194
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
163
195
|
none: false
|
164
196
|
requirements:
|
165
197
|
- - ! '>='
|
166
198
|
- !ruby/object:Gem::Version
|
167
199
|
version: '0'
|
200
|
+
segments:
|
201
|
+
- 0
|
202
|
+
hash: -227341645228978432
|
168
203
|
requirements: []
|
169
204
|
rubyforge_project:
|
170
205
|
rubygems_version: 1.8.23
|
@@ -180,9 +215,14 @@ summary: CanBe allows you to track the type of your ActiveRecord model in a cons
|
|
180
215
|
test_files:
|
181
216
|
- spec/can_be/config_spec.rb
|
182
217
|
- spec/can_be/model_extensions_spec.rb
|
218
|
+
- spec/can_be/rspec/matchers/can_be_detail_matcher_spec.rb
|
219
|
+
- spec/can_be/rspec/matchers/can_be_matcher_spec.rb
|
183
220
|
- spec/can_be_spec.rb
|
184
221
|
- spec/spec_helper.rb
|
222
|
+
- spec/support/can_be_detail_history_shared_examples.rb
|
223
|
+
- spec/support/can_be_detail_shared_examples.rb
|
224
|
+
- spec/support/can_be_history_shared_examples.rb
|
225
|
+
- spec/support/can_be_shared_examples.rb
|
185
226
|
- spec/support/model_macros.rb
|
186
227
|
- spec/support/models.rb
|
187
228
|
- spec/support/schema.rb
|
188
|
-
has_rdoc:
|