From bcc4f1eadaa08f8c1dab85ce4d659668270f3f4a Mon Sep 17 00:00:00 2001 From: Lucas Mazza Date: Tue, 24 Jan 2012 16:23:50 -0200 Subject: [PATCH] add the `--bootstrap` option to the simple_form:install generator --- Gemfile | 1 + Gemfile.lock | 11 ++++ gemfiles/Gemfile-rails.3.0.x | 1 + gemfiles/Gemfile-rails.3.1.x | 1 + .../simple_form/install_generator.rb | 3 +- .../{simple_form.rb => simple_form.rb.tt} | 60 +++++++++++++++++++ test/generators/simple_form_generator_test.rb | 29 +++++++++ test/test_helper.rb | 3 + 8 files changed, 108 insertions(+), 1 deletion(-) rename lib/generators/simple_form/templates/config/initializers/{simple_form.rb => simple_form.rb.tt} (65%) create mode 100644 test/generators/simple_form_generator_test.rb diff --git a/Gemfile b/Gemfile index d4f1f9ec..11d1ee38 100644 --- a/Gemfile +++ b/Gemfile @@ -2,6 +2,7 @@ source :rubygems gemspec +gem 'railties', '~> 3.2.0' gem 'activemodel', '~> 3.2.0' gem 'actionpack', '~> 3.2.0' gem 'rake' diff --git a/Gemfile.lock b/Gemfile.lock index 35bbd2fa..3c4780f8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/gemfiles/Gemfile-rails.3.0.x b/gemfiles/Gemfile-rails.3.0.x index e77e0c19..9102f4fc 100644 --- a/gemfiles/Gemfile-rails.3.0.x +++ b/gemfiles/Gemfile-rails.3.0.x @@ -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' diff --git a/gemfiles/Gemfile-rails.3.1.x b/gemfiles/Gemfile-rails.3.1.x index 71ae5976..aff0ff55 100644 --- a/gemfiles/Gemfile-rails.3.1.x +++ b/gemfiles/Gemfile-rails.3.1.x @@ -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' diff --git a/lib/generators/simple_form/install_generator.rb b/lib/generators/simple_form/install_generator.rb index 03e5251d..82df4ea9 100644 --- a/lib/generators/simple_form/install_generator.rb +++ b/lib/generators/simple_form/install_generator.rb @@ -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' diff --git a/lib/generators/simple_form/templates/config/initializers/simple_form.rb b/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt similarity index 65% rename from lib/generators/simple_form/templates/config/initializers/simple_form.rb rename to lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt index 888775e6..468c3b8f 100644 --- a/lib/generators/simple_form/templates/config/initializers/simple_form.rb +++ b/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt @@ -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 diff --git a/test/generators/simple_form_generator_test.rb b/test/generators/simple_form_generator_test.rb new file mode 100644 index 00000000..4f49c20a --- /dev/null +++ b/test/generators/simple_form_generator_test.rb @@ -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 \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index b762ab03..9e6a6bf4 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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