Class: Layout::Generators::LayoutGenerator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



9
10
11
# File 'lib/generators/layout/layout_generator.rb', line 9

def app_name
  @app_name
end

Instance Method Details

If ‘About’ or ‘Contact’ views exist in known locations, add navigation links



23
24
25
26
27
28
29
30
31
# File 'lib/generators/layout/layout_generator.rb', line 23

def add_navigation_links
  # not yet accommodating Haml and Slim (we'll need different substitutions)
  if File.exists?('app/views/pages/about.html.erb')
    insert_into_file 'app/views/layouts/_navigation.html.erb', "\n  <li><%= link_to 'About', page_path('about') %></li>", :before => "\n</ul>"
  end
  if File.exists?('app/views/contacts/new.html.erb')
    insert_into_file 'app/views/layouts/_navigation.html.erb', "\n  <li><%= link_to 'Contact', new_contact_path %></li>", :before => "\n</ul>"
  end
end

#generate_layoutObject

Create an application layout file with partials for messages and navigation



12
13
14
15
16
17
18
19
20
# File 'lib/generators/layout/layout_generator.rb', line 12

def generate_layout
  app = ::Rails.application
  @app_name = app.class.to_s.split("::").first
  ext = app.config.generators.options[:rails][:template_engine] || :erb
  # so far, I've only got templates for ERB, but Haml and Slim could be added
  template "#{framework_name}-application.html.#{ext}", "app/views/layouts/application.html.#{ext}"
  copy_file "#{framework_name}-messages.html.#{ext}", "app/views/layouts/_messages.html.#{ext}"
  copy_file "#{framework_name}-navigation.html.#{ext}", "app/views/layouts/_navigation.html.#{ext}"
end