Class: ComponentGenerator

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

Instance Method Summary collapse

Instance Method Details

#clear_componentObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/generators/component/component_generator.rb', line 69

def clear_component
  return unless destroying?

  base_path = default_path + "components"
  base_path_dup = base_path.dup

  paths = split_name[0..-2].map do |split|
    base_path_dup += split
    [base_path_dup, split]
  end

  paths.reverse.each do |(path, split)|
    FileUtils.rm_rf(component_path)

    Dir.chdir(path)
    directories = Dir.glob("*").select do |entry|
      File.directory?(entry)
    end

    if directories.size == 0
      FileUtils.rm_rf(path)
      remove_line!(base_path + "index.js", split)
    else
      remove_line!(path + "index.js", component_name)
    end
  end

  FileUtils.rm_rf(component_path)
  remove_line!(base_path + "index.js", component_name)
end

#create_css_fileObject



13
14
15
# File 'lib/generators/component/component_generator.rb', line 13

def create_css_file
  template "#{stylesheet_engine}.erb", component_path + "#{name_with_namespace}.#{stylesheet_engine}"
end

#create_js_fileObject



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

def create_js_file
  template "js.erb", component_path + "#{name_with_namespace}.js"
  if stimulus?
    template "stimulus_controller_js.erb", component_path + "#{name_with_namespace}_controller.js"
  end
end

#create_locale_filesObject



28
29
30
31
32
33
34
35
# File 'lib/generators/component/component_generator.rb', line 28

def create_locale_files
  return unless locale?

  I18n.available_locales.each do |locale|
    @locale = locale
    template "locale.erb", component_path + "#{name_with_namespace}.#{locale}.yml"
  end
end

#create_rb_fileObject



24
25
26
# File 'lib/generators/component/component_generator.rb', line 24

def create_rb_file
  template "rb.erb", component_path + "#{module_name.underscore}.rb"
end

#create_view_fileObject



9
10
11
# File 'lib/generators/component/component_generator.rb', line 9

def create_view_file
  template "#{template_prefix}view.html.#{template_engine}.erb", component_path + "_#{name_with_namespace.underscore}.html.#{template_engine}"
end

#import_to_packsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/generators/component/component_generator.rb', line 37

def import_to_packs
  root_path = default_path
  base_path = root_path + "components"

  imports = []

  split_name[0..-2].each do |split|
    base_path += split
    file_path = base_path + "index.js"
    create_file(file_path) unless File.exist?(file_path)
    imports << base_path.relative_path_from(root_path)
  end

  root_path_dup = root_path.dup

  [Pathname.new("components"), *split_name[0..-2]].each do |split|
    root_path_dup += split
    import = imports.shift
    if import
      append_to_file(root_path_dup + "index.js") do
        "\nimport \"#{import}\";\n"
      end
      sort_lines_alphabetically!(root_path_dup + "index.js")
    end
  end

  append_to_file(base_path + "index.js") do
    "\nimport \"#{base_path.relative_path_from(root_path)}/#{component_name}/#{name_with_namespace.underscore}\";\n"
  end
  sort_lines_alphabetically!(base_path + "index.js")
end