Permalink
Browse files

Changed formtastic:form generator to generate a partial by default (i…

…nstead of copying to clipboard, which can be done with --copy)
  • Loading branch information...
1 parent 27baf42 commit 44e944c815b964f956e643bdb1fb863d0d35cd26 @justinfrench committed Jan 20, 2011
Showing with 25 additions and 48 deletions.
  1. +2 −1 CHANGELOG
  2. +5 −23 README.textile
  3. +18 −24 lib/generators/formtastic/form/form_generator.rb
View
@@ -1,13 +1,14 @@
[v2 branch]
+* Changed formtastic:form generator to generate a partial by default (instead of copying to clipboard, which can be done with --copy)
+* Removed formtastic_stylesheet_link_tag
* Improved some documentation, much more to come
* Tagged many modules as @private to remove from YARD documentation
* Added YARD as development dependency for a massive documentation effort
* Renamed Formtastic::SemanticFormHelper to Formtastic::Helpers::FormHelper (SemanticFormHelper still exists as a module which includes the new module, need to handle deprecation notices somehow)
* Renamed Formtastic::SemanticFormBuilder to Formtastic::FormBuilder (SemanticFormBuilder still exists as a subclass, need to handle deprecation notices somehow)
* Massive refactoring of formtastic.rb into around 30 files and modules like StringInput and ButtonsHelper
* Removed chunks of Rails 2 specific code, tests and documentation
-* Removed formtastic_stylesheet_link_tag
1.2.3
View
@@ -508,34 +508,16 @@ h3. Basic usage
<pre>
$ rails generate formtastic:form ModelName
+ exists app/views/posts
+ create app/views/posts/_form.html.erb
</pre>
-The results are something like this, with the ERB code printed out to the terminal
-
-<pre>
- $ rails generate formtastic:form Post
- # ---------------------------------------------------------
- # GENERATED FORMTASTIC CODE
- # ---------------------------------------------------------
-
- <% f.inputs do %>
- <%= f.input :title, :label => 'Title' %>
- <%= f.input :body, :label => 'Body' %>
- <%= f.input :published, :label => 'Published' %>
- <% end %>
-
- # ---------------------------------------------------------
- Copied to clipboard - just paste it!
-</pre>
-
-h3. Generating a partial with @--partial@
+The ERB code is saved to a partial file, but it will not overwrite existing files without a prompt.
-You can also ask Formtastic to *generate a partial* with the @--partial@ option, placing it in the correct location inside your @app/views@ directory:
+h3. Copying the ERB code to the clipboard instead with @--copy@
<pre>
- $ rails generate formtastic:form Post --partial
- exists app/views/posts
- create app/views/posts/_form.html.erb
+ $ rails generate formtastic:form Post --copy
</pre>
h3. Specifying HAML instead of ERB with @--haml@
@@ -1,29 +1,30 @@
# encoding: utf-8
module Formtastic
- # Copies formtastic.css and formtastic_changes.css to public/stylesheets/ and a config initializer
- # to config/initializers/formtastic_config.rb.
+ # Generates a Formtastic form partial based on an existing model. It will not overwrite existing
+ # files without confirmation.
#
# @example
# $ rails generate formtastic:form Post
- # @example Write a partial to the file system
- # $ rails generate formtastic:form Post --partial
+ # @example Copy the partial code to the pasteboard rather than generating a partial
+ # $ rails generate formtastic:form Post --copy
# @example Return HAML output instead of ERB
# $ rails generate formtastic:form Post --haml
# @example Generate a form for specific model attributes
# $ rails generate formtastic:form Post title:string body:text
class FormGenerator < Rails::Generators::NamedBase
- desc "Generates formtastic form code based on an existing model. By default the " <<
- "generated code will be printed out directly in the terminal, and also copied " <<
- "to clipboard. Can optionally be saved into partial directly."
+ desc "Generates a Formtastic form partial based on an existing model."
- argument :name, :type => :string, :required => true, :banner => 'ExistingModelName'
- argument :attributes, :type => :array, :default => [], :banner => 'field:type field:type'
+ argument :name, :type => :string, :required => true, :banner => 'MyExistingModel'
+ argument :attributes, :type => :array, :default => [], :banner => 'attribute:type attribute:type'
class_option :haml, :type => :boolean, :default => false, :group => :formtastic,
:desc => "Generate HAML instead of ERB"
- class_option :partial, :type => :boolean, :default => false, :group => :formtastic,
- :desc => 'Generate a form partial in the model views path, i.e. "_form.html.erb" or "_form.html.haml"'
+ class_option :partial, :type => :boolean, :default => true, :group => :formtastic,
+ :desc => 'Generate a form partial in the model views path (eg `posts/_form.html.erb`'
+
+ class_option :copy, :type => :boolean, :default => false, :group => :formtastic,
+ :desc => 'Copy the generated code the clipboard instead of generating a partial file."'
class_option :controller, :type => :string, :default => false, :group => :formtastic,
:desc => 'Generate for custom controller/view path - in case model and controller namespace is different, i.e. "admin/posts"'
@@ -33,22 +34,15 @@ class FormGenerator < Rails::Generators::NamedBase
def create_or_show
@attributes = self.columns if @attributes.empty?
- if options[:partial]
- empty_directory "app/views/#{controller_path}"
- template "_form.html.#{template_type}", "app/views/#{controller_path}/_form.html.#{template_type}"
- else
+ if options[:copy]
template = File.read("#{self.class.source_root}/_form.html.#{template_type}")
erb = ERB.new(template, nil, '-')
generated_code = erb.result(binding).strip rescue nil
-
- puts "# ---------------------------------------------------------"
- puts "# GENERATED FORMTASTIC CODE"
- puts "# ---------------------------------------------------------"
- puts
- puts generated_code || "Nothing could be generated - model exists?"
- puts
- puts "# ---------------------------------------------------------"
- puts "Copied to clipboard - just paste it!" if save_to_clipboard(generated_code)
+ puts "The following code has been to the clipboard, just paste it in your views:" if save_to_clipboard(generated_code)
+ puts generated_code || "Error: Nothing generated. Does the model exist?"
+ else
+ empty_directory "app/views/#{controller_path}"
+ template "_form.html.#{template_type}", "app/views/#{controller_path}/_form.html.#{template_type}"
end
end

0 comments on commit 44e944c

Please sign in to comment.