upgrow 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/lib/generators/test_unit/input/input_generator.rb +22 -0
  3. data/lib/generators/test_unit/input/templates/input_test.rb.tt +8 -0
  4. data/lib/generators/test_unit/install/install_generator.rb +48 -0
  5. data/lib/generators/test_unit/record/record_generator.rb +22 -0
  6. data/lib/generators/test_unit/record/templates/record_test.rb.tt +8 -0
  7. data/lib/generators/test_unit/repository/repository_generator.rb +23 -0
  8. data/lib/generators/test_unit/repository/templates/repository_test.rb.tt +8 -0
  9. data/lib/generators/upgrow.rb +54 -0
  10. data/lib/generators/upgrow/action/USAGE +13 -0
  11. data/lib/generators/upgrow/action/action_generator.rb +24 -0
  12. data/lib/generators/upgrow/action/templates/action.rb.tt +9 -0
  13. data/lib/generators/upgrow/input/USAGE +10 -0
  14. data/lib/generators/upgrow/input/input_generator.rb +26 -0
  15. data/lib/generators/upgrow/input/templates/input.rb.tt +6 -0
  16. data/lib/generators/upgrow/install/USAGE +25 -0
  17. data/lib/generators/upgrow/install/install_generator.rb +56 -0
  18. data/lib/generators/upgrow/install/templates/app/actions/application_action.rb.tt +4 -0
  19. data/lib/generators/upgrow/install/templates/app/inputs/application_input.rb.tt +4 -0
  20. data/lib/generators/upgrow/install/templates/app/models/application_model.rb.tt +4 -0
  21. data/lib/generators/upgrow/install/templates/app/repositories/application_repository.rb.tt +4 -0
  22. data/lib/generators/upgrow/model/USAGE +10 -0
  23. data/lib/generators/upgrow/model/model_generator.rb +24 -0
  24. data/lib/generators/upgrow/model/templates/model.rb.tt +6 -0
  25. data/lib/generators/upgrow/record/record_generator.rb +34 -0
  26. data/lib/generators/upgrow/record/templates/record.rb.tt +6 -0
  27. data/lib/generators/upgrow/repository/USAGE +10 -0
  28. data/lib/generators/upgrow/repository/repository_generator.rb +27 -0
  29. data/lib/generators/upgrow/repository/templates/repository.rb.tt +6 -0
  30. data/lib/upgrow.rb +3 -0
  31. data/lib/upgrow/active_record_conversion.rb +2 -3
  32. data/lib/upgrow/active_record_queries.rb +1 -2
  33. data/lib/upgrow/active_record_schema.rb +1 -3
  34. data/lib/upgrow/basic_model.rb +11 -0
  35. data/lib/upgrow/model.rb +2 -1
  36. data/lib/upgrow/naming.rb +40 -0
  37. data/lib/upgrow/railtie.rb +9 -0
  38. data/lib/upgrow/record.rb +121 -0
  39. data/test/dummy/app/records/application_record.rb +2 -0
  40. data/test/dummy/app/records/article_record.rb +2 -3
  41. data/test/dummy/app/records/comment_record.rb +2 -3
  42. data/test/dummy/app/records/user_record.rb +2 -4
  43. data/test/dummy/app/repositories/article_repository.rb +3 -3
  44. data/test/test_unit/generators/input_generator_test.rb +56 -0
  45. data/test/test_unit/generators/install_generator_test.rb +138 -0
  46. data/test/test_unit/generators/record_generator_test.rb +56 -0
  47. data/test/test_unit/generators/repository_generator_test.rb +57 -0
  48. data/test/upgrow/active_record_conversion_test.rb +2 -2
  49. data/test/upgrow/active_record_schema_test.rb +1 -1
  50. data/test/upgrow/basic_model_test.rb +85 -0
  51. data/test/upgrow/generators/action_generator_test.rb +59 -0
  52. data/test/upgrow/generators/helper_test.rb +86 -0
  53. data/test/upgrow/generators/input_generator_test.rb +65 -0
  54. data/test/upgrow/generators/install_generator_test.rb +158 -0
  55. data/test/upgrow/generators/model_generator_test.rb +39 -0
  56. data/test/upgrow/generators/record_generator_test.rb +65 -0
  57. data/test/upgrow/generators/repository_generator_test.rb +50 -0
  58. data/test/upgrow/naming_test.rb +22 -0
  59. data/test/upgrow/record_test.rb +176 -0
  60. metadata +60 -3
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ require 'upgrow/naming'
6
+
7
+ module Upgrow
8
+ class NamingTest < ActiveSupport::TestCase
9
+ test '.record_to_model removes the Record suffix' do
10
+ assert_equal 'Article', Naming.record_to_model('ArticleRecord')
11
+ end
12
+
13
+ test '.model_to_record adds the Record suffix' do
14
+ assert_equal 'ArticleRecord', Naming.model_to_record('Article')
15
+ end
16
+
17
+ test '.repository_to_record removes the Repository suffix and adds the Record suffix' do
18
+ assert_equal 'ArticleRecord',
19
+ Naming.repository_to_record('ArticleRepository')
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,176 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ require 'active_record'
6
+ require 'upgrow/record'
7
+
8
+ module Upgrow
9
+ class RecordTest < ActiveSupport::TestCase
10
+ class ArticleRecord < ActiveRecord::Base
11
+ include Record
12
+ end
13
+
14
+ class BaseRecord < ActiveRecord::Base
15
+ include Record
16
+ self.abstract_class = true
17
+ end
18
+
19
+ class UserRecord < BaseRecord; end
20
+
21
+ setup do
22
+ @original_table_name = ArticleRecord.table_name
23
+ end
24
+
25
+ teardown do
26
+ ArticleRecord.table_name = @original_table_name
27
+ end
28
+
29
+ test '.table_name is inferred from the class name without the Record suffix' do
30
+ assert_equal 'articles', ArticleRecord.table_name
31
+ end
32
+
33
+ test '.table_name can be set explicitly' do
34
+ ArticleRecord.table_name = 'my_table'
35
+ assert_equal 'my_table', ArticleRecord.table_name
36
+ end
37
+
38
+ test '.table_name ignores inherited abstract classes' do
39
+ assert_equal 'users', UserRecord.table_name
40
+ end
41
+
42
+ test '.has_many sets a default foreign key based on the class name without the Record suffix' do
43
+ ArticleRecord.has_many(:comments)
44
+ reflection = ArticleRecord.reflect_on_association(:comments)
45
+ assert_equal 'article_id', reflection.foreign_key
46
+ end
47
+
48
+ test '.has_many accepts a foreign key value' do
49
+ ArticleRecord.has_many(:cats, foreign_key: 'my_custom_id')
50
+ reflection = ArticleRecord.reflect_on_association(:cats)
51
+ assert_equal 'my_custom_id', reflection.foreign_key
52
+ end
53
+
54
+ test '.has_many sets the association class name based on its name and the Record suffix' do
55
+ ArticleRecord.has_many(:paragraphs)
56
+ reflection = ArticleRecord.reflect_on_association(:paragraphs)
57
+ assert_equal 'ParagraphRecord', reflection.options.fetch(:class_name)
58
+ end
59
+
60
+ test '.has_many accepts a class name value' do
61
+ ArticleRecord.has_many(:birds, class_name: 'MyClass')
62
+ reflection = ArticleRecord.reflect_on_association(:birds)
63
+ assert_equal 'MyClass', reflection.options.fetch(:class_name)
64
+ end
65
+
66
+ test '.has_many sets the foreign key based on the :as option' do
67
+ ArticleRecord.has_many(:words, as: 'writable')
68
+ reflection = ArticleRecord.reflect_on_association(:words)
69
+ assert_equal 'writable_id', reflection.foreign_key
70
+ end
71
+
72
+ test '.has_one sets a default foreign key based on the class name without the Record suffix' do
73
+ ArticleRecord.has_one(:excerpt)
74
+ reflection = ArticleRecord.reflect_on_association(:excerpt)
75
+ assert_equal 'article_id', reflection.foreign_key
76
+ end
77
+
78
+ test '.has_one accepts a foreign key value' do
79
+ ArticleRecord.has_one(:title, foreign_key: 'my_custom_id')
80
+ reflection = ArticleRecord.reflect_on_association(:title)
81
+ assert_equal 'my_custom_id', reflection.foreign_key
82
+ end
83
+
84
+ test '.has_one sets the association class name based on its name and the Record suffix' do
85
+ ArticleRecord.has_one(:signature)
86
+ reflection = ArticleRecord.reflect_on_association(:signature)
87
+ assert_equal 'SignatureRecord', reflection.options.fetch(:class_name)
88
+ end
89
+
90
+ test '.has_one accepts a class name value' do
91
+ ArticleRecord.has_one(:image, class_name: 'MyClass')
92
+ reflection = ArticleRecord.reflect_on_association(:image)
93
+ assert_equal 'MyClass', reflection.options.fetch(:class_name)
94
+ end
95
+
96
+ test '.has_one sets the foreign key based on the :as option' do
97
+ ArticleRecord.has_one(:pin, as: 'pinnable')
98
+ reflection = ArticleRecord.reflect_on_association(:pin)
99
+ assert_equal 'pinnable_id', reflection.foreign_key
100
+ end
101
+
102
+ test '.belongs_to sets a default foreign key based on the association name' do
103
+ ArticleRecord.belongs_to(:user)
104
+ reflection = ArticleRecord.reflect_on_association(:user)
105
+ assert_equal 'user_id', reflection.foreign_key
106
+ end
107
+
108
+ test '.belongs_to accepts a foreign key value' do
109
+ ArticleRecord.belongs_to(:book, foreign_key: 'custom_id')
110
+ reflection = ArticleRecord.reflect_on_association(:book)
111
+ assert_equal 'custom_id', reflection.foreign_key
112
+ end
113
+
114
+ test '.belongs_to sets the association class name based on its name and the Record suffix' do
115
+ ArticleRecord.belongs_to(:category)
116
+ reflection = ArticleRecord.reflect_on_association(:category)
117
+ assert_equal 'CategoryRecord', reflection.options.fetch(:class_name)
118
+ end
119
+
120
+ test '.belongs_to accepts a class name value' do
121
+ ArticleRecord.belongs_to(:section, class_name: 'MyClass')
122
+ reflection = ArticleRecord.reflect_on_association(:section)
123
+ assert_equal 'MyClass', reflection.options.fetch(:class_name)
124
+ end
125
+
126
+ test '.has_and_belongs_to_many sets a default foreign key based on the class name without the Record suffix' do
127
+ ArticleRecord.has_and_belongs_to_many(:tags)
128
+ reflection = ArticleRecord.reflect_on_association(:tags)
129
+ assert_equal 'article_id', reflection.foreign_key
130
+ end
131
+
132
+ test '.has_and_belongs_to_many accepts a foreign key value' do
133
+ ArticleRecord.has_and_belongs_to_many(:stars, foreign_key: 'my_custom_id')
134
+ reflection = ArticleRecord.reflect_on_association(:stars)
135
+ assert_equal 'my_custom_id', reflection.foreign_key
136
+ end
137
+
138
+ test '.has_and_belongs_to_many sets a default association foreign key based on the class name without the Record suffix' do
139
+ ArticleRecord.has_and_belongs_to_many(:prizes)
140
+ reflection = ArticleRecord.reflect_on_association(:prizes)
141
+ assert_equal 'prize_id', reflection.association_foreign_key
142
+ end
143
+
144
+ test '.has_and_belongs_to_many sets a default association foreign key based on the given class name' do
145
+ ArticleRecord.has_and_belongs_to_many(:trees, class_name: 'MyClass')
146
+ reflection = ArticleRecord.reflect_on_association(:trees)
147
+ assert_equal 'my_class_id', reflection.association_foreign_key
148
+ end
149
+
150
+ test '.has_and_belongs_to_many sets a default association foreign key based on the given class name without the Record suffix' do
151
+ ArticleRecord.has_and_belongs_to_many(:dogs, class_name: 'MyClassRecord')
152
+ reflection = ArticleRecord.reflect_on_association(:dogs)
153
+ assert_equal 'my_class_id', reflection.association_foreign_key
154
+ end
155
+
156
+ test '.has_and_belongs_to_many accepts an association foreign key value' do
157
+ ArticleRecord.has_and_belongs_to_many(
158
+ :countries, association_foreign_key: 'my_custom_id'
159
+ )
160
+ reflection = ArticleRecord.reflect_on_association(:countries)
161
+ assert_equal 'my_custom_id', reflection.association_foreign_key
162
+ end
163
+
164
+ test '.has_and_belongs_to_many sets the association class name based on its name and the Record suffix' do
165
+ ArticleRecord.has_and_belongs_to_many(:flags)
166
+ reflection = ArticleRecord.reflect_on_association(:flags)
167
+ assert_equal 'FlagRecord', reflection.options.fetch(:class_name)
168
+ end
169
+
170
+ test '.has_and_belongs_to_many accepts a class name value' do
171
+ ArticleRecord.has_and_belongs_to_many(:horses, class_name: 'MyClass')
172
+ reflection = ArticleRecord.reflect_on_association(:horses)
173
+ assert_equal 'MyClass', reflection.options.fetch(:class_name)
174
+ end
175
+ end
176
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: upgrow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify Engineering
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-11 00:00:00.000000000 Z
11
+ date: 2021-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -32,6 +32,34 @@ extra_rdoc_files: []
32
32
  files:
