b4um_generators 0.1.0

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.
Files changed (87) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/.rubocop.yml +28 -0
  4. data/CHANGELOG.md +5 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.de.md +821 -0
  7. data/README.md +815 -0
  8. data/Rakefile +12 -0
  9. data/b4um_generators.gemspec +30 -0
  10. data/bin/console +11 -0
  11. data/bin/setup +8 -0
  12. data/lib/b4um_generators/version.rb +5 -0
  13. data/lib/b4um_generators.rb +8 -0
  14. data/lib/generators/b4um/bento/bento_generator.rb +81 -0
  15. data/lib/generators/b4um/bento/templates/_alternating.html.erb.tt +15 -0
  16. data/lib/generators/b4um/bento/templates/_bento.html.erb.tt +26 -0
  17. data/lib/generators/b4um/bento/templates/_grid.html.erb.tt +13 -0
  18. data/lib/generators/b4um/bento/templates/_list.html.erb.tt +15 -0
  19. data/lib/generators/b4um/comments/comments_generator.rb +259 -0
  20. data/lib/generators/b4um/comments/templates/_comments.html.erb.tt +63 -0
  21. data/lib/generators/b4um/comments/templates/comment.rb.tt +5 -0
  22. data/lib/generators/b4um/comments/templates/comments_controller.rb.tt +45 -0
  23. data/lib/generators/b4um/comments/templates/create_comments.rb.tt +14 -0
  24. data/lib/generators/b4um/controller/controller_generator.rb +397 -0
  25. data/lib/generators/b4um/help/help_generator.rb +755 -0
  26. data/lib/generators/b4um/infinite_scroll/infinite_scroll_generator.rb +199 -0
  27. data/lib/generators/b4um/infinite_scroll/templates/infinite_scroll_controller.js +61 -0
  28. data/lib/generators/b4um/install/install_generator.rb +492 -0
  29. data/lib/generators/b4um/install/templates/_cookie_consent.html.erb +27 -0
  30. data/lib/generators/b4um/install/templates/_footer.html.erb +17 -0
  31. data/lib/generators/b4um/install/templates/_hero.html.erb +33 -0
  32. data/lib/generators/b4um/install/templates/_navigation.html.erb +31 -0
  33. data/lib/generators/b4um/install/templates/_sitemap.html.erb +48 -0
  34. data/lib/generators/b4um/install/templates/_theme_switcher.html.erb +57 -0
  35. data/lib/generators/b4um/install/templates/b4um/badges.css +43 -0
  36. data/lib/generators/b4um/install/templates/b4um/base.css +42 -0
  37. data/lib/generators/b4um/install/templates/b4um/callouts.css +58 -0
  38. data/lib/generators/b4um/install/templates/b4um/cards.css +401 -0
  39. data/lib/generators/b4um/install/templates/b4um/comments.css +52 -0
  40. data/lib/generators/b4um/install/templates/b4um/cookie-consent.css +83 -0
  41. data/lib/generators/b4um/install/templates/b4um/empty-state.css +26 -0
  42. data/lib/generators/b4um/install/templates/b4um/flash.css +86 -0
  43. data/lib/generators/b4um/install/templates/b4um/footer.css +338 -0
  44. data/lib/generators/b4um/install/templates/b4um/forms.css +924 -0
  45. data/lib/generators/b4um/install/templates/b4um/hero.css +149 -0
  46. data/lib/generators/b4um/install/templates/b4um/layout.css +166 -0
  47. data/lib/generators/b4um/install/templates/b4um/lightbox.css +268 -0
  48. data/lib/generators/b4um/install/templates/b4um/navigation.css +257 -0
  49. data/lib/generators/b4um/install/templates/b4um/pagination.css +85 -0
  50. data/lib/generators/b4um/install/templates/b4um/resources.css +121 -0
  51. data/lib/generators/b4um/install/templates/b4um/search.css +41 -0
  52. data/lib/generators/b4um/install/templates/b4um/tables.css +67 -0
  53. data/lib/generators/b4um/install/templates/b4um/theme-switcher.css +178 -0
  54. data/lib/generators/b4um/install/templates/b4um/theme.css +132 -0
  55. data/lib/generators/b4um/install/templates/b4um.css +20 -0
  56. data/lib/generators/b4um/install/templates/b4um.yml +12 -0
  57. data/lib/generators/b4um/install/templates/b4um_helper.rb +27 -0
  58. data/lib/generators/b4um/install/templates/cookie_consent_controller.js +76 -0
  59. data/lib/generators/b4um/install/templates/dismissible_controller.js +7 -0
  60. data/lib/generators/b4um/install/templates/image_lightbox_controller.js +414 -0
  61. data/lib/generators/b4um/install/templates/image_preview_controller.js +85 -0
  62. data/lib/generators/b4um/install/templates/navigation_controller.js +25 -0
  63. data/lib/generators/b4um/install/templates/navigation_helper.rb +26 -0
  64. data/lib/generators/b4um/install/templates/sitemap_controller.js +35 -0
  65. data/lib/generators/b4um/install/templates/theme_controller.js +122 -0
  66. data/lib/generators/b4um/pagination/pagination_generator.rb +179 -0
  67. data/lib/generators/b4um/pagination/templates/_pagination.html.erb.tt +48 -0
  68. data/lib/generators/b4um/pagination/templates/pagination.rb +36 -0
  69. data/lib/generators/b4um/scaffold/scaffold_generator.rb +333 -0
  70. data/lib/generators/b4um/scaffold/templates/_bento.html.erb.tt +26 -0
  71. data/lib/generators/b4um/scaffold/templates/_form.html.erb.tt +218 -0
  72. data/lib/generators/b4um/scaffold/templates/_resource.html.erb.tt +144 -0
  73. data/lib/generators/b4um/scaffold/templates/_table.html.erb.tt +40 -0
  74. data/lib/generators/b4um/scaffold/templates/edit.html.erb.tt +21 -0
  75. data/lib/generators/b4um/scaffold/templates/index.html.erb.tt +24 -0
  76. data/lib/generators/b4um/scaffold/templates/new.html.erb.tt +17 -0
  77. data/lib/generators/b4um/scaffold/templates/show.html.erb.tt +31 -0
  78. data/lib/generators/b4um/search/search_generator.rb +165 -0
  79. data/lib/generators/b4um/search/templates/search.rb +34 -0
  80. data/lib/generators/b4um/search/templates/search_form.html.erb.tt +15 -0
  81. data/lib/generators/b4um/table/table_generator.rb +63 -0
  82. data/lib/generators/b4um/table/templates/_table.html.erb.tt +40 -0
  83. data/lib/generators/b4um/trix/templates/b4um_rich_text_helper.rb +58 -0
  84. data/lib/generators/b4um/trix/templates/b4um_trix.js +551 -0
  85. data/lib/generators/b4um/trix/trix_generator.rb +383 -0
  86. data/sig/b4um_generators.rbs +4 -0
  87. metadata +148 -0
@@ -0,0 +1,397 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+ require "rails/generators"
5
+ require "rails/generators/rails/controller/controller_generator"
6
+
7
+ module B4um
8
+ module Generators
9
+ class ControllerGenerator < Rails::Generators::ControllerGenerator
10
+ source_root Rails::Generators::ControllerGenerator.source_root
11
+
12
+ class_option :sitemap,
13
+ type: :string,
14
+ aliases: "-s",
15
+ desc: "Add generated actions to a B4UM sitemap column configured in config/b4um.yml"
16
+
17
+ class << self
18
+ def desc(_description = nil)
19
+ "Generates a B4UM controller with views, navigation links, and legal footer links."
20
+ end
21
+ end
22
+
23
+ def create_b4um_views
24
+ actions.each do |action|
25
+ view_path = File.join(
26
+ "app/views",
27
+ file_name,
28
+ "#{action}.html.erb"
29
+ )
30
+
31
+ create_file(
32
+ view_path,
33
+ b4um_view_content(action),
34
+ force: true
35
+ )
36
+ end
37
+ end
38
+
39
+ def add_navigation_links
40
+ return if options[:sitemap].present?
41
+
42
+ navigation_path = "app/views/shared/_navigation.html.erb"
43
+ full_navigation_path = File.join(destination_root, navigation_path)
44
+
45
+ return unless File.exist?(full_navigation_path)
46
+
47
+ navigation = File.read(full_navigation_path)
48
+ marker = "<%# B4UM_NAVIGATION_LINKS %>"
49
+
50
+ return unless navigation.include?(marker)
51
+
52
+ actions.each do |action|
53
+ next if footer_action?(action)
54
+
55
+ next if navigation.include?(
56
+ "action: :#{action}"
57
+ )
58
+
59
+ navigation_link = <<~ERB
60
+ <%= navigation_link_to "#{navigation_label(action)}",
61
+ #{file_name}_#{action}_path,
62
+ controller: :#{file_name},
63
+ action: :#{action} %>
64
+
65
+ #{marker}
66
+ ERB
67
+
68
+ gsub_file(
69
+ navigation_path,
70
+ marker,
71
+ navigation_link.chomp
72
+ )
73
+ end
74
+ end
75
+
76
+ def add_sitemap_links
77
+ config_path = File.join(
78
+ destination_root,
79
+ "config/b4um.yml"
80
+ )
81
+
82
+ return unless File.exist?(config_path)
83
+
84
+ config = YAML.safe_load_file(config_path) || {}
85
+ section = sitemap_section || legal_sitemap_section(config)
86
+
87
+ return unless section
88
+
89
+ column = sitemap_column(config, section)
90
+
91
+ return unless column
92
+
93
+ add_links_to_sitemap_column(
94
+ column,
95
+ sitemap_actions(config)
96
+ )
97
+
98
+ File.write(
99
+ config_path,
100
+ config.to_yaml
101
+ )
102
+ end
103
+
104
+ def remove_legal_navigation_links
105
+ navigation_path = "app/views/shared/_navigation.html.erb"
106
+ full_navigation_path = File.join(destination_root, navigation_path)
107
+
108
+ return unless File.exist?(full_navigation_path)
109
+
110
+ actions.each do |action|
111
+ next unless footer_action?(action)
112
+
113
+ navigation = File.read(full_navigation_path)
114
+ path_name = "#{file_name}_#{action}_path"
115
+
116
+ next unless navigation.include?(path_name)
117
+
118
+ navigation_link_pattern = /
119
+ \n*
120
+ \s*<%=\s*navigation_link_to
121
+ \s+"[^"]*",
122
+ \s*#{Regexp.escape(path_name)},
123
+ \s*controller:\s*:#{Regexp.escape(file_name)},
124
+ \s*action:\s*:#{Regexp.escape(action.to_s)}
125
+ \s*%>
126
+ \n*
127
+ /x
128
+
129
+ gsub_file(
130
+ navigation_path,
131
+ navigation_link_pattern,
132
+ "\n"
133
+ )
134
+ end
135
+ end
136
+
137
+ def add_footer_links
138
+ return if legal_links_in_sitemap?
139
+
140
+ footer_path = "app/views/shared/_footer.html.erb"
141
+ full_footer_path = File.join(destination_root, footer_path)
142
+
143
+ return unless File.exist?(full_footer_path)
144
+
145
+ footer = File.read(full_footer_path)
146
+ marker = "<%# B4UM_FOOTER_LINKS %>"
147
+
148
+ return unless footer.include?(marker)
149
+
150
+ actions.each do |action|
151
+ next unless footer_action?(action)
152
+
153
+ path_name = "#{file_name}_#{action}_path"
154
+
155
+ if footer.include?(path_name)
156
+ upgrade_footer_link(
157
+ footer_path,
158
+ footer,
159
+ path_name
160
+ )
161
+
162
+ footer = File.read(full_footer_path)
163
+ next
164
+ end
165
+
166
+ footer_link = <<~ERB
167
+ <%= link_to "#{navigation_label(action)}",
168
+ #{path_name},
169
+ class: [
170
+ "b4um-footer__link",
171
+ ("is-active" if current_page?(#{path_name}))
172
+ ].compact.join(" "),
173
+ aria: (current_page?(#{path_name}) ? { current: "page" } : {}) %>
174
+
175
+ #{marker}
176
+ ERB
177
+
178
+ gsub_file(
179
+ footer_path,
180
+ marker,
181
+ footer_link.chomp
182
+ )
183
+
184
+ footer = File.read(full_footer_path)
185
+ end
186
+ end
187
+
188
+ def install_footer_active_styles
189
+ stylesheet_path = "app/assets/stylesheets/b4um/footer.css"
190
+ full_stylesheet_path = File.join(
191
+ destination_root,
192
+ stylesheet_path
193
+ )
194
+
195
+ return unless File.exist?(full_stylesheet_path)
196
+
197
+ stylesheet = File.read(full_stylesheet_path)
198
+
199
+ return if stylesheet.include?(
200
+ ".b4um-footer__link.is-active"
201
+ )
202
+
203
+ append_to_file(
204
+ stylesheet_path,
205
+ <<~CSS
206
+
207
+ .b4um-footer__link.is-active {
208
+ color: var(--b4um-primary);
209
+
210
+ font-weight: 700;
211
+ }
212
+ CSS
213
+ )
214
+ end
215
+
216
+ private
217
+
218
+ def legal_sitemap_section(config)
219
+ placement = config.dig(
220
+ "legal_links",
221
+ "placement"
222
+ ).to_s.downcase
223
+
224
+ return if placement.empty? || placement == "footer"
225
+
226
+ return unless sitemap_column(config, placement)
227
+
228
+ placement
229
+ end
230
+
231
+ def legal_links_in_sitemap?
232
+ config_path = File.join(
233
+ destination_root,
234
+ "config/b4um.yml"
235
+ )
236
+
237
+ return false unless File.exist?(config_path)
238
+
239
+ config = YAML.safe_load_file(config_path) || {}
240
+
241
+ legal_sitemap_section(config).present?
242
+ end
243
+
244
+ def sitemap_actions(config)
245
+ return actions if options[:sitemap].present?
246
+
247
+ return actions unless legal_sitemap_section(config)
248
+
249
+ actions.select do |action|
250
+ footer_action?(action)
251
+ end
252
+ end
253
+
254
+ def add_links_to_sitemap_column(column, selected_actions)
255
+ column["links"] ||= []
256
+
257
+ selected_actions.each do |action|
258
+ route = "#{file_name}_#{action}_path"
259
+
260
+ next if sitemap_link_exists?(column, route)
261
+
262
+ column["links"] << {
263
+ "title" => navigation_label(action),
264
+ "route" => route
265
+ }
266
+ end
267
+ end
268
+
269
+ def sitemap_column(config, section)
270
+ Array(config["sitemap"]).find do |item|
271
+ item["key"].to_s.downcase == section
272
+ end
273
+ end
274
+
275
+ def sitemap_link_exists?(column, route)
276
+ Array(column["links"]).any? do |link|
277
+ link["route"].to_s == route
278
+ end
279
+ end
280
+
281
+ def sitemap_section
282
+ section = options[:sitemap].to_s.downcase
283
+
284
+ return if section.empty?
285
+
286
+ config_path = File.join(
287
+ destination_root,
288
+ "config/b4um.yml"
289
+ )
290
+
291
+ unless File.exist?(config_path)
292
+ say(
293
+ " B4UM sitemap configuration not found.",
294
+ :yellow
295
+ )
296
+
297
+ return
298
+ end
299
+
300
+ config = YAML.safe_load_file(config_path) || {}
301
+
302
+ valid_sections = Array(config["sitemap"]).filter_map do |column|
303
+ column["key"].to_s.downcase.presence
304
+ end
305
+
306
+ return section if valid_sections.include?(section)
307
+
308
+ say(
309
+ " Unknown sitemap section '#{section}'. " \
310
+ "Available sections: #{valid_sections.join(", ")}.",
311
+ :yellow
312
+ )
313
+
314
+ nil
315
+ end
316
+
317
+ def upgrade_footer_link(footer_path, footer, path_name)
318
+ return if footer.include?(
319
+ "current_page?(#{path_name})"
320
+ )
321
+
322
+ old_link_pattern = /
323
+ <%=\s*link_to\s+"([^"]+)",
324
+ \s*#{Regexp.escape(path_name)},
325
+ \s*class:\s*"b4um-footer__link"\s*%>
326
+ /x
327
+
328
+ return unless footer.match?(old_link_pattern)
329
+
330
+ upgraded_link = <<~ERB.chomp
331
+ <%= link_to "\\1",
332
+ #{path_name},
333
+ class: [
334
+ "b4um-footer__link",
335
+ ("is-active" if current_page?(#{path_name}))
336
+ ].compact.join(" "),
337
+ aria: (current_page?(#{path_name}) ? { current: "page" } : {}) %>
338
+ ERB
339
+
340
+ gsub_file(
341
+ footer_path,
342
+ old_link_pattern,
343
+ upgraded_link
344
+ )
345
+ end
346
+
347
+ def footer_action?(action)
348
+ footer_actions = %w[
349
+ impressum
350
+ datenschutz
351
+ agb
352
+ imprint
353
+ privacy
354
+ privacy_policy
355
+ terms
356
+ terms_and_conditions
357
+ ]
358
+
359
+ footer_actions.include?(action.to_s)
360
+ end
361
+
362
+ def b4um_view_content(action)
363
+ title = navigation_label(action)
364
+
365
+ <<~ERB
366
+ <% content_for :title, "#{title}" %>
367
+
368
+ <div class="page-header">
369
+ <h1>#{title}</h1>
370
+ </div>
371
+
372
+ <div class="b4um-grid">
373
+ <section class="b4um-card b4um-card--full">
374
+ <h2 class="b4um-card__title">
375
+ #{title}
376
+ </h2>
377
+
378
+ <p class="b4um-card__text">
379
+ Add your content here.
380
+ </p>
381
+ </section>
382
+ </div>
383
+ ERB
384
+ end
385
+
386
+ def navigation_label(action)
387
+ abbreviations = {
388
+ "agb" => "AGB",
389
+ "faq" => "FAQ",
390
+ "ueber_uns" => "Über uns"
391
+ }
392
+
393
+ abbreviations.fetch(action.to_s, action.to_s.humanize)
394
+ end
395
+ end
396
+ end
397
+ end