diff --git a/lib/generators/haml/scaffold/scaffold_generator.rb b/lib/generators/haml/scaffold/scaffold_generator.rb new file mode 100644 index 0000000..99ff7c4 --- /dev/null +++ b/lib/generators/haml/scaffold/scaffold_generator.rb @@ -0,0 +1,40 @@ +require 'rails/generators/erb/scaffold/scaffold_generator' + +module Haml + module Generators + class ScaffoldGenerator < Erb::Generators::ScaffoldGenerator + source_root File.expand_path("../templates", __FILE__) + + def copy_view_files + available_views.each do |view| + filename = filename_with_extensions(view) + template "#{view}.html.haml", File.join("app/views", controller_file_path, filename) + end + + # add _resource partial for easy partial rendereing + filename = filename_with_extensions("_#{controller_file_path.split('/').last.singularize}") + template "_preview.html.haml", File.join("app/views", controller_file_path, filename) + end + + hook_for :form_builder, :as => :scaffold + + def copy_form_file + if options[:form_builder].nil? + filename = filename_with_extensions("_form") + template "_form.html.haml", File.join("app/views", controller_file_path, filename) + end + end + + protected + + def available_views + %w(index edit show new) + end + + def handler + :haml + end + + end + end +end \ No newline at end of file diff --git a/lib/templates/haml/scaffold/_form.html.haml b/lib/templates/haml/scaffold/_form.html.haml new file mode 100644 index 0000000..321495b --- /dev/null +++ b/lib/templates/haml/scaffold/_form.html.haml @@ -0,0 +1,17 @@ +- <%= singular_table_name %> ||= @<%= singular_table_name %> if defined?(@<%= singular_table_name %>) + += form_for <%= singular_table_name %> do |f| + - if <%= singular_table_name %>.errors.any? + #error_explanation + %h2= "#{pluralize(<%= singular_table_name %>.errors.count, "error")} prohibited this <%= singular_table_name %> from being saved:" + %ul + - <%= singular_table_name %>.errors.full_messages.each do |msg| + %li= msg + %fieldset +<% for attribute in attributes -%> + .field + = f.label :<%= attribute.name %> + = f.<%= attribute.field_type %> :<%= attribute.name %> +<% end -%> + .actions + = f.submit <%= singular_table_name %>.persisted? ? 'Save' : 'Create', class: 'button' diff --git a/lib/templates/haml/scaffold/_preview.html.haml b/lib/templates/haml/scaffold/_preview.html.haml new file mode 100644 index 0000000..883680f --- /dev/null +++ b/lib/templates/haml/scaffold/_preview.html.haml @@ -0,0 +1,4 @@ +%dl + %dt + = link_to <%= singular_table_name %>, title: <%= singular_table_name %> do + = <%= singular_table_name %>.to_s \ No newline at end of file diff --git a/lib/templates/haml/scaffold/edit.html.haml b/lib/templates/haml/scaffold/edit.html.haml new file mode 100644 index 0000000..e77a6eb --- /dev/null +++ b/lib/templates/haml/scaffold/edit.html.haml @@ -0,0 +1,3 @@ +%h1 Editing <%= human_name %> + += render 'form' \ No newline at end of file diff --git a/lib/templates/haml/scaffold/index.html.haml b/lib/templates/haml/scaffold/index.html.haml new file mode 100644 index 0000000..9644203 --- /dev/null +++ b/lib/templates/haml/scaffold/index.html.haml @@ -0,0 +1,12 @@ +.row + .small-6.columns + %h1 <%= human_name.pluralize %> + .small-6.columns + .right + = link_to 'New <%= human_name %>', new_<%= singular_table_name %>_path, class: 'button' + +.row + %ul.<%= plural_table_name %> + - @<%= plural_table_name %>.each do |<%= singular_table_name %>| + %li.<%= singular_table_name %> + = render <%= singular_table_name %> \ No newline at end of file diff --git a/lib/templates/haml/scaffold/new.html.haml b/lib/templates/haml/scaffold/new.html.haml new file mode 100644 index 0000000..b7b7395 --- /dev/null +++ b/lib/templates/haml/scaffold/new.html.haml @@ -0,0 +1,3 @@ +%h1 New <%= human_name %> + += render 'form' \ No newline at end of file diff --git a/lib/templates/haml/scaffold/show.html.haml b/lib/templates/haml/scaffold/show.html.haml new file mode 100644 index 0000000..ff8205c --- /dev/null +++ b/lib/templates/haml/scaffold/show.html.haml @@ -0,0 +1,16 @@ +.row + .small-6.columns + %h1= @<%= singular_table_name %> + .small-6.columns + .right + = link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>), class: 'button' + = link_to 'Delete', @<%= singular_table_name %>, :method => :delete, "data-confirm" => 'Are you sure?', class: 'button alert' + +.row +<% for attribute in attributes -%> + %p + %b <%= attribute.human_name %>: + = @<%= singular_table_name %>.<%= attribute.name %> +<% end -%> + +