33
33
  - README.md
34
34
  - Rakefile
35
+ - lib/generators/test_unit/input/input_generator.rb
36
+ - lib/generators/test_unit/input/templates/input_test.rb.tt
37
+ - lib/generators/test_unit/install/install_generator.rb
38
+ - lib/generators/test_unit/record/record_generator.rb
39
+ - lib/generators/test_unit/record/templates/record_test.rb.tt
40
+ - lib/generators/test_unit/repository/repository_generator.rb
41
+ - lib/generators/test_unit/repository/templates/repository_test.rb.tt
42
+ - lib/generators/upgrow.rb
43
+ - lib/generators/upgrow/action/USAGE
44
+ - lib/generators/upgrow/action/action_generator.rb
45
+ - lib/generators/upgrow/action/templates/action.rb.tt
46
+ - lib/generators/upgrow/input/USAGE
47
+ - lib/generators/upgrow/input/input_generator.rb
48
+ - lib/generators/upgrow/input/templates/input.rb.tt
49
+ - lib/generators/upgrow/install/USAGE
50
+ - lib/generators/upgrow/install/install_generator.rb
51
+ - lib/generators/upgrow/install/templates/app/actions/application_action.rb.tt
52
+ - lib/generators/upgrow/install/templates/app/inputs/application_input.rb.tt
53
+ - lib/generators/upgrow/install/templates/app/models/application_model.rb.tt
54
+ - lib/generators/upgrow/install/templates/app/repositories/application_repository.rb.tt
55
+ - lib/generators/upgrow/model/USAGE
56
+ - lib/generators/upgrow/model/model_generator.rb
57
+ - lib/generators/upgrow/model/templates/model.rb.tt
58
+ - lib/generators/upgrow/record/record_generator.rb
59
+ - lib/generators/upgrow/record/templates/record.rb.tt
60
+ - lib/generators/upgrow/repository/USAGE
61
+ - lib/generators/upgrow/repository/repository_generator.rb
62
+ - lib/generators/upgrow/repository/templates/repository.rb.tt
35
63
  - lib/upgrow.rb
