Skip to content

Commit

Permalink
add haml _form file and copy it into place if app template engine is …
Browse files Browse the repository at this point in the history
…haml
  • Loading branch information
jackdempsey committed Jul 13, 2010
1 parent 2a1b232 commit cbe2ddf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/generators/simple_form/install_generator.rb
Expand Up @@ -13,7 +13,11 @@ def copy_locale_file
end

def copy_scaffold_template
copy_file '_form.html.erb', 'lib/templates/erb/scaffold/_form.html.erb'
if Rails::Generators.options[:rails][:template_engine] == :haml
copy_file '_form.html.haml', 'lib/templates/haml/scaffold/_form.html.haml'
else
copy_file '_form.html.erb', 'lib/templates/erb/scaffold/_form.html.erb'
end
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions lib/generators/simple_form/templates/_form.html.haml
@@ -0,0 +1,18 @@
= simple_form_for(@<%= singular_name %>) do |f|
- if @<%= singular_name %>.errors.any?
#error_explanation
%h2
= pluralize(@<%= singular_name %>.errors.count, "error")
prohibited this <%= singular_name %> from being saved:

%ul
- @<%= singular_name %>.errors.full_messages.each do |msg|
%li= msg

.inputs
<%- attributes.each do |attribute| -%>
= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %>
<%- end -%>

.actions
= f.button :submit

2 comments on commit cbe2ddf

@josevalim
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks great! But I think there is a better way to get the template engine. You could do this:

class_option :template_engine

And then it would retrieve from Rails::Generators.options by default. And then in the method:

engine = options[:template_engine]
copy_file "_form.html.#{engine}", "lib/templates/#{engine}/scaffold/_form.html.#{engine}"

:)

@jackdempsey
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Learning the better/right way to get at some of this info was one of the motivations behind this. Thx for the tip on class_option--will be putting it in place next.

Please sign in to comment.