diff options
Diffstat (limited to 'railties/lib/rails/generators/erb/scaffold')
7 files changed, 119 insertions, 0 deletions
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..f607e580a5 --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb @@ -0,0 +1,41 @@ +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_view_files + views = available_views + views.delete("index") if options[:singleton] + + views.each do |view| + filename = filename_with_extensions(view) + template filename, File.join("app/views", controller_file_path, filename) + end + end + + def copy_layout_file + return unless options[:layout] + template filename_with_extensions(:layout), + File.join("app/views/layouts", controller_class_path, filename_with_extensions(controller_file_name)) + end + + protected + + def available_views + %w(index edit show new _form) + 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..01ec58c615 --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb @@ -0,0 +1,13 @@ +<%%= 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"> + <%%= f.submit %> + </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..d30d306d42 --- /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><%%= <%= 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 <%= human_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..3f64be0c45 --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/templates/layout.html.erb @@ -0,0 +1,17 @@ +<!DOCTYPE html> +<html> +<head> + <title><%= controller_class_name %>: <%%= controller.action_name %></title> + <%%= stylesheet_link_tag 'scaffold' %> + <%%= javascript_include_tag :defaults %> + <%%= csrf_meta_tag %> +</head> +<body> + +<p class="notice"><%%= notice %></p> +<p class="alert"><%%= alert %></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..24f13fc0f8 --- /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> + <%%= @<%= singular_name %>.<%= attribute.name %> %> +</p> + +<% end -%> + +<%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> | +<%%= link_to 'Back', <%= plural_name %>_path %> |