36
64
  - lib/upgrow/action.rb
37
65
  - lib/upgrow/actions.rb
@@ -46,6 +74,9 @@ files:
46
74
  - lib/upgrow/input.rb
47
75
  - lib/upgrow/model.rb
48
76
  - lib/upgrow/model_schema.rb
77
+ - lib/upgrow/naming.rb
78
+ - lib/upgrow/railtie.rb
79
+ - lib/upgrow/record.rb
49
80
  - lib/upgrow/repository.rb
50
81
  - lib/upgrow/result.rb
51
82
  - lib/upgrow/schema.rb
@@ -122,6 +153,10 @@ files:
122
153
  - test/system/sign_out_test.rb
123
154
  - test/system/sign_up_test.rb
124
155
  - test/test_helper.rb
156
+ - test/test_unit/generators/input_generator_test.rb
157
+ - test/test_unit/generators/install_generator_test.rb
158
+ - test/test_unit/generators/record_generator_test.rb
159
+ - test/test_unit/generators/repository_generator_test.rb
125
160
  - test/upgrow/action_test.rb
126
161
  - test/upgrow/actions_test.rb
127
162
  - test/upgrow/active_record_conversion_test.rb
@@ -130,18 +165,27 @@ files:
130
165
  - test/upgrow/basic_model_test.rb
