Class: Repack::ViewGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/repack/view_generator.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#create_containerObject

ASSUMPTION: container will be PascalCased



32
33
34
35
36
37
# File 'lib/generators/repack/view_generator.rb', line 32

def create_container
  name = @view.titleize.gsub(/ /, '')
  file = "client/containers/#{name}.js"
  copy_file "boilerplate/views/ContainerTemplate.js", file
  gsub_file file, /Placeholder/, name
end

#create_entry_fileObject



24
25
26
27
28
29
# File 'lib/generators/repack/view_generator.rb', line 24

def create_entry_file
  file = "client/#{@view.underscore}.js"
  name = @view.titleize.gsub(/ /, '')
  copy_file "boilerplate/views/ViewTemplate.js", file
  gsub_file file, /Placeholder/, name
end

#create_rails_viewObject

ASSUMPTION: Rails controllers will be PascalCased, file & directory names will be snake_cased



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/generators/repack/view_generator.rb', line 40

def create_rails_view
  name = @view.underscore
  Rails.application.eager_load! if ApplicationController.descendants.length == 0
  controllers = ApplicationController.descendants.map { |cont| cont.name.gsub('Controller', '').underscore }
  dirname = controllers.include?(name) ? name : name.pluralize
  empty_directory "app/views/#{dirname}"
  if Gem.loaded_specs.has_key? 'haml-rails'
    file = "app/views/#{dirname}/index.html.haml"
    copy_file "boilerplate/views/rails_view.html.haml", file
    gsub_file file, /placeholder/, name
  else
    file = "app/views/#{dirname}/index.html.erb"
    copy_file "boilerplate/views/rails_view.html.erb", file
    gsub_file file, /placeholder/, name
  end
end

#normalize_view_nameObject



8
9
10
11
# File 'lib/generators/repack/view_generator.rb', line 8

def normalize_view_name
  raise "View name argument missing" if args.length == 0
  @view = args[0]
end

#update_webpack_entryObject

ASSUMPTION: entry file will be snake_cased



14
15
16
17
18
19
20
21
22
# File 'lib/generators/repack/view_generator.rb', line 14

def update_webpack_entry
  name = @view.underscore
  path = "'#{name}': './client/#{name}.js',"
  insert_into_file 'config/webpack.config.js', after: /entry: {\n/ do
    "\#{path}\n    CONFIG\n  end\nend\n"