add the `--bootstrap` option to the simple_form:install generator

This commit is contained in:
Lucas Mazza 2012-01-24 16:23:50 -02:00
parent 67da8d8861
commit bcc4f1eada
8 changed files with 108 additions and 1 deletions

View File

@ -2,6 +2,7 @@ source :rubygems
gemspec
gem 'railties', '~> 3.2.0'
gem 'activemodel', '~> 3.2.0'
gem 'actionpack', '~> 3.2.0'
gem 'rake'

View File

@ -40,8 +40,17 @@ GEM
rack (1.4.1)
rack-cache (1.1)
rack (>= 0.4)
rack-ssl (1.3.2)
rack
rack-test (0.6.1)
rack (>= 1.0)
railties (3.2.0)
actionpack (= 3.2.0)
activesupport (= 3.2.0)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (~> 0.14.6)
rake (0.9.2.2)
rbx-require-relative (0.0.5)
rdoc (3.12)
@ -55,6 +64,7 @@ GEM
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
thor (0.14.6)
tilt (1.3.3)
tzinfo (0.3.31)
@ -65,6 +75,7 @@ DEPENDENCIES
actionpack (~> 3.2.0)
activemodel (~> 3.2.0)
mocha
railties (~> 3.2.0)
rake
rdoc
ruby-debug

View File

@ -2,6 +2,7 @@ source :rubygems
gem 'simple_form', :path => '..'
gem 'railties', '~> 3.0.0'
gem 'activemodel', '~> 3.0.0'
gem 'actionpack', '~> 3.0.0'
gem 'rake'

View File

@ -2,6 +2,7 @@ source :rubygems
gem 'simple_form', :path => '..'
gem 'railties', '~> 3.1.0'
gem 'activemodel', '~> 3.1.0'
gem 'actionpack', '~> 3.1.0'
gem 'rake'

View File

@ -3,7 +3,8 @@ module SimpleForm
class InstallGenerator < Rails::Generators::Base
desc "Copy SimpleForm default files"
source_root File.expand_path('../templates', __FILE__)
class_option :template_engine
class_option :template_engine, :desc => 'Template engine to be invoked (erb, haml or slim).'
class_option :bootstrap, :type => :boolean, :desc => 'Add the Twitter Bootstrap wrappers to the SimpleForm initializer.'
def copy_config
directory 'config'

View File

@ -44,6 +44,66 @@ SimpleForm.setup do |config|
b.use :error, :tag => :span, :class => :error
end
<% if options.bootstrap? %>
# Wrappers for forms and inputs using the Twitter Bootstrap toolkit.
# Check the Bootstrap docs (http://twitter.github.com/bootstrap)
# to learn about the different styles for forms and inputs,
# buttons and other elements.
config.wrappers :bootstrap, :tag => 'fieldset', :class => 'control-group', :error_class => 'error' do |b|
b.use :placeholder
b.use :label, :class => 'control-label'
b.use :tag => 'div', :class => 'controls' do |ba|
ba.use :input
ba.use :error, :tag => 'span', :class => 'help-inline'
ba.use :hint, :tag => 'p', :class => 'help-block'
end
end
config.wrappers :checkbox, :tag => 'fieldset', :class => 'control-group', :error_class => 'error' do |b|
b.use :placeholder
b.use :label, :class => 'control-label'
b.use :tag => 'div', :class => 'controls' do |ba|
ba.use :tag => 'label', :class => 'checkbox' do |bb|
bb.use :input
end
ba.use :error, :tag => 'span', :class => 'help-inline'
ba.use :hint, :tag => 'p', :class => 'help-block'
end
end
config.wrappers :inline_checkbox, :tag => 'span', :error_class => 'error' do |b|
b.use :placeholder
b.use :input
b.use :label_text
b.use :error, :tag => 'span', :class => 'help-inline'
b.use :hint, :tag => 'p', :class => 'help-block'
end
config.wrappers :prepend, :tag => 'fieldset', :class => "control-group", :error_class => 'error' do |b|
b.use :placeholder
b.use :label, :class => 'control-label'
b.use :hint, :tag => 'span', :class => 'help-block'
b.use :tag => 'div', :class => 'controls' do |input|
input.use :tag => 'div', :class => 'input-prepend' do |prepend|
prepend.use :input
end
input.use :error, :tag => 'span', :class => 'help-inline'
end
end
config.wrappers :append, :tag => 'fieldset', :class => "control-group", :error_class => 'error' do |b|
b.use :placeholder
b.use :label, :class => 'control-label'
b.use :hint, :tag => 'span', :class => 'help-block'
b.use :tag => 'div', :class => 'controls' do |input|
input.use :tag => 'div', :class => 'input-append' do |append|
append.use :input
end
input.use :error, :tag => 'span', :class => 'help-inline'
end
end
<% end %>
# Method used to tidy up errors.
# config.error_method = :first

View File

@ -0,0 +1,29 @@
require 'test_helper'
class SimpleFormGeneratorTest < Rails::Generators::TestCase
tests SimpleForm::Generators::InstallGenerator
destination File.expand_path('../../tmp', __FILE__)
setup :prepare_destination
test 'generates example locale file' do
run_generator
assert_file 'config/locales/simple_form.en.yml'
end
test 'generates the simple_form initializer' do
run_generator
assert_file 'config/initializers/simple_form.rb'
end
test 'generates the simple_form initializer with the bootstrap wrappers' do
run_generator %w(--bootstrap)
assert_file 'config/initializers/simple_form.rb', /config\.wrappers :bootstrap/
end
%W(erb haml slim).each do |engine|
test "generates the scaffold template when using #{engine}" do
run_generator ['-e', engine]
assert_file "lib/templates/#{engine}/scaffold/_form.html.#{engine}"
end
end
end

View File

@ -22,6 +22,9 @@ end
$:.unshift File.expand_path("../../lib", __FILE__)
require 'simple_form'
require "rails/generators/test_case"
require 'generators/simple_form/install_generator'
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each { |f| require f }
I18n.default_locale = :en