131
166
  - test/upgrow/basic_repository_test.rb
132
167
  - test/upgrow/documentation_test.rb
168
+ - test/upgrow/generators/action_generator_test.rb
169
+ - test/upgrow/generators/helper_test.rb
170
+ - test/upgrow/generators/input_generator_test.rb
171
+ - test/upgrow/generators/install_generator_test.rb
172
+ - test/upgrow/generators/model_generator_test.rb
173
+ - test/upgrow/generators/record_generator_test.rb
174
+ - test/upgrow/generators/repository_generator_test.rb
133
175
  - test/upgrow/immutable_object_test.rb
134
176
  - test/upgrow/immutable_struct_test.rb
135
177
  - test/upgrow/input_test.rb
136
178
  - test/upgrow/model_schema_test.rb
137
179
  - test/upgrow/model_test.rb
180
+ - test/upgrow/naming_test.rb
181
+ - test/upgrow/record_test.rb
138
182
  - test/upgrow/result_test.rb
139
183
  - test/upgrow/schema_test.rb
140
184
  homepage: https://github.com/Shopify/upgrow
141
185
  licenses:
142
186
  - MIT
143
187
  metadata:
144
- source_code_uri: https://github.com/Shopify/upgrow/tree/v0.0.4
188
+ source_code_uri: https://github.com/Shopify/upgrow/tree/v0.0.5
145
189
  allowed_push_host: https://rubygems.org
