Class: RubyCms::Generators::AdminPageGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/ruby_cms/admin_page_generator.rb

Overview

Scaffolds a new admin page that matches the modern RubyCMS conventions: an Admin::ApplicationController subclass using the shared concern stack (pagination, turbo tables, bulk actions, audit), a data_table-backed index, and — unless --read-only — full CRUD with a form.

rails g ruby_cms:admin_page product name:string price:decimal published:boolean
rails g ruby_cms:admin_page report --read-only --section settings --icon chart_bar

Fields are optional "name" pairs (type defaults to string). They drive the strong params, the form inputs, the table headers and the row cells.

Defined Under Namespace

Classes: Field

Instance Method Summary collapse

Instance Method Details

#add_routeObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/generators/ruby_cms/admin_page_generator.rb', line 71

def add_route
  return if options[:skip_route]

  routes_path = Rails.root.join("config/routes.rb")
  return say("⚠ config/routes.rb not found, skipping route.", :yellow) unless routes_path.exist?

  content = File.read(routes_path)
  return say("⚠ Route resources :#{plural_name} already present, skipping.", :yellow) \
    if content.match?(/resources\s+:#{plural_name}\b/)

  if content.match?(/namespace\s+:admin\s+do/)
    inject_into_file routes_path.to_s, route_block, after: /namespace\s+:admin\s+do\s*\n/
  else
    route "namespace :admin do\n#{route_block}end\n"
  end
  say "✓ Route: added resources :#{plural_name} to the admin namespace.", :green
end

#create_controllerObject



49
50
51
52
# File 'lib/generators/ruby_cms/admin_page_generator.rb', line 49

def create_controller
  template "controller.rb.tt",
           File.join("app/controllers/admin", "#{file_name.pluralize}_controller.rb")
end

#create_formObject



63
64
65
66
67
68
69
# File 'lib/generators/ruby_cms/admin_page_generator.rb', line 63

def create_form
  return if read_only?

  template "_form.html.erb.tt", File.join(view_dir, "_form.html.erb")
  template "new.html.erb.tt",   File.join(view_dir, "new.html.erb")
  template "edit.html.erb.tt",  File.join(view_dir, "edit.html.erb")
end

#create_indexObject



54
55
56
57
58
59
60
61
# File 'lib/generators/ruby_cms/admin_page_generator.rb', line 54

def create_index
  template "index.html.erb.tt",
           File.join(view_dir, "index.html.erb")
  template "_admin_table_content.html.erb.tt",
           File.join(view_dir, "_admin_table_content.html.erb")
  template "_row.html.erb.tt",
           File.join(view_dir, "_row.html.erb")
end

#register_navObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/generators/ruby_cms/admin_page_generator.rb', line 89

def register_nav
  return if options[:skip_nav]

  # Write to a host-owned initializer (NOT admin_nav.rb — that file is
  # regenerated by `ruby_cms update` and would clobber app entries).
  target = Rails.root.join("config/initializers/admin_pages.rb")

  return say("⚠ Page :#{plural_name} already registered in admin_pages.rb, skipping.", :yellow) \
    if target.exist? && File.read(target).include?("key: :#{plural_name}")

  create_file target.to_s, "# frozen_string_literal: true\n" unless target.exist?
  append_to_file target.to_s, "\n#{register_page_block}\n"
  say "✓ Registered :#{plural_name} in config/initializers/admin_pages.rb.", :green
end

#show_next_stepsObject



104
105
106
107
108
109
110
111
112
# File 'lib/generators/ruby_cms/admin_page_generator.rb', line 104

def show_next_steps
  say "\n✓ Admin page '#{plural_name}' created.", :green
  say "  Controller: app/controllers/admin/#{plural_name}_controller.rb"
  say "  Views:      #{view_dir}/"
  say ""
  say "Next steps:", :cyan
  say "  rails ruby_cms:seed_permissions   # registers :#{permission_key}", :cyan
  say "  # Adjust the generated _row / _form to match your columns" unless fields.any?
end

#validate_optionsObject

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/generators/ruby_cms/admin_page_generator.rb', line 37

def validate_options
  raise ArgumentError, "--section must be 'main' or 'settings', got '#{section_name}'" \
    unless %w[main settings].include?(section_name)

  return unless defined?(RubyCms::Icons::REGISTRY)
  return if RubyCms::Icons::REGISTRY.key?(icon_name.to_sym)

  say "⚠ Icon '#{icon_name}' not found in RubyCms::Icons. " \
      "Available: #{RubyCms::Icons.available.join(', ')}. " \
      "Continuing — pass any registered name with --icon.", :yellow
end