inline_forms 8.1.75 → 8.1.76
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -1
- data/README.rdoc +1 -1
- data/inline_forms.gemspec +0 -1
- data/lib/generators/USAGE +1 -1
- data/lib/generators/inline_forms_addto/USAGE +1 -1
- data/lib/generators/inline_forms_addto_generator.rb +30 -139
- data/lib/generators/inline_forms_attribute_list_editor.rb +211 -0
- data/lib/generators/inline_forms_attribute_overrides.rb +9 -0
- data/lib/generators/inline_forms_generator.rb +16 -35
- data/lib/inline_forms/gem_files.rb +20 -17
- data/lib/inline_forms/version.rb +1 -1
- metadata +3 -142
- data/.document +0 -5
- data/.forgejo/brakeman-engine.sh +0 -51
- data/.forgejo/brakeman.ignore +0 -211
- data/.forgejo/workflows/ci.yml +0 -74
- data/.forgejo/workflows/full-gate.yml +0 -104
- data/.gitignore +0 -39
- data/.rubocop.yml +0 -31
- data/.rubocop_todo.yml +0 -151
- data/.ruby-version +0 -1
- data/Gemfile +0 -28
- data/Rakefile +0 -103
- data/script/migrate_form_elements.rb +0 -62
- data/test/archived_form_elements_test.rb +0 -101
- data/test/attribute_list_test.rb +0 -165
- data/test/dummy/app/controllers/application_controller.rb +0 -32
- data/test/dummy/app/controllers/daily_reports_controller.rb +0 -4
- data/test/dummy/app/controllers/documents_controller.rb +0 -4
- data/test/dummy/app/controllers/dossiers_controller.rb +0 -4
- data/test/dummy/app/controllers/gizmos_controller.rb +0 -4
- data/test/dummy/app/controllers/machines_controller.rb +0 -4
- data/test/dummy/app/controllers/parts_controller.rb +0 -4
- data/test/dummy/app/controllers/stats_controller.rb +0 -10
- data/test/dummy/app/controllers/widgets_controller.rb +0 -4
- data/test/dummy/app/helpers/application_helper.rb +0 -13
- data/test/dummy/app/models/ability.rb +0 -15
- data/test/dummy/app/models/application_record.rb +0 -27
- data/test/dummy/app/models/daily_report.rb +0 -21
- data/test/dummy/app/models/document.rb +0 -33
- data/test/dummy/app/models/dossier.rb +0 -38
- data/test/dummy/app/models/gizmo.rb +0 -25
- data/test/dummy/app/models/kind.rb +0 -8
- data/test/dummy/app/models/machine.rb +0 -24
- data/test/dummy/app/models/part.rb +0 -17
- data/test/dummy/app/models/phase.rb +0 -9
- data/test/dummy/app/models/widget.rb +0 -40
- data/test/dummy/app/views/_gizmos_search.html.erb +0 -3
- data/test/dummy/app/views/stats/show.html.erb +0 -1
- data/test/dummy/config/application.rb +0 -49
- data/test/dummy/config/database.yml +0 -3
- data/test/dummy/config/environment.rb +0 -5
- data/test/dummy/config/initializers/inline_forms.rb +0 -6
- data/test/dummy/config/initializers/paper_trail_yaml_safe_load.rb +0 -28
- data/test/dummy/config/routes.rb +0 -51
- data/test/form_element_from_callee_test.rb +0 -73
- data/test/gem_package_modes_test.rb +0 -86
- data/test/inline_edit_polymorphic_path_test.rb +0 -81
- data/test/inline_forms_addto_generator_test.rb +0 -584
- data/test/inline_forms_generator_test.rb +0 -301
- data/test/integration/csp_nonce_test.rb +0 -50
- data/test/integration/dropdown_test.rb +0 -186
- data/test/integration/field_authorization_test.rb +0 -131
- data/test/integration/file_slots_test.rb +0 -665
- data/test/integration/html_refusal_test.rb +0 -152
- data/test/integration/inline_forms_crud_test.rb +0 -165
- data/test/integration/layout_title_test.rb +0 -56
- data/test/integration/legacy_elements_test.rb +0 -99
- data/test/integration/list_params_test.rb +0 -200
- data/test/integration/locale_test.rb +0 -234
- data/test/integration/model_top_bar_test.rb +0 -85
- data/test/integration/native_inputs_test.rb +0 -138
- data/test/integration/open_after_create_test.rb +0 -85
- data/test/integration/pending_migration_gate_test.rb +0 -75
- data/test/integration/presentation_escaping_test.rb +0 -80
- data/test/integration/revert_skipped_columns_test.rb +0 -59
- data/test/integration/schema_batch_pipeline_test.rb +0 -189
- data/test/integration/schema_edit_test.rb +0 -141
- data/test/integration/schema_preview_test.rb +0 -65
- data/test/integration/turbo_menu_test.rb +0 -60
- data/test/integration_test_helper.rb +0 -203
- data/test/plain_text_configuration_test.rb +0 -90
- data/test/schema_apply_test.rb +0 -73
- data/test/schema_intent_test.rb +0 -53
- data/test/schema_label_test.rb +0 -61
- data/test/tabs_test.rb +0 -98
- data/test/test_helper.rb +0 -10
- data/test/user_model_config_test.rb +0 -44
|
@@ -1,584 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative "test_helper"
|
|
4
|
-
require "tmpdir"
|
|
5
|
-
require "fileutils"
|
|
6
|
-
require "logger"
|
|
7
|
-
require "rails"
|
|
8
|
-
require "active_record"
|
|
9
|
-
require "rails/generators"
|
|
10
|
-
require "inline_forms"
|
|
11
|
-
require_relative "../lib/generators/inline_forms_addto_generator"
|
|
12
|
-
|
|
13
|
-
class InlineFormsAddtoGeneratorTest < Minitest::Test
|
|
14
|
-
GENERATOR_SHAPED_MODEL = <<~RUBY
|
|
15
|
-
class Widget < ApplicationRecord
|
|
16
|
-
|
|
17
|
-
belongs_to :category
|
|
18
|
-
|
|
19
|
-
def inline_forms_attribute_list
|
|
20
|
-
@inline_forms_attribute_list ||= [
|
|
21
|
-
[ :name, :text_field ],
|
|
22
|
-
[ :category, :belongs_to ],
|
|
23
|
-
]
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
end
|
|
27
|
-
RUBY
|
|
28
|
-
|
|
29
|
-
INSTALLER_SHAPED_USER = <<~RUBY
|
|
30
|
-
class User < ApplicationRecord
|
|
31
|
-
|
|
32
|
-
devise :database_authenticatable
|
|
33
|
-
belongs_to :locale
|
|
34
|
-
has_and_belongs_to_many :roles
|
|
35
|
-
|
|
36
|
-
def _presentation
|
|
37
|
-
"\#{name}"
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def inline_forms_attribute_list
|
|
41
|
-
@inline_forms_attribute_list ||= [
|
|
42
|
-
[ :header_user_login, :header ],
|
|
43
|
-
[ :name, :text_field ],
|
|
44
|
-
[ :email, :text_field ],
|
|
45
|
-
[ :locale, :dropdown ],
|
|
46
|
-
[ :password, :devise_password_field ],
|
|
47
|
-
[ :header_user_roles, :header ],
|
|
48
|
-
[ :roles, :check_list ],
|
|
49
|
-
]
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
end
|
|
53
|
-
RUBY
|
|
54
|
-
|
|
55
|
-
BARE_MODEL = <<~RUBY
|
|
56
|
-
class Bare < ApplicationRecord
|
|
57
|
-
end
|
|
58
|
-
RUBY
|
|
59
|
-
|
|
60
|
-
# A model whose list ends with a trailing metadata block (a :header above
|
|
61
|
-
# :info / timestamps rows). New rows must land above the header, not after
|
|
62
|
-
# the info rows.
|
|
63
|
-
METADATA_MODEL = <<~RUBY
|
|
64
|
-
class Profile < ApplicationRecord
|
|
65
|
-
|
|
66
|
-
def inline_forms_attribute_list
|
|
67
|
-
@inline_forms_attribute_list ||= [
|
|
68
|
-
[ :name, :text_field ],
|
|
69
|
-
[ :bio, :text_area ],
|
|
70
|
-
[ :header_meta, :header ],
|
|
71
|
-
[ :encrypted_password, :info ],
|
|
72
|
-
[ :created_at, :info ],
|
|
73
|
-
[ :updated_at, :info ],
|
|
74
|
-
]
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
end
|
|
78
|
-
RUBY
|
|
79
|
-
|
|
80
|
-
# List closed with `].freeze` and a method defined *after* the list. The old
|
|
81
|
-
# greedy `]\\n end` regex could mismatch here; the bracket-depth scan must not.
|
|
82
|
-
FROZEN_LIST_MODEL = <<~RUBY
|
|
83
|
-
class Gadget < ApplicationRecord
|
|
84
|
-
|
|
85
|
-
def inline_forms_attribute_list
|
|
86
|
-
@inline_forms_attribute_list ||= [
|
|
87
|
-
[ :name, :text_field ],
|
|
88
|
-
[ :size, :integer_field ],
|
|
89
|
-
].freeze
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
def _presentation
|
|
93
|
-
"\#{name}"
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
end
|
|
97
|
-
RUBY
|
|
98
|
-
|
|
99
|
-
def setup
|
|
100
|
-
@destination_root = Dir.mktmpdir("inline_forms_addto_generator_test")
|
|
101
|
-
mkdir_p("app/models")
|
|
102
|
-
mkdir_p("db/migrate")
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
def teardown
|
|
106
|
-
FileUtils.remove_entry(@destination_root) if @destination_root && Dir.exist?(@destination_root)
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
def test_raises_when_model_file_missing
|
|
110
|
-
stdout, stderr = capture_io do
|
|
111
|
-
run_generator("DoesNotExist", "occupation:string")
|
|
112
|
-
end
|
|
113
|
-
output = "#{stdout}#{stderr}"
|
|
114
|
-
assert_includes(output, "rails g inline_forms DoesNotExist")
|
|
115
|
-
refute(File.exist?(File.join(@destination_root, "app/models/does_not_exist.rb")))
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
def test_adds_scalar_column_belongs_to_rich_text_and_image_field_to_existing_model
|
|
119
|
-
write_model("widget.rb", GENERATOR_SHAPED_MODEL)
|
|
120
|
-
|
|
121
|
-
run_generator(
|
|
122
|
-
"Widget",
|
|
123
|
-
"occupation:string",
|
|
124
|
-
"organization:belongs_to",
|
|
125
|
-
"supplier:dropdown",
|
|
126
|
-
"bio:rich_text",
|
|
127
|
-
"avatar:image_field"
|
|
128
|
-
)
|
|
129
|
-
|
|
130
|
-
model = read("app/models/widget.rb")
|
|
131
|
-
migration = read_single_addto_migration_for("widgets")
|
|
132
|
-
|
|
133
|
-
assert_includes(model, "belongs_to :organization")
|
|
134
|
-
assert_includes(model, "belongs_to :supplier")
|
|
135
|
-
assert_includes(model, "has_rich_text :bio")
|
|
136
|
-
assert_includes(model, "mount_uploader :avatar, AvatarUploader")
|
|
137
|
-
assert_includes(model, '[ :occupation, :text_field ]')
|
|
138
|
-
# :belongs_to is a relation -> no row in inline_forms_attribute_list
|
|
139
|
-
# (matches InlineFormsGenerator semantics). :dropdown is not a relation
|
|
140
|
-
# at lookup time, so it does get a row.
|
|
141
|
-
refute_includes(model, '[ :organization, :belongs_to ]')
|
|
142
|
-
assert_includes(model, '[ :supplier, :dropdown ]')
|
|
143
|
-
assert_includes(model, '[ :avatar, :image_field ]')
|
|
144
|
-
|
|
145
|
-
assert_includes(migration, "add_column :widgets, :occupation, :string")
|
|
146
|
-
assert_includes(migration, "add_reference :widgets, :organization, foreign_key: true")
|
|
147
|
-
assert_includes(migration, "add_reference :widgets, :supplier, foreign_key: true")
|
|
148
|
-
assert_includes(migration, "add_column :widgets, :avatar, :string")
|
|
149
|
-
refute_includes(migration, ":bio")
|
|
150
|
-
refute_includes(migration, "create_table")
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
def test_header_adds_a_row_but_no_migration_column
|
|
154
|
-
write_model("widget.rb", GENERATOR_SHAPED_MODEL)
|
|
155
|
-
|
|
156
|
-
capture_io { run_generator("Widget", "section:header") }
|
|
157
|
-
|
|
158
|
-
model = read("app/models/widget.rb")
|
|
159
|
-
assert_includes(model, "[ :section, :header ]")
|
|
160
|
-
# A header needs no column, so no migration should be written at all.
|
|
161
|
-
refute_addto_migration_for("widgets")
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
def test_header_alongside_scalar_migrates_only_the_scalar
|
|
165
|
-
write_model("widget.rb", GENERATOR_SHAPED_MODEL)
|
|
166
|
-
|
|
167
|
-
capture_io { run_generator("Widget", "section:header", "note:string") }
|
|
168
|
-
|
|
169
|
-
migration = read_single_addto_migration_for("widgets")
|
|
170
|
-
assert_includes(migration, "add_column :widgets, :note, :string")
|
|
171
|
-
refute_includes(migration, ":section")
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
def test_idempotent_rerun_does_not_duplicate_lines
|
|
175
|
-
write_model("widget.rb", GENERATOR_SHAPED_MODEL)
|
|
176
|
-
|
|
177
|
-
run_generator("Widget", "occupation:string", "organization:belongs_to")
|
|
178
|
-
sleep 1 # unique migration timestamp on second run
|
|
179
|
-
run_generator("Widget", "occupation:string", "organization:belongs_to")
|
|
180
|
-
|
|
181
|
-
model = read("app/models/widget.rb")
|
|
182
|
-
|
|
183
|
-
assert_equal(1, model.scan("belongs_to :organization").size)
|
|
184
|
-
assert_equal(1, model.scan('[ :occupation, :text_field ]').size)
|
|
185
|
-
end
|
|
186
|
-
|
|
187
|
-
def test_appends_row_to_installer_shaped_user_attribute_list
|
|
188
|
-
write_model("user.rb", INSTALLER_SHAPED_USER)
|
|
189
|
-
|
|
190
|
-
run_generator("User", "occupation:string", "birthdate:date")
|
|
191
|
-
|
|
192
|
-
model = read("app/models/user.rb")
|
|
193
|
-
|
|
194
|
-
assert_includes(model, '[ :occupation, :text_field ]')
|
|
195
|
-
assert_includes(model, '[ :birthdate, :date_select ]')
|
|
196
|
-
refute_match(/\]\s*\n\s*\[\s*:occupation/, model)
|
|
197
|
-
|
|
198
|
-
user_array_section = model[/@inline_forms_attribute_list \|\|=\s*\[(.|\n)*?\n\s*\]/]
|
|
199
|
-
assert(user_array_section, "could not locate @inline_forms_attribute_list array")
|
|
200
|
-
assert_match(/\[ :roles,.*\].*?\[ :occupation\b/m, user_array_section)
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
def test_bare_model_gets_fresh_attribute_list_method
|
|
204
|
-
write_model("bare.rb", BARE_MODEL)
|
|
205
|
-
|
|
206
|
-
out, _err = capture_io do
|
|
207
|
-
run_generator("Bare", "occupation:string")
|
|
208
|
-
end
|
|
209
|
-
|
|
210
|
-
model = read("app/models/bare.rb")
|
|
211
|
-
|
|
212
|
-
assert_includes(out, "no inline_forms_attribute_list found")
|
|
213
|
-
assert_includes(model, "def inline_forms_attribute_list")
|
|
214
|
-
assert_includes(model, '[ :occupation, :text_field ]')
|
|
215
|
-
end
|
|
216
|
-
|
|
217
|
-
def test_unknown_type_raises_thor_error_by_default
|
|
218
|
-
write_model("widget.rb", GENERATOR_SHAPED_MODEL)
|
|
219
|
-
|
|
220
|
-
stdout, stderr = capture_io do
|
|
221
|
-
run_generator("Widget", "payload:not_a_real_type")
|
|
222
|
-
end
|
|
223
|
-
output = "#{stdout}#{stderr}"
|
|
224
|
-
assert_includes(output, "Unknown field type(s): payload:not_a_real_type")
|
|
225
|
-
assert_includes(output, "--allow-unknown")
|
|
226
|
-
|
|
227
|
-
refute_addto_migration_for("widgets")
|
|
228
|
-
end
|
|
229
|
-
|
|
230
|
-
def test_allow_unknown_keeps_legacy_commented_output
|
|
231
|
-
write_model("widget.rb", GENERATOR_SHAPED_MODEL)
|
|
232
|
-
|
|
233
|
-
run_generator("Widget", "payload:not_a_real_type", "--allow-unknown")
|
|
234
|
-
|
|
235
|
-
model = read("app/models/widget.rb")
|
|
236
|
-
migration = read_single_addto_migration_for("widgets")
|
|
237
|
-
|
|
238
|
-
assert_includes(model, '[ :payload, :unknown ]')
|
|
239
|
-
assert_includes(migration, "# add_column :widgets, :payload, :unknown")
|
|
240
|
-
end
|
|
241
|
-
|
|
242
|
-
def test_install_time_only_names_are_rejected
|
|
243
|
-
write_model("widget.rb", GENERATOR_SHAPED_MODEL)
|
|
244
|
-
|
|
245
|
-
%w[_no_model _no_migration _id _enabled].each do |forbidden|
|
|
246
|
-
stdout, stderr = capture_io do
|
|
247
|
-
run_generator("Widget", "#{forbidden}:yes")
|
|
248
|
-
end
|
|
249
|
-
output = "#{stdout}#{stderr}"
|
|
250
|
-
assert_includes(output, forbidden, "expected error to mention #{forbidden}")
|
|
251
|
-
assert_includes(output, "install-time only")
|
|
252
|
-
end
|
|
253
|
-
end
|
|
254
|
-
|
|
255
|
-
def test_skips_replace_only_names_without_replace_flag
|
|
256
|
-
write_model("widget.rb", GENERATOR_SHAPED_MODEL)
|
|
257
|
-
|
|
258
|
-
out, _err = capture_io do
|
|
259
|
-
run_generator("Widget", "_list_order:title")
|
|
260
|
-
end
|
|
261
|
-
|
|
262
|
-
assert_includes(out, "_list_order:title")
|
|
263
|
-
assert_includes(out, "skipped")
|
|
264
|
-
model = read("app/models/widget.rb")
|
|
265
|
-
refute_includes(model, "scope :inline_forms_list, -> { order(:title, :id) }")
|
|
266
|
-
end
|
|
267
|
-
|
|
268
|
-
def test_replace_flag_rewrites_list_order_scope
|
|
269
|
-
initial = GENERATOR_SHAPED_MODEL.sub(
|
|
270
|
-
/class Widget < ApplicationRecord\n/,
|
|
271
|
-
"class Widget < ApplicationRecord\n\n scope :inline_forms_list, -> { order(:name, :id) }\n def <=>(other)\n self.name <=> other.name\n end\n"
|
|
272
|
-
)
|
|
273
|
-
write_model("widget.rb", initial)
|
|
274
|
-
|
|
275
|
-
run_generator("Widget", "_list_order:title", "--replace")
|
|
276
|
-
|
|
277
|
-
model = read("app/models/widget.rb")
|
|
278
|
-
assert_includes(model, "scope :inline_forms_list, -> { order(:title, :id) }")
|
|
279
|
-
refute_includes(model, "order(:name, :id)")
|
|
280
|
-
assert_includes(model, "self.title <=> other.title")
|
|
281
|
-
refute_includes(model, "self.name <=> other.name")
|
|
282
|
-
end
|
|
283
|
-
|
|
284
|
-
def test_smart_placement_lands_above_trailing_metadata_block
|
|
285
|
-
write_model("profile.rb", METADATA_MODEL)
|
|
286
|
-
|
|
287
|
-
capture_io { run_generator("Profile", "occupation:string") }
|
|
288
|
-
|
|
289
|
-
model = read("app/models/profile.rb")
|
|
290
|
-
assert_includes(model, "[ :occupation, :text_field ]")
|
|
291
|
-
# Above the metadata header, and therefore above every :info row.
|
|
292
|
-
assert(model.index("[ :occupation") < model.index("[ :header_meta"),
|
|
293
|
-
"occupation should be inserted above the metadata header")
|
|
294
|
-
assert(model.index("[ :bio") < model.index("[ :occupation"),
|
|
295
|
-
"occupation should stay below the existing content rows")
|
|
296
|
-
end
|
|
297
|
-
|
|
298
|
-
def test_frozen_list_and_trailing_method_do_not_break_insertion
|
|
299
|
-
write_model("gadget.rb", FROZEN_LIST_MODEL)
|
|
300
|
-
|
|
301
|
-
capture_io { run_generator("Gadget", "color:string") }
|
|
302
|
-
|
|
303
|
-
model = read("app/models/gadget.rb")
|
|
304
|
-
assert_includes(model, "[ :color, :text_field ]")
|
|
305
|
-
# Still a single, valid, frozen list; the trailing method is untouched.
|
|
306
|
-
assert_equal(1, model.scan("].freeze").size)
|
|
307
|
-
assert_includes(model, "def _presentation")
|
|
308
|
-
assert(model.index("[ :size") < model.index("[ :color"),
|
|
309
|
-
"color should be appended after the last existing row")
|
|
310
|
-
assert(model.index("[ :color") < model.index("].freeze"),
|
|
311
|
-
"color should be inside the frozen array")
|
|
312
|
-
refute_includes(model, "no inline_forms_attribute_list found")
|
|
313
|
-
end
|
|
314
|
-
|
|
315
|
-
def test_after_option_places_row_after_named_attribute
|
|
316
|
-
write_model("widget.rb", GENERATOR_SHAPED_MODEL)
|
|
317
|
-
|
|
318
|
-
capture_io { run_generator("Widget", "occupation:string", "--after=name") }
|
|
319
|
-
|
|
320
|
-
model = read("app/models/widget.rb")
|
|
321
|
-
assert(model.index("[ :name") < model.index("[ :occupation"))
|
|
322
|
-
assert(model.index("[ :occupation") < model.index("[ :category"))
|
|
323
|
-
end
|
|
324
|
-
|
|
325
|
-
def test_before_option_places_row_before_named_attribute
|
|
326
|
-
write_model("widget.rb", GENERATOR_SHAPED_MODEL)
|
|
327
|
-
|
|
328
|
-
capture_io { run_generator("Widget", "occupation:string", "--before=category") }
|
|
329
|
-
|
|
330
|
-
model = read("app/models/widget.rb")
|
|
331
|
-
assert(model.index("[ :occupation") < model.index("[ :category"))
|
|
332
|
-
end
|
|
333
|
-
|
|
334
|
-
# What `rails g inline_forms` writes: no comma after the last row.
|
|
335
|
-
def test_list_without_a_trailing_comma
|
|
336
|
-
write_model("widget.rb", widget_with_rows("[ :name, :text_field ],", "[ :size, :integer_field ]"))
|
|
337
|
-
|
|
338
|
-
capture_io { run_generator("Widget", "color:string") }
|
|
339
|
-
|
|
340
|
-
assert_includes(read("app/models/widget.rb"),
|
|
341
|
-
" [ :size, :integer_field ],\n [ :color, :text_field ]\n ]")
|
|
342
|
-
assert_valid_ruby("app/models/widget.rb")
|
|
343
|
-
end
|
|
344
|
-
|
|
345
|
-
def test_last_row_with_a_trailing_comment
|
|
346
|
-
write_model("widget.rb", widget_with_rows("[ :size, :dropdown_with_values, { 1 => \"a\" }, [ 1 ] ] # see [x], y"))
|
|
347
|
-
|
|
348
|
-
capture_io { run_generator("Widget", "color:string") }
|
|
349
|
-
|
|
350
|
-
assert_includes(read("app/models/widget.rb"),
|
|
351
|
-
"[ 1 ] ], # see [x], y\n [ :color, :text_field ]\n ]")
|
|
352
|
-
assert_valid_ruby("app/models/widget.rb")
|
|
353
|
-
end
|
|
354
|
-
|
|
355
|
-
def test_row_that_already_has_its_comma_before_a_comment
|
|
356
|
-
write_model("widget.rb", widget_with_rows("[ :size, :integer_field ], # cm"))
|
|
357
|
-
|
|
358
|
-
capture_io { run_generator("Widget", "color:string") }
|
|
359
|
-
|
|
360
|
-
assert_includes(read("app/models/widget.rb"), "[ :size, :integer_field ], # cm\n [ :color, :text_field ]\n")
|
|
361
|
-
assert_valid_ruby("app/models/widget.rb")
|
|
362
|
-
end
|
|
363
|
-
|
|
364
|
-
def test_row_before_another_gets_a_comma
|
|
365
|
-
write_model("widget.rb", widget_with_rows("[ :name, :text_field ],", "[ :size, :integer_field ]"))
|
|
366
|
-
|
|
367
|
-
capture_io { run_generator("Widget", "color:string", "--before=size") }
|
|
368
|
-
|
|
369
|
-
assert_includes(read("app/models/widget.rb"),
|
|
370
|
-
" [ :color, :text_field ],\n [ :size, :integer_field ]\n ]")
|
|
371
|
-
assert_valid_ruby("app/models/widget.rb")
|
|
372
|
-
end
|
|
373
|
-
|
|
374
|
-
def test_rerun_recognises_the_has_many_destroy_line_from_before_8_1_72
|
|
375
|
-
write_model("widget.rb", widget_with_rows("[ :name, :text_field ]")
|
|
376
|
-
.sub("ApplicationRecord\n", "ApplicationRecord\n has_many :parts, :dependent => :destroy\n"))
|
|
377
|
-
|
|
378
|
-
capture_io { run_generator("Widget", "parts:has_many_destroy") }
|
|
379
|
-
|
|
380
|
-
assert_equal(1, read("app/models/widget.rb").scan("has_many :parts").size)
|
|
381
|
-
end
|
|
382
|
-
|
|
383
|
-
def test_missing_anchor_warns_and_falls_back_to_default
|
|
384
|
-
write_model("widget.rb", GENERATOR_SHAPED_MODEL)
|
|
385
|
-
|
|
386
|
-
out, _err = capture_io do
|
|
387
|
-
run_generator("Widget", "occupation:string", "--after=nope")
|
|
388
|
-
end
|
|
389
|
-
|
|
390
|
-
assert_includes(out, "--after nope not found")
|
|
391
|
-
model = read("app/models/widget.rb")
|
|
392
|
-
assert_includes(model, "[ :occupation, :text_field ]")
|
|
393
|
-
end
|
|
394
|
-
|
|
395
|
-
def test_value_bearing_element_gets_placeholder_hash_and_warns
|
|
396
|
-
write_model("widget.rb", GENERATOR_SHAPED_MODEL)
|
|
397
|
-
|
|
398
|
-
out, _err = capture_io do
|
|
399
|
-
run_generator("Widget", "status:dropdown_with_values")
|
|
400
|
-
end
|
|
401
|
-
|
|
402
|
-
model = read("app/models/widget.rb")
|
|
403
|
-
assert_includes(model, '[ :status, :dropdown_with_values, { 1 => "one", 2 => "two" } ]')
|
|
404
|
-
assert_includes(out, "values hash")
|
|
405
|
-
end
|
|
406
|
-
|
|
407
|
-
def test_back_to_back_runs_produce_distinct_migration_filenames
|
|
408
|
-
write_model("widget.rb", GENERATOR_SHAPED_MODEL)
|
|
409
|
-
|
|
410
|
-
# No sleep between runs: unique_time_stamp must still disambiguate.
|
|
411
|
-
capture_io { run_generator("Widget", "alpha:string") }
|
|
412
|
-
capture_io { run_generator("Widget", "beta:string") }
|
|
413
|
-
|
|
414
|
-
files = Dir.glob(File.join(@destination_root, "db/migrate/*_inline_forms_add_to_widgets_*.rb"))
|
|
415
|
-
assert_equal(2, files.size, "expected two distinct addto migrations, got #{files.inspect}")
|
|
416
|
-
prefixes = files.map { |f| File.basename(f)[/\A(\d{14})/, 1] }
|
|
417
|
-
assert_equal(prefixes.uniq.size, prefixes.size, "migration timestamps collided: #{prefixes.inspect}")
|
|
418
|
-
end
|
|
419
|
-
|
|
420
|
-
def test_generated_migration_applies_cleanly_to_sqlite
|
|
421
|
-
write_model("widget.rb", GENERATOR_SHAPED_MODEL)
|
|
422
|
-
|
|
423
|
-
capture_io { run_generator("Widget", "occupation:string", "organization:belongs_to") }
|
|
424
|
-
|
|
425
|
-
migration_file = Dir.glob(
|
|
426
|
-
File.join(@destination_root, "db/migrate/*_inline_forms_add_to_widgets_*.rb")
|
|
427
|
-
).first
|
|
428
|
-
refute_nil(migration_file, "expected an addto migration to be generated")
|
|
429
|
-
|
|
430
|
-
conn = apply_migration_to_memory_sqlite(migration_file, tables: %i[organizations widgets])
|
|
431
|
-
|
|
432
|
-
assert(conn.column_exists?(:widgets, :occupation), "occupation column should exist after migrate")
|
|
433
|
-
assert(conn.column_exists?(:widgets, :organization_id), "organization_id column should exist after migrate")
|
|
434
|
-
ensure
|
|
435
|
-
teardown_sqlite
|
|
436
|
-
end
|
|
437
|
-
|
|
438
|
-
def test_simple_file_field_adds_a_declared_slot_columns_and_routes
|
|
439
|
-
write_model("widget.rb", GENERATOR_SHAPED_MODEL)
|
|
440
|
-
mkdir_p("config")
|
|
441
|
-
File.write(File.join(@destination_root, "config/routes.rb"), <<~RUBY)
|
|
442
|
-
Rails.application.routes.draw do
|
|
443
|
-
resources :widgets do
|
|
444
|
-
post 'revert', :on => :member
|
|
445
|
-
get 'list_versions', :on => :member
|
|
446
|
-
end
|
|
447
|
-
end
|
|
448
|
-
RUBY
|
|
449
|
-
|
|
450
|
-
capture_io { run_generator("Widget", "manual:simple_file_field") }
|
|
451
|
-
capture_io { run_generator("Widget", "plan_filename:simple_file_field") }
|
|
452
|
-
|
|
453
|
-
model = read("app/models/widget.rb")
|
|
454
|
-
assert_equal(1, model.scan("include InlineForms::StoredFiles").size)
|
|
455
|
-
assert_includes(model, "inline_forms_file :manual")
|
|
456
|
-
assert_includes(model, "inline_forms_file :plan_filename")
|
|
457
|
-
assert_operator(model.index("include InlineForms::StoredFiles"), :<, model.index("inline_forms_file :manual"))
|
|
458
|
-
assert_operator(model.index("include InlineForms::StoredFiles"), :<, model.index("inline_forms_file :plan_filename"))
|
|
459
|
-
assert_includes(model, "[ :manual, :simple_file_field ]")
|
|
460
|
-
routes = read("config/routes.rb")
|
|
461
|
-
assert_equal(1, routes.scan("InlineForms.file_routes(self)").size)
|
|
462
|
-
assert_match(/resources :widgets do\n\s+InlineForms\.file_routes\(self\)/, routes)
|
|
463
|
-
|
|
464
|
-
migration_file = Dir.glob(File.join(@destination_root, "db/migrate/*_inline_forms_add_to_widgets_*.rb")).min
|
|
465
|
-
conn = apply_migration_to_memory_sqlite(migration_file, tables: %i[widgets])
|
|
466
|
-
%i[manual manual_data manual_content_type].each do |column|
|
|
467
|
-
assert(conn.column_exists?(:widgets, column), "#{column} should exist after migrate")
|
|
468
|
-
end
|
|
469
|
-
ensure
|
|
470
|
-
teardown_sqlite
|
|
471
|
-
end
|
|
472
|
-
|
|
473
|
-
def test_simple_file_field_without_a_resource_block_reminds_about_routes
|
|
474
|
-
write_model("widget.rb", GENERATOR_SHAPED_MODEL)
|
|
475
|
-
mkdir_p("config")
|
|
476
|
-
File.write(File.join(@destination_root, "config/routes.rb"), "Rails.application.routes.draw do\n MODELS.each { |m| resources m }\nend\n")
|
|
477
|
-
|
|
478
|
-
stdout, = capture_io { run_generator("Widget", "manual:simple_file_field") }
|
|
479
|
-
|
|
480
|
-
assert_includes(read("app/models/widget.rb"), "inline_forms_file :manual")
|
|
481
|
-
refute_includes(read("config/routes.rb"), "file_routes")
|
|
482
|
-
assert_includes(stdout, "InlineForms.file_routes(self)")
|
|
483
|
-
end
|
|
484
|
-
|
|
485
|
-
private
|
|
486
|
-
|
|
487
|
-
def run_generator(*args)
|
|
488
|
-
InlineForms::InlineFormsAddtoGenerator.start(args, destination_root: @destination_root)
|
|
489
|
-
end
|
|
490
|
-
|
|
491
|
-
# Applies a generated migration file against a fresh in-memory sqlite DB on a
|
|
492
|
-
# dedicated connection (ScratchMigrationBase), pre-creating the referenced
|
|
493
|
-
# tables. Proves the emitted migration DSL is syntactically and semantically
|
|
494
|
-
# valid without a full Rails app — and without disturbing ActiveRecord::Base's
|
|
495
|
-
# connection (the engine integration tests share this process). Returns the
|
|
496
|
-
# scratch connection for assertions.
|
|
497
|
-
def apply_migration_to_memory_sqlite(path, tables:)
|
|
498
|
-
scratch_migration_base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
|
499
|
-
conn = scratch_migration_base.connection
|
|
500
|
-
ActiveRecord::Migration.verbose = false
|
|
501
|
-
tables.each do |t|
|
|
502
|
-
conn.create_table(t, force: true) { |tbl| tbl.string :name }
|
|
503
|
-
end
|
|
504
|
-
|
|
505
|
-
src = File.read(path)
|
|
506
|
-
klass_name = src[/class\s+(\w+)\s*</, 1]
|
|
507
|
-
Object.send(:remove_const, klass_name.to_sym) if Object.const_defined?(klass_name.to_sym)
|
|
508
|
-
Object.class_eval(src)
|
|
509
|
-
klass = Object.const_get(klass_name)
|
|
510
|
-
@sqlite_migration_const = klass_name
|
|
511
|
-
# exec_migration(conn, dir) runs the migration against `conn` specifically,
|
|
512
|
-
# unlike migrate(:up) which uses ActiveRecord::Base.connection.
|
|
513
|
-
ActiveRecord::Migration.suppress_messages { klass.new.exec_migration(conn, :up) }
|
|
514
|
-
conn
|
|
515
|
-
end
|
|
516
|
-
|
|
517
|
-
def teardown_sqlite
|
|
518
|
-
if @sqlite_migration_const && Object.const_defined?(@sqlite_migration_const.to_sym)
|
|
519
|
-
Object.send(:remove_const, @sqlite_migration_const.to_sym)
|
|
520
|
-
end
|
|
521
|
-
@sqlite_migration_const = nil
|
|
522
|
-
@@scratch_migration_base&.remove_connection
|
|
523
|
-
rescue StandardError
|
|
524
|
-
nil
|
|
525
|
-
end
|
|
526
|
-
|
|
527
|
-
# A dedicated abstract ActiveRecord::Base subclass whose connection is the
|
|
528
|
-
# scratch in-memory sqlite DB. Created lazily at *runtime* (not file-load
|
|
529
|
-
# time): subclassing ActiveRecord::Base before the dummy app boots trips the
|
|
530
|
-
# railtie into resolving the wrong Rails env. Never touches Base's own
|
|
531
|
-
# connection, so the engine integration tests sharing this process keep the
|
|
532
|
-
# dummy app's in-memory schema.
|
|
533
|
-
@@scratch_migration_base = nil
|
|
534
|
-
|
|
535
|
-
def scratch_migration_base
|
|
536
|
-
@@scratch_migration_base ||= begin
|
|
537
|
-
unless Object.const_defined?(:ScratchAddtoMigrationBase)
|
|
538
|
-
Object.const_set(:ScratchAddtoMigrationBase,
|
|
539
|
-
Class.new(ActiveRecord::Base) { self.abstract_class = true })
|
|
540
|
-
end
|
|
541
|
-
Object.const_get(:ScratchAddtoMigrationBase)
|
|
542
|
-
end
|
|
543
|
-
end
|
|
544
|
-
|
|
545
|
-
def widget_with_rows(*rows)
|
|
546
|
-
list = rows.map { |row| " #{row}\n" }.join
|
|
547
|
-
<<~RUBY
|
|
548
|
-
class Widget < ApplicationRecord
|
|
549
|
-
def inline_forms_attribute_list
|
|
550
|
-
@inline_forms_attribute_list ||= [
|
|
551
|
-
#{list.chomp}
|
|
552
|
-
]
|
|
553
|
-
end
|
|
554
|
-
end
|
|
555
|
-
RUBY
|
|
556
|
-
end
|
|
557
|
-
|
|
558
|
-
def assert_valid_ruby(path)
|
|
559
|
-
assert(RubyVM::InstructionSequence.compile(read(path)))
|
|
560
|
-
end
|
|
561
|
-
|
|
562
|
-
def write_model(file, content)
|
|
563
|
-
File.write(File.join(@destination_root, "app/models", file), content)
|
|
564
|
-
end
|
|
565
|
-
|
|
566
|
-
def read(relative_path)
|
|
567
|
-
File.read(File.join(@destination_root, relative_path))
|
|
568
|
-
end
|
|
569
|
-
|
|
570
|
-
def mkdir_p(relative_path)
|
|
571
|
-
FileUtils.mkdir_p(File.join(@destination_root, relative_path))
|
|
572
|
-
end
|
|
573
|
-
|
|
574
|
-
def read_single_addto_migration_for(table_name)
|
|
575
|
-
files = Dir.glob(File.join(@destination_root, "db/migrate/*_inline_forms_add_to_#{table_name}_*.rb"))
|
|
576
|
-
assert_equal(1, files.size, "Expected one addto migration for #{table_name}, got #{files.inspect}")
|
|
577
|
-
File.read(files.first)
|
|
578
|
-
end
|
|
579
|
-
|
|
580
|
-
def refute_addto_migration_for(table_name)
|
|
581
|
-
files = Dir.glob(File.join(@destination_root, "db/migrate/*_inline_forms_add_to_#{table_name}_*.rb"))
|
|
582
|
-
assert_equal([], files, "Expected no addto migration for #{table_name}")
|
|
583
|
-
end
|
|
584
|
-
end
|