Class: Hibiki::Rails::Generators::FormGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
GeneratorHelpers, ScaffoldHelpers, ScaffoldPhlexHelpers, ScaffoldPostInstall, ScaffoldSharedViews, ScaffoldViewHelpers, Rails::Generators::ResourceHelpers
Defined in:
lib/generators/hibiki/rails/form/form_generator.rb

Overview

Re-derives the validator-shaped slice of a scaffolded resource: the ReactiveForm (live_errors) and the two form views (bounds, requiredness). Run it after adding or changing validators on the model; everything else the scaffold wrote is deliberately out of scope, so a customized index or controller is never put at risk over a validator.

Always the introspection path: the model and its table must exist, because validators are the one thing this generator is for and only a live model can answer them. A field list is optional and chooses order and subset only — the facts stay the schema's, like the scaffold's own merge.

Constant Summary collapse

SCAFFOLD_TEMPLATES =

The scaffold's templates, reused wholesale — this generator re-emits a subset of that output and must never drift from it. Its own source_root stays beside its own USAGE (Base#usage_path resolves ../USAGE from source_root, and pointing it at the scaffold's tree would serve the scaffold's help text).

File.expand_path("../scaffold_controller/templates", __dir__)

Constants included from ScaffoldSharedViews

ScaffoldSharedViews::SHARED_VIEW_DIR

Constants included from ScaffoldPhlexHelpers

ScaffoldPhlexHelpers::HELPER_MODULES, ScaffoldPhlexHelpers::PHLEX_BASE, ScaffoldPhlexHelpers::PHLEX_INITIALIZER, ScaffoldPhlexHelpers::PHLEX_NAMESPACE, ScaffoldPhlexHelpers::PLAIN_TYPES

Constants included from ScaffoldViewHelpers

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

#check_phlex_wiringObject



58
59
60
# File 'lib/generators/hibiki/rails/form/form_generator.rb', line 58

def check_phlex_wiring
  warn_without_phlex_rails unless skip_views?
end

#create_formObject



69
70
71
# File 'lib/generators/hibiki/rails/form/form_generator.rb', line 69

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

#create_viewsObject



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/generators/hibiki/rails/form/form_generator.rb', line 73

def create_views
  return if skip_views?

  if phlex?
    template "views/form.rb.tt", view_path("form.rb")
    template "views/row_form.rb.tt", view_path("row_form.rb")
  else
    template "views/_form.html.erb.tt", view_path("_form.html.erb")
    template "views/_row_form.html.erb.tt", view_path("_#{row_form_partial}.html.erb")
  end

  create_form_shared_views
end

#post_installObject



87
88
89
90
91
92
93
# File 'lib/generators/hibiki/rails/form/form_generator.rb', line 87

def post_install
  restart_notice
  shared_views_notice
  skipped_notice
  live_errors_notice
  uniqueness_notice
end

#resolve_schemaObject

Resolving the schema first means a missing model or an unmigrated table aborts before anything is written.



64
65
66
67
# File 'lib/generators/hibiki/rails/form/form_generator.rb', line 64

def resolve_schema
  schema
  @new_app_dirs = %w[app/forms].reject { exists?(it) }
end