Class: ComponentGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_componentObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/component/component_generator.rb', line 4

def add_component
  segments = name.underscore.split('/')
  fail("NAME must be of the form Family::ComponentName or family/component_name but got #{name.inspect}") if segments.size != 2
  @family, @comp = segments
  @family = @family.pluralize # Force plural
  @family_cst = @family.camelize.pluralize # Force plural
  @comp_cst = @comp.camelize # Tolerate singular and plural
  @args = args

  # If BaseComponents::ComponentAboutToBeGenerated is present, inherit from that
  if defined?(BaseComponents) && BaseComponents.const_defined?(@comp_cst)
    @parent_base_component_class = BaseComponents.const_get(@comp_cst)
    template 'with_base_component.rb.erb', "app/components/#{@family}/#{@comp}.rb"
    return
  end
  # If a Compony component with the specified name exists, inherit from that
  case @comp_cst
  when 'Index'
    template 'index.rb.erb', "app/components/#{@family}/#{@comp}.rb"
  when 'List'
    template 'list.rb.erb', "app/components/#{@family}/#{@comp}.rb"
  when 'Show'
    template 'show.rb.erb', "app/components/#{@family}/#{@comp}.rb"
  when 'Destroy'
    template 'destroy.rb.erb', "app/components/#{@family}/#{@comp}.rb"
  when 'Edit'
    template 'edit.rb.erb', "app/components/#{@family}/#{@comp}.rb"
  when 'Form'
    template 'form.rb.erb', "app/components/#{@family}/#{@comp}.rb"
  when 'New'
    template 'new.rb.erb', "app/components/#{@family}/#{@comp}.rb"
  else
    # Inherit from regular component
    template 'component.rb.erb', "app/components/#{@family}/#{@comp}.rb"
  end
end