Class: PhlexKit::Generators::ComponentGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/phlex_kit/component/component_generator.rb

Overview

rails g phlex_kit:component button — the shadcn-style "eject": copies a component's folder (.rb parts + co-located .css) out of the gem and into the host app so the team owns and can edit the source. Appends its @import to application.css. The ejected copy keeps the PhlexKit namespace; the host's copy shadows the gem's via Zeitwerk load-path order.

Instance Method Summary collapse

Instance Method Details

#eject_componentObject



15
16
17
18
19
20
21
# File 'lib/generators/phlex_kit/component/component_generator.rb', line 15

def eject_component
  unless File.directory?(File.join(self.class.source_root, file_name))
    say_status :error, "no PhlexKit component named '#{file_name}'", :red
    return
  end
  directory file_name, "app/components/phlex_kit/#{file_name}"
end

#remind_collapseObject



30
31
32
33
34
35
36
37
38
# File 'lib/generators/phlex_kit/component/component_generator.rb', line 30

def remind_collapse
  say <<~MSG
    Ejected phlex_kit/#{file_name}. Ensure your app collapses ejected
    component folders (config/application.rb):

        Rails.autoloaders.main.collapse("app/components/phlex_kit/*")

  MSG
end

#wire_importObject



23
24
25
26
27
28
# File 'lib/generators/phlex_kit/component/component_generator.rb', line 23

def wire_import
  css = "app/assets/stylesheets/application.css"
  line = %(@import url("phlex_kit/#{file_name}/#{file_name}.css");\n)
  return unless File.exist?(css)
  prepend_to_file(css, line) unless File.read(css).include?(line.strip)
end