146
190
  post_install_message:
147
191
  rdoc_options: []
@@ -236,6 +280,10 @@ test_files:
236
280
  - test/system/sign_out_test.rb
237
281
  - test/system/sign_up_test.rb
238
282
  - test/test_helper.rb
283
+ - test/test_unit/generators/input_generator_test.rb
284
+ - test/test_unit/generators/install_generator_test.rb
285
+ - test/test_unit/generators/record_generator_test.rb
286
+ - test/test_unit/generators/repository_generator_test.rb
239
287
  - test/upgrow/action_test.rb
240
288
  - test/upgrow/actions_test.rb
241
289
  - test/upgrow/active_record_conversion_test.rb
@@ -244,10 +292,19 @@ test_files:
244
292
  - test/upgrow/basic_model_test.rb
245
293
  - test/upgrow/basic_repository_test.rb
246
294
  - test/upgrow/documentation_test.rb
295
+ - test/upgrow/generators/action_generator_test.rb
296
+ - test/upgrow/generators/helper_test.rb
297
+ - test/upgrow/generators/input_generator_test.rb
298
+ - test/upgrow/generators/install_generator_test.rb
299
+ - test/upgrow/generators/model_generator_test.rb
300
+ - test/upgrow/generators/record_generator_test.rb
301
+ - test/upgrow/generators/repository_generator_test.rb
247
302
  - test/upgrow/immutable_object_test.rb
248
303
  - test/upgrow/immutable_struct_test.rb
249
304
  - test/upgrow/input_test.rb
250
305
  - test/upgrow/model_schema_test.rb
251
306
  - test/upgrow/model_test.rb
307
+ - test/upgrow/naming_test.rb
308
+ - test/upgrow/record_test.rb
252
309
  - test/upgrow/result_test.rb
253
310
  - test/upgrow/schema_test.rb