diff options
Diffstat (limited to 'railties/lib/rails/generators/erb')
11 files changed, 179 insertions, 0 deletions
diff --git a/railties/lib/rails/generators/erb/controller/controller_generator.rb b/railties/lib/rails/generators/erb/controller/controller_generator.rb new file mode 100644 index 0000000000..f8780d9c33 --- /dev/null +++ b/railties/lib/rails/generators/erb/controller/controller_generator.rb @@ -0,0 +1,21 @@ +require 'rails/generators/erb' + +module Erb + module Generators + class ControllerGenerator < Base + argument :actions, :type => :array, :default => [], :banner => "action action" + + def create_view_files + base_path = File.join("app/views", class_path, file_name) + empty_directory base_path + + actions.each do |action| + @action = action + @path = File.join(base_path, "#{action}.html.erb") + + template 'view.html.erb', @path + end + end + end + end +end diff --git a/railties/lib/rails/generators/erb/controller/templates/view.html.erb b/railties/lib/rails/generators/erb/controller/templates/view.html.erb new file mode 100644 index 0000000000..cd54d13d83 --- /dev/null +++ b/railties/lib/rails/generators/erb/controller/templates/view.html.erb @@ -0,0 +1,2 @@ +<h1><%= class_name %>#<%= @action %></h1> +<p>Find me in <%= @path %></p> diff --git a/railties/lib/rails/generators/erb/mailer/mailer_generator.rb b/railties/lib/rails/generators/erb/mailer/mailer_generator.rb new file mode 100644 index 0000000000..5266259e2f --- /dev/null +++ b/railties/lib/rails/generators/erb/mailer/mailer_generator.rb @@ -0,0 +1,20 @@ +require 'rails/generators/erb' + +module Erb + module Generators + class MailerGenerator < Base + argument :actions, :type => :array, :default => [], :banner => "method method" + + def create_view_folder + empty_directory File.join("app/views", file_path) + end + + def create_view_files + actions.each do |action| + @action, @path = action, File.join(file_path, action) + template "view.erb", File.join("app/views", "#{@path}.erb") + end + end + end + end +end diff --git a/railties/lib/rails/generators/erb/mailer/templates/view.erb b/railties/lib/rails/generators/erb/mailer/templates/view.erb new file mode 100644 index 0000000000..fcce7bd805 --- /dev/null +++ b/railties/lib/rails/generators/erb/mailer/templates/view.erb @@ -0,0 +1,3 @@ +<%= class_name %>#<%= @action %> + +Find me in app/views/<%= @path %> diff --git a/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb new file mode 100644 index 0000000000..de5b0e9c5d --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb @@ -0,0 +1,53 @@ +require 'rails/generators/erb' +require 'rails/generators/resource_helpers' + +module Erb + module Generators + class ScaffoldGenerator < Base + include Rails::Generators::ResourceHelpers + + argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" + + class_option :layout, :type => :boolean + class_option :singleton, :type => :boolean, :desc => "Supply to skip index view" + + def create_root_folder + empty_directory File.join("app/views", controller_file_path) + end + + def copy_index_file + return if options[:singleton] + copy_view :index + end + + def copy_edit_file + copy_view :edit + end + + def copy_show_file + copy_view :show + end + + def copy_new_file + copy_view :new + end + + def copy_form_file + copy_view :_form + end + + def copy_layout_file + return unless options[:layout] + template "layout.html.erb", + File.join("app/views/layouts", controller_class_path, "#{controller_file_name}.html.erb") + end + + protected + + def copy_view(view) + template "#{view}.html.erb", File.join("app/views", controller_file_path, "#{view}.html.erb") + end + + end + end +end diff --git a/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb new file mode 100644 index 0000000000..d02028d983 --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb @@ -0,0 +1,17 @@ +<%% form_for(@<%= singular_name %>) do |f| %> + <%%= f.error_messages %> + +<% for attribute in attributes -%> + <div class="field"> + <%%= f.label :<%= attribute.name %> %><br /> + <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %> + </div> +<% end -%> + <div class="actions"> + <%% if @<%= singular_name %>.new_record? %> + <%%= f.submit 'Create' %> + <%% else %> + <%%= f.submit 'Update' %> + <%% end %> + </div> +<%% end %> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb new file mode 100644 index 0000000000..5bc507ffc8 --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb @@ -0,0 +1,6 @@ +<h1>Editing <%= singular_name %></h1> + +<%%= render 'form' %> + +<%%= link_to 'Show', @<%= singular_name %> %> | +<%%= link_to 'Back', <%= plural_name %>_path %> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb new file mode 100644 index 0000000000..5e6a4af9e0 --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb @@ -0,0 +1,27 @@ +<h1>Listing <%= plural_name %></h1> + +<table> + <tr> +<% for attribute in attributes -%> + <th><%= attribute.human_name %></th> +<% end -%> + <th></th> + <th></th> + <th></th> + </tr> + +<%% @<%= plural_name %>.each do |<%= singular_name %>| %> + <tr> +<% for attribute in attributes -%> + <td><%%=h <%= singular_name %>.<%= attribute.name %> %></td> +<% end -%> + <td><%%= link_to 'Show', <%= singular_name %> %></td> + <td><%%= link_to 'Edit', edit_<%= singular_name %>_path(<%= singular_name %>) %></td> + <td><%%= link_to 'Destroy', <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %></td> + </tr> +<%% end %> +</table> + +<br /> + +<%%= link_to 'New <%= singular_name %>', new_<%= singular_name %>_path %> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/layout.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/layout.html.erb new file mode 100644 index 0000000000..6460e5b599 --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/templates/layout.html.erb @@ -0,0 +1,15 @@ +<!DOCTYPE html> +<html> +<head> + <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> + <title><%= controller_class_name %>: <%%= controller.action_name %></title> + <%%= stylesheet_link_tag 'scaffold' %> +</head> +<body> + +<p class="notice"><%%= flash[:notice] %></p> + +<%%= yield %> + +</body> +</html> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb new file mode 100644 index 0000000000..9a1c489331 --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb @@ -0,0 +1,5 @@ +<h1>New <%= singular_name %></h1> + +<%%= render 'form' %> + +<%%= link_to 'Back', <%= plural_name %>_path %> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb new file mode 100644 index 0000000000..25567957be --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb @@ -0,0 +1,10 @@ +<% for attribute in attributes -%> +<p> + <b><%= attribute.human_name %>:</b> + <%%=h @<%= singular_name %>.<%= attribute.name %> %> +</p> + +<% end -%> + +<%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> | +<%%= link_to 'Back', <%= plural_name %>_path %> |