Skip to content
Browse files

Removed formtastic:form generator and changed formtastic:install gene…

…rator

to override scaffold generator (closes #461)
  • Loading branch information...
1 parent 6789a46 commit 6d9cae1219a764ffe733f18fb4000ec030cde0f9 @sobrinho sobrinho committed Jun 5, 2011
View
2 CHANGELOG
@@ -35,6 +35,8 @@
* Added non-standard zoom:1; to clearfixes to support IE better
* Changed CSS coding style to multi-line so we can annotate the styles better
* Fixed that overly specific CSS rules weren't applied to nested form elements properly
+* Removed formtastic:form generator and changed formtastic:install generator
+to override scaffold generator
1.2.4.beta (unreleased)
View
3 formtastic.gemspec
@@ -26,9 +26,6 @@ Gem::Specification.new do |s|
(and some stylesheets, in Rails 3.0.x) into your application:
rails generate formtastic:install
- To generate some semantic form markup for your existing models, just run:
- rails generate formtastic:form MODEL_NAME
-
Find out more and get involved:
http://github.com/justinfrench/formtastic
http://rdoc.info/github/justinfrench/formtastic/master/frames
View
95 lib/generators/formtastic/form/form_generator.rb
@@ -1,95 +0,0 @@
-# encoding: utf-8
-module Formtastic
- # 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 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
- # @example Generate a form for a specific controller
- # $ rails generate formtastic:form Post --controller admin/posts
- class FormGenerator < Rails::Generators::NamedBase
- desc "Generates a Formtastic form partial based on an existing model."
-
- argument :name, :type => :string, :required => true, :banner => 'MyExistingModel'
- argument :attributes, :type => :array, :default => [], :banner => 'attribute attribute'
-
- class_option :haml, :type => :boolean, :default => false, :group => :formtastic,
- :desc => "Generate HAML instead of ERB"
-
- 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"'
-
- source_root File.expand_path('../../../templates', __FILE__)
-
- def create_or_show
- @attributes = reflected_attributes if @attributes.empty?
-
- 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 "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
-
- protected
-
- def template_type
- @template_type ||= options[:haml] ? :haml : :erb
- end
-
- def controller_path
- @controller_path ||= if options[:controller]
- options[:controller].underscore
- else
- name.underscore.pluralize
- end
- end
-
- def reflected_attributes
- columns = model.content_columns.map{|column| column.name}
- columns += model.reflect_on_all_associations.map{|association| association.name.to_s}
- columns -= %w(created_at updated_at)
- end
-
- def model
- @model ||= name.camelize.constantize
- end
-
- def save_to_clipboard(data)
- return unless data
-
- begin
- case RUBY_PLATFORM
- when /win32/
- require 'win32/clipboard'
- ::Win32::Clipboard.data = data
- when /darwin/ # mac
- `echo "#{data}" | pbcopy`
- else # linux/unix
- `echo "#{data}" | xsel --clipboard` || `echo "#{data}" | xclip`
- end
- rescue LoadError
- false
- else
- true
- end
- end
- end
-end
View
17 lib/generators/formtastic/install/install_generator.rb
@@ -9,22 +9,27 @@ module Formtastic
#
# @todo Test with Rails 3.0
class InstallGenerator < Rails::Generators::Base
-
source_root File.expand_path('../../../templates', __FILE__)
-
+ class_option :template_engine
+
if ::Rails::VERSION::MAJOR == 3 && ::Rails::VERSION::MINOR >= 1
- # Rails 3.1.x has the asset pipeline, no need to copy CSS files any more
+ # Rails 3.1 has the asset pipeline, no need to copy CSS files any more
desc "Copies a config initializer to config/initializers/formtastic.rb"
def copy_files
- template 'formtastic.rb', 'config/initializers/formtastic.rb'
+ copy_file 'formtastic.rb', 'config/initializers/formtastic.rb'
end
else
# Rails 3.0 doesn't have an asset pipeline, so we copy in CSS too
desc "Copies formtastic.css to public/stylesheets/ and a config initializer to config/initializers/formtastic.rb"
def copy_files
- template 'formtastic.rb', 'config/initializers/formtastic.rb'
- template '../../../app/assets/stylesheets/formtastic.css', 'public/stylesheets/formtastic.css'
+ copy_file 'formtastic.rb', 'config/initializers/formtastic.rb'
+ copy_file '../../../app/assets/stylesheets/formtastic.css', 'public/stylesheets/formtastic.css'
end
end
+
+ def copy_scaffold_template
+ engine = options[:template_engine]
+ copy_file "_form.html.#{engine}", "lib/templates/#{engine}/scaffold/_form.html.#{engine}"
+ end
end
end
View
12 lib/generators/templates/_form.html.erb
@@ -1,7 +1,11 @@
-<%%= semantic_form_for @<%= name.underscore %> do |f| %>
+<%%= semantic_form_for @<%= singular_name %> do |f| %>
<%%= f.inputs do %>
- <% @attributes.each do |attribute| -%>
- <%%= f.input :<%= attribute %> %>
- <% end -%>
+ <%- attributes.each do |attribute| -%>
+ <%%= f.input :<%= attribute.name %> %>
+ <%- end -%>
+ <%% end %>
+
+ <%%= f.buttons do %>
+ <%%= f.commit_button %>
<%% end %>
<%% end %>
View
9 lib/generators/templates/_form.html.haml
@@ -1,5 +1,8 @@
-= semantic_form_for @<%= name.underscore %> do |f|
+= semantic_form_for @<%= singular_name %> do |f|
= f.inputs do
- <% @attributes.each do |attribute| -%>
- = f.input :<%= attribute %>
+ <% attributes.each do |attribute| -%>
+ = f.input :<%= attribute.name %>
<% end -%>
+
+ = f.buttons do
+ = f.commit_button

5 comments on commit 6d9cae1

@joel
joel commented on 6d9cae1 Jun 9, 2011

Please update formtastic / README.textile

@justinfrench
Owner

@joel I noticed the same thing and have a patch locally, will push today.

@joel
joel commented on 6d9cae1 Jun 10, 2011

You're really fast ! Ok but how can use generator for form now ? You out of master branch for put in a other project and a new gem file ? Or it's definitely give up ?

@sobrinho
Collaborator

You need to run the formtastic:install generator once and then, the scaffold generator will use the formtastic for _form partial ;)

@joel
joel commented on 6d9cae1 Jun 10, 2011

Awesome !!!

Please sign in to comment.
Something went wrong with that request. Please try again.