Sha256: f7177c37453ed83617e9aa67104534b8966b9bb3a06e944dea3d25a861bd5da6

Contents?: true

Size: 1012 Bytes

Versions: 4

Compression:

Stored size: 1012 Bytes

Contents

module Stimulus::Manifest
  extend self

  def generate_from(controllers_path)
    manifest = extract_controllers_from(controllers_path).collect do |controller_path|
      import_and_register_controller(controllers_path, controller_path)
    end

    manifest.uniq
  end

  def import_and_register_controller(controllers_path, controller_path)
    controller_path = controller_path.relative_path_from(controllers_path).to_s
    module_path = controller_path.split('.').first
    controller_class_name = module_path.underscore.camelize.gsub(/::/, "__")
    tag_name = module_path.remove(/_controller/).gsub(/_/, "-").gsub(/\//, "--")

    <<-JS

import #{controller_class_name} from "./#{module_path}"
application.register("#{tag_name}", #{controller_class_name})
    JS
  end

  def extract_controllers_from(directory)
    (directory.children.select { |e| e.to_s =~ /_controller(\.\w+)+$/ } +
      directory.children.select(&:directory?).collect { |d| extract_controllers_from(d) }
    ).flatten.sort
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
stimulus-rails-1.3.4 lib/stimulus/manifest.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/stimulus-rails-1.3.3/lib/stimulus/manifest.rb
stimulus-rails-1.3.3 lib/stimulus/manifest.rb
stimulus-rails-1.3.2 lib/stimulus/manifest.rb