Class: Hibiki::Rails::Generators::ScaffoldControllerGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
GeneratorHelpers, ScaffoldHelpers, ScaffoldModelInjection, ScaffoldParentInjection, ScaffoldParentNotices, ScaffoldPhlexHelpers, ScaffoldPostInstall, ScaffoldSharedViews, ScaffoldTransportStylesheet, ScaffoldViewHelpers, Rails::Generators::ResourceHelpers
Defined in:
lib/generators/hibiki/rails/scaffold_controller/scaffold_controller_generator.rb

Overview

A reactive CRUD resource for a model that already exists: the graph (a collection channel and a per-record one), a shared query object, a ReactiveForm, the views, and a scaffold-shaped controller that still serves the first paint and the non-JS path.

Everything is derived from the model's schema — column names, column types, belongs_to reflections — or from the same NAME field:type argument list Rails' own scaffold takes. Nothing here knows what a "book" is.

Constant Summary collapse

TEMPLATE_ROOT =
File.expand_path("templates", __dir__)

Constants included from ScaffoldTransportStylesheet

Hibiki::Rails::Generators::ScaffoldTransportStylesheet::BULK_LINK, Hibiki::Rails::Generators::ScaffoldTransportStylesheet::ENTRIES, Hibiki::Rails::Generators::ScaffoldTransportStylesheet::IMPORT_LINE, Hibiki::Rails::Generators::ScaffoldTransportStylesheet::LAYOUT, Hibiki::Rails::Generators::ScaffoldTransportStylesheet::LINK_TAG, Hibiki::Rails::Generators::ScaffoldTransportStylesheet::LOGICAL_NAME, Hibiki::Rails::Generators::ScaffoldTransportStylesheet::SINGLE_LINK, Hibiki::Rails::Generators::ScaffoldTransportStylesheet::STYLESHEET, Hibiki::Rails::Generators::ScaffoldTransportStylesheet::TAILWIND_IMPORT

Constants included from ScaffoldSharedViews

Hibiki::Rails::Generators::ScaffoldSharedViews::SHARED_VIEW_DIR

Constants included from ScaffoldParentNotices

Hibiki::Rails::Generators::ScaffoldParentNotices::PARENT_PIECES

Constants included from ScaffoldPhlexHelpers

Hibiki::Rails::Generators::ScaffoldPhlexHelpers::HELPER_MODULES, Hibiki::Rails::Generators::ScaffoldPhlexHelpers::PHLEX_BASE, Hibiki::Rails::Generators::ScaffoldPhlexHelpers::PHLEX_INITIALIZER, Hibiki::Rails::Generators::ScaffoldPhlexHelpers::PHLEX_NAMESPACE, Hibiki::Rails::Generators::ScaffoldPhlexHelpers::PLAIN_TYPES

Constants included from ScaffoldViewHelpers

Hibiki::Rails::Generators::ScaffoldViewHelpers::FIELD_TOKENS

Constants included from GeneratorHelpers

GeneratorHelpers::APPLICATION_HELPER, GeneratorHelpers::IMPORTMAP, GeneratorHelpers::INDEX_JS, GeneratorHelpers::REGISTER_FRAGMENT, GeneratorHelpers::SHIM

Instance Method Summary collapse

Instance Method Details

#add_routeObject

The route action directly rather than hook_for :resource_route. The hook resolves through Rails::Generators.find_by_namespace with OUR base_name ("hibiki"), which is a resolution risk for a one-line action — and calling it here also keeps hibiki:rails:scaffold from having to reason about whether Thor de-duplicated the invocation. Guarded, so re-running an overlay does not stack duplicate routes.



163
164
165
166
167
# File 'lib/generators/hibiki/rails/scaffold_controller/scaffold_controller_generator.rb', line 163

def add_route
  return if options[:skip_routes] || route_declared?

  route "resources :#{plural_name}", namespace: regular_class_path
end

#check_phlex_wiringObject

First, so a --phlex app missing its wiring reads that at the TOP of the output rather than under forty created-file lines. It only warns, so the files are written either way — see ScaffoldPhlexHelpers.



