Class: Ui::PagesGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/ui/pages/pages_generator.rb

Constant Summary collapse

ACTIONS =
%w(index show edit new)

Instance Method Summary collapse

Instance Method Details

#create_asset_filesObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/generators/ui/pages/pages_generator.rb', line 6

def create_asset_files
  destination_js = "app/assets/javascripts/application/views"
  destination_css = "app/assets/stylesheets/application/views"
  template_js = "javascript.js.erb"
  template_css = "stylesheet.scss.erb"

  ACTIONS.each do |action|

    # Check if need to generate for singular views (so don't create index view)
    next if file_name != plural_name && action == "index"

    @register_name = plural_name + ".#{action}"
    template template_js, File.join(destination_js, plural_name, "#{action}.js")
    template template_css, File.join(destination_css, plural_name, "#{action}.scss")
  end
end

#create_controller_filesObject



23
24
25
# File 'lib/generators/ui/pages/pages_generator.rb', line 23

def create_controller_files
  template 'controller.rb', File.join('app/controllers', "#{plural_name}_controller.rb")
end

#create_html_filesObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/ui/pages/pages_generator.rb', line 27

def create_html_files
  destination_html = "app/views/#{plural_name}"
  template_html = "template.html.erb"

  ACTIONS.each do |action|
    # Check if need to generate for singular views (so don't create index view)
    next if file_name != plural_name && action == "index"

    template template_html, File.join(destination_html, "#{action}.html.erb")
  end
  empty_directory File.join(destination_html, "templates")
end

#generate_routesObject



40
41
42
43
44
45
46
# File 'lib/generators/ui/pages/pages_generator.rb', line 40

def generate_routes
  if file_name == plural_name
    inject_into_file "config/routes.rb", "\tresources :#{plural_name}\n", :before => /^end/
  else
    inject_into_file "config/routes.rb", "\tresource :#{file_name}\n", :before => /^end/
  end
end