Download:
Download(14 MB, 10:07)
Alternativer Downloadfür iPod & Apple TV(13.8 MB, 10:07)
Resourcen:
- Episode 216: Generators in Rails 3
- Episode 58: How to Make a Generator
- Thor
- Full episode source code
Quellcode:
[bash]
rails g generator –help
rails g generator layout
rails g layout –help
rails g layout admin
rails g layout foo –skip-stylesheet
[/bash]
[ruby]
# lib/generators/layout/layout_generator.rb
class LayoutGenerator < Rails::Generators::Base
source_root File.expand_path(‚../templates‘, __FILE__)
argument :layout_name, :type => :string, :default => "application"
class_option :stylesheet, :type => :boolean, :default => true, :desc => "Include stylesheet file."
def generate_layout
copy_file "stylesheet.css", "public/stylesheets/#{file_name}.css" if options.stylesheet?
template "layout.html.erb", "app/views/layouts/#{file_name}.html.erb"
end
private
def file_name
layout_name.underscore
end
end
[/ruby]
[html]
# lib/generators/layout/templates/layout.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<%- if options.stylesheet? -%>
<%%= stylesheet_link_tag "<%= file_name %>" %>
<%- end -%>
<%%= javascript_include_tag :defaults %>
<%%= csrf_meta_tag %>
<%%= yield(:head) %>
</head>
<body>
<div id="container">
<%% flash.each do |name, msg| %>
<%%= content_tag :div, msg, :id => "flash_#{name}" %>
<%% end %>
<%%= yield %>
</div>
</body>
</html>
[/html]