76
77
78
# File 'lib/generators/hibiki/rails/scaffold_controller/scaffold_controller_generator.rb', line 76

def check_phlex_wiring
  warn_without_phlex_rails
end

#create_channelsObject



98
99
100
101
# File 'lib/generators/hibiki/rails/scaffold_controller/scaffold_controller_generator.rb', line 98

def create_channels
  template "channel.rb.tt", collection_channel_path
  template "member_channel.rb.tt", member_channel_path
end

#create_controllerObject



111
112
113
# File 'lib/generators/hibiki/rails/scaffold_controller/scaffold_controller_generator.rb', line 111

def create_controller
  template "controller.rb.tt", scaffold_controller_path
end

#create_formObject



107
108
109
# File 'lib/generators/hibiki/rails/scaffold_controller/scaffold_controller_generator.rb', line 107

def create_form
  template "form.rb.tt", form_path
end

#create_modelsObject



103
104
105
# File 'lib/generators/hibiki/rails/scaffold_controller/scaffold_controller_generator.rb', line 103

def create_models
  template "query.rb.tt", query_path
end

#create_stylesheetObject

One file per app, not per resource, and the only thing this generator writes outside app/channels,models,forms,views,controllers. It carries no model knowledge at all — the rules key on the client's attributes — so a second resource finds it already there.



94
95
96
# File 'lib/generators/hibiki/rails/scaffold_controller/scaffold_controller_generator.rb', line 94

def create_stylesheet
  create_transport_stylesheet
end

#create_viewsObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/generators/hibiki/rails/scaffold_controller/scaffold_controller_generator.rb', line 115

def create_views
  if phlex?
    create_phlex_views
  else
    %w[index show new edit _form _list _controls].each do |view|
      template "views/#{view}.html.erb.tt", view_path("#{view}.html.erb")
    end

    template "views/_row.html.erb.tt", view_path("_#{row_partial}.html.erb")
    template "views/_row_form.html.erb.tt", view_path("_#{row_form_partial}.html.erb")
  end

  # The page control and the field-error line, once per app — see
  # ScaffoldSharedViews.
  create_shared_views
end

#inject_model_supportObject

The model being scaffolded — one of two files this generator modifies that the app already owned. It gets the after_commit ping, which is what makes another tab, another user, the plain controller and a console write all reach an open list.

The alternative — printing it and hoping — fails SILENTLY: everything works in one tab, and cross-tab plus out-of-band writes simply never arrive.



140
141
142
143
144
145
146
# File 'lib/generators/hibiki/rails/scaffold_controller/scaffold_controller_generator.rb', line 140

def inject_model_support
  return manual_wiring(model_path, model_support) unless exists?(model_path)
  return if wired?(model_path, ping_marker)

  inject_into_model model_path, class_name, model_support
  announce_model_change
end

#inject_parent_supportObject

The other one: the model a belongs_to points AT, which Rails' own scaffold never touches. It owes this resource two things it cannot write for itself — the has_many half of the association, and a ping, because a row prints the parent's label rather than its id. Both are derived from the reflection; see ScaffoldParentInjection.



153
154
155
# File 'lib/generators/hibiki/rails/scaffold_controller/scaffold_controller_generator.rb', line 153

def inject_parent_support
  inject_parent_models
end

#post_installObject



169
170
171
# File 'lib/generators/hibiki/rails/scaffold_controller/scaffold_controller_generator.rb', line 169

def post_install
  post_install_notices
end

#resolve_schemaObject

Resolving the schema first means a missing model or an unmigrated table aborts before anything is written, rather than half-way through.



82
83
84
85
86
87
88
# File 'lib/generators/hibiki/rails/scaffold_controller/scaffold_controller_generator.rb', line 82

def resolve_schema
  schema
  # Captured BEFORE anything is written, so post_install can tell which
  # app/* directories are new — Rails computes autoload paths from that
  # glob at boot, and a new one needs a restart.
  @new_app_dirs = %w[app/forms app/channels app/models app/views].reject { exists?(it) }
end