Class: Hibiki::Rails::Generators::UploadFieldGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
ExtrasThreading, GeneratorHelpers, ScaffoldHelpers, ScaffoldModelInjection, ScaffoldPhlexHelpers, ScaffoldViewHelpers, UploadFieldHelpers, UploadFieldInjections, Rails::Generators::ResourceHelpers
Defined in:
lib/generators/hibiki/rails/upload_field/upload_field_generator.rb

Overview

A has_one_attached upload (or, with --many, a has_many_attached gallery) on both edit surfaces of a resource hibiki:rails:scaffold_controller already generated. The classic form gets a direct-upload file field plus remove checkboxes; the inline channel form gets an upload-on-change input whose pending state the graph owns, keyed by the form's dom — only the signed blob ids ever ride the channel.

The owner must exist and be migrated; one attachment per run.

Constant Summary

Constants included from UploadFieldInjections

Hibiki::Rails::Generators::UploadFieldInjections::CHANNEL_ANCHOR, Hibiki::Rails::Generators::UploadFieldInjections::ERB_FIELDSET_CLOSE, Hibiki::Rails::Generators::UploadFieldInjections::ERB_SUBMIT_DIV, Hibiki::Rails::Generators::UploadFieldInjections::FILE_END, Hibiki::Rails::Generators::UploadFieldInjections::PHLEX_FIELDSET_CLOSE, Hibiki::Rails::Generators::UploadFieldInjections::PHLEX_PAGE_FIELDS_CALL, Hibiki::Rails::Generators::UploadFieldInjections::QUERY_WINDOW_SCOPE, Hibiki::Rails::Generators::UploadFieldInjections::ROW_ACTIONS_ANCHOR_ERB, Hibiki::Rails::Generators::UploadFieldInjections::ROW_ACTIONS_ANCHOR_PHLEX

Constants included from UploadFieldHelpers

Hibiki::Rails::Generators::UploadFieldHelpers::ACCEPT, Hibiki::Rails::Generators::UploadFieldHelpers::JS_CONTROLLER, Hibiki::Rails::Generators::UploadFieldHelpers::JS_MANY_MARKER, Hibiki::Rails::Generators::UploadFieldHelpers::JS_TEMPLATE

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

#create_concernObject



66
67
68
# File 'lib/generators/hibiki/rails/upload_field/upload_field_generator.rb', line 66

def create_concern
  template concern_template, concern_path
end

#create_stimulus_controllerObject

ONE controller file per app — every upload field shares it, routed by the action/dom Stimulus values the view stamps on the input. A gallery needs the per-file loop, so many-mode refreshes a copy from before it (single-mode leaves any existing file alone).



74
75
76
77
78
79
80
# File 'lib/generators/hibiki/rails/upload_field/upload_field_generator.rb', line 74

def create_stimulus_controller
  return template(JS_TEMPLATE, JS_CONTROLLER) unless exists?(JS_CONTROLLER)
  return say_status :identical, JS_CONTROLLER, :blue if !many? || wired?(JS_CONTROLLER, JS_MANY_MARKER)

  say_status :js, "#{JS_CONTROLLER} predates --many (one file per pick) — refreshed", :yellow
  template JS_TEMPLATE, JS_CONTROLLER, force: true
end

#create_viewObject



92
93
94
95
96
97
98
# File 'lib/generators/hibiki/rails/upload_field/upload_field_generator.rb', line 92

def create_view
  if phlex?
    template component_template, view_path("#{upload_partial}.rb")
  else
    template partial_template, view_path("_#{upload_partial}.html.erb")
  end
end

#post_installObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/generators/hibiki/rails/upload_field/upload_field_generator.rb', line 132

def post_install
  if auto_many?
    say_status :many, "#{class_name} already declares has_many_attached :#{attachment_name}" \
                      "generated the gallery (as with --many).", :blue
  end
  unless active_storage_ready?
    say_status :migrate, "no Active Storage tables — run bin/rails active_storage:install " \
                         "and bin/rails db:migrate before uploading.", :yellow
  end
  if image_accepted? && !wired?("Gemfile", /^\s*gem ["']image_processing/)
    say_status :gem, "thumbnails need image_processing — uncomment it in the Gemfile " \
                     "and bundle install.", :yellow
  end
  importmap_notice
  unless wired?("app/javascript/application.js", "activestorage")
    say_status :js, "the classic form needs Active Storage started in " \
                    "app/javascript/application.js: " \
                    'import * as ActiveStorage from "@rails/activestorage"; ' \
                    "ActiveStorage.start()", :yellow
  end
  return unless @concerns_dir_new

  say_status :restart, "app/channels/concerns is new. " \
                       "Please restart if the server is running.", :yellow
end

#preflightObject

Everything that can refuse, before anything is written.



58
59
60
61
62
63
64
# File 'lib/generators/hibiki/rails/upload_field/upload_field_generator.rb', line 58

def preflight
  owner_class
  check_scaffolded!
  check_attachment!
  accept_attr
  @concerns_dir_new = !exists?("app/channels/concerns")
end

#register_stimulus_controllerObject

Importmap apps eager-load the controllers directory; jsbundling apps get the import/register pair (stimulus:manifest:update format).



84
85
86
87
88
89
90
# File 'lib/generators/hibiki/rails/upload_field/upload_field_generator.rb', line 84

def register_stimulus_controller
  return if importmap?
  return say_status :identical, INDEX_JS, :blue if wired?(INDEX_JS, %(register("upload-field")))
  return manual_wiring(INDEX_JS, js_registration) unless exists?(INDEX_JS)

  append_to_file INDEX_JS, "\n#{js_registration}"
end

#thread_extrasObject

BEFORE the render injection: the compat probe reads "extras" from the row form, and the render line would satisfy it vacuously.



102
103
104
# File 'lib/generators/hibiki/rails/upload_field/upload_field_generator.rb', line 102

def thread_extras
  thread_extras_through_partials
end

#wire_channelObject



106
107
108
# File 'lib/generators/hibiki/rails/upload_field/upload_field_generator.rb', line 106

def wire_channel
  inject_channel_include
end

#wire_controllerObject



128
129
130
# File 'lib/generators/hibiki/rails/upload_field/upload_field_generator.rb', line 128

def wire_controller
  inject_permitted_params
end

#wire_modelObject



110
111
112
# File 'lib/generators/hibiki/rails/upload_field/upload_field_generator.rb', line 110

def wire_model
  inject_model_attachment
end

#wire_page_formObject



124
125
126
# File 'lib/generators/hibiki/rails/upload_field/upload_field_generator.rb', line 124

def wire_page_form
  inject_page_form
end

#wire_preloadsObject



119
120
121
122
# File 'lib/generators/hibiki/rails/upload_field/upload_field_generator.rb', line 119

def wire_preloads
  inject_query_preload
  inject_member_channel_preload
end

#wire_viewsObject



114
115
116
117
# File 'lib/generators/hibiki/rails/upload_field/upload_field_generator.rb', line 114

def wire_views
  inject_row_form_render
  inject_row_display
end