Class: Monolith::GeneratorsController::Show

Inherits:
View
  • Object
show all
Defined in:
app/controllers/monolith/generators_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#generator=(value) ⇒ Object (writeonly)

Sets the attribute generator

Parameters:

  • value

    the value to set the attribute generator to.



191
192
193
# File 'app/controllers/monolith/generators_controller.rb', line 191

def generator=(value)
  @generator = value
end

Instance Method Details

#view_templateObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'app/controllers/monolith/generators_controller.rb', line 193

def view_template
  g = @generator

  div(class: "p-6 space-y-6") do
    h1(class: "text-2xl font-bold") { g.name }
    p { code { "rails generate #{g.namespace}" } }

    if g.description
      p { g.description }
    end

    # Arguments
    section do
      h2(class: "text-xl font-bold mb-2") { "Arguments" }
      if g.arguments.any?
        ul(class: "list-disc pl-6 space-y-1") do
          g.arguments.each do |arg|
            li do
              code { arg[:name] }
              plain " (#{arg[:type]}"
              plain ", required" if arg[:required]
              plain ")"
              if arg[:description]
                plain " - #{arg[:description]}"
              end
              if arg[:default]
                plain " [default: #{arg[:default]}]"
              end
            end
          end
        end
      else
        p { em { "No arguments" } }
      end
    end

    # Options
    section do
      h2(class: "text-xl font-bold mb-2") { "Options" }
      if g.class_options.any?
        ul(class: "list-disc pl-6 space-y-1") do
          g.class_options.each do |name, option|
            li do
              code { "--#{name}" }
              if option[:aliases]&.any?
                plain " ("
                code { option[:aliases].join(", ") }
                plain ")"
              end
              plain " - #{option[:type]}"
              if option[:description]
                plain " - #{option[:description]}"
              end
              if option[:default]
                plain " [default: #{option[:default]}]"
              end
            end
          end
        end
      else
        p { em { "No options" } }
      end
    end

    # Invoke Form
    section do
      h2(class: "text-xl font-bold mb-2") { "Run Generator" }
      form(method: "post", action: url_for(controller: "/monolith/generators", action: :create, id: g.to_param)) do
        div(class: "space-y-3") do
          # CSRF token
          input(type: "hidden", name: "authenticity_token", value: form_authenticity_token)

          # Arguments input
          div do
            label(class: "block font-semibold mb-1") { "Arguments (space-separated)" }
            input(
              type: "text",
              name: "args",
              placeholder: g.arguments.map { |a| a[:name] }.join(" "),
              class: "w-full border px-3 py-2"
            )
            if g.arguments.any?
              p(class: "text-sm mt-1") do
                plain "Expected: "
                g.arguments.each_with_index do |arg, idx|
                  plain " " if idx > 0
                  code { arg[:name] }
                end
              end
            end
          end

          # Options
          if g.class_options.any?
            div do
              label(class: "block font-semibold mb-2") { "Options" }
              div(class: "space-y-2") do
                g.class_options.each do |name, option|
                  div(class: "flex items-center gap-2") do
                    label(class: "w-48") {
                      code { "--#{name}" }
                    }
                    case option[:type]
                    when :boolean
                      input(type: "checkbox", name: "options[#{name}]", value: "true")
                    else
                      input(
                        type: "text",
                        name: "options[#{name}]",
                        placeholder: option[:default]&.to_s || option[:banner]&.to_s,
                        class: "flex-1 border px-2 py-1"
                      )
                    end
                  end
                end
              end
            end
          end

          # Submit button
          div do
            button(
              type: "submit",
              class: "px-4 py-2 border font-semibold hover:bg-gray-100"
            ) { "Generate" }
          end
        end
      end
    end

    # Source location
    if g.source_location
      section do
        h2(class: "text-xl font-bold mb-2") { "Source" }
        p { code { g.source_location } }
      end
    end

    div(class: "pt-4") { nav_link "← All generators", controller: "/monolith/generators", action: :index }
  end
end