class Formtastic::FormGenerator

Generates a Formtastic form partial based on an existing model. It will not overwrite existing files without confirmation.

@example !!!shell

$ rails generate formtastic:form Post

@example Copy the partial code to the pasteboard rather than generating a partial !!!shell

$ rails generate formtastic:form Post --copy

@example Return HAML or Slim output instead of default ERB !!!shell

$ rails generate formtastic:form Post --template-engine haml
$ rails generate formtastic:form Post --template-engine slim

@example Generate a form for specific model attributes !!!shell

$ rails generate formtastic:form Post title:string body:text

@example Generate a form for a specific controller !!!shell

$ rails generate formtastic:form Post --controller admin/posts

Public Instance Methods

create_or_show() click to toggle source
# File lib/generators/formtastic/form/form_generator.rb, line 37
def create_or_show
  @attributes = reflected_attributes if @attributes.empty?

  engine = options[:template_engine]

  if options[:copy]
    template = File.read("#{self.class.source_root}/_form.html.#{engine}")
    erb = ERB.new(template, nil, '-')
    generated_code = erb.result(binding).strip rescue nil
    puts "The following code has been copied 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.#{engine}", "app/views/#{controller_path}/_form.html.#{engine}"
  end
end

Protected Instance Methods

association_columns() click to toggle source

Collects association columns (relation columns) for the current class. Skips polymorphic associations because we can't guess which class to use for an automatically generated input.

# File lib/generators/formtastic/form/form_generator.rb, line 83
def association_columns
  model.reflect_on_all_associations(:belongs_to).select do |association_reflection|
    association_reflection.options[:polymorphic] != true
  end
end
content_columns() click to toggle source

Collects content columns (non-relation columns) for the current class. Skips Active Record Timestamps.

# File lib/generators/formtastic/form/form_generator.rb, line 75
def content_columns
  model.content_columns.select do |column|
    !Formtastic::Helpers::InputsHelper::SKIPPED_COLUMNS.include? column.name.to_sym
  end
end
controller_path() click to toggle source
# File lib/generators/formtastic/form/form_generator.rb, line 56
def controller_path
  @controller_path ||= if options[:controller]
    options[:controller].underscore
  else
    name.underscore.pluralize
  end
end
model() click to toggle source
# File lib/generators/formtastic/form/form_generator.rb, line 69
def model
  @model ||= name.camelize.constantize
end
reflected_attributes() click to toggle source
# File lib/generators/formtastic/form/form_generator.rb, line 64
def reflected_attributes
  columns = content_columns
  columns += association_columns
end
save_to_clipboard(data) click to toggle source
# File lib/generators/formtastic/form/form_generator.rb, line 89
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