Class: ViewComponent::Generators::StylesheetGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
AbstractGenerator
Defined in:
lib/generators/view_component/stylesheet/stylesheet_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_stylesheetObject



15
16
17
# File 'lib/generators/view_component/stylesheet/stylesheet_generator.rb', line 15

def create_stylesheet
  template "component.css", stylesheet_destination
end

#inject_scoped_styles_moduleObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/generators/view_component/stylesheet/stylesheet_generator.rb', line 19

def inject_scoped_styles_module
  return unless File.exist?(absolute_component_rb_path)

  content = File.read(absolute_component_rb_path)
  return if content.include?("ViewComponent::ScopedStyles")

  class_name_pattern = component_class_name.split("::").last
  match = content.match(/^(\s*)class #{Regexp.escape(class_name_pattern)}\b[^\n]*\n/)
  return unless match

  indent = "#{match[1]}  "
  inject_into_file component_rb_path,
    "#{indent}include ViewComponent::ScopedStyles\n",
    after: match[0]
end

#update_view_templateObject



35
36
37
38
39
40
41
42
# File 'lib/generators/view_component/stylesheet/stylesheet_generator.rb', line 35

def update_view_template
  return if options["inline"] || options["call"]

  path = view_template_path
  return unless File.exist?(File.join(destination_root, path))

  gsub_file path, /<div/, '<div class="<%= component_class %>"', verbose: false
end