From bcc4f1eadaa08f8c1dab85ce4d659668270f3f4a Mon Sep 17 00:00:00 2001 From: Lucas Mazza Date: Tue, 24 Jan 2012 16:23:50 -0200 Subject: [PATCH 1/5] 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 From e506e60f6add331f70a2e698d85da47211a19f89 Mon Sep 17 00:00:00 2001 From: Lucas Mazza Date: Tue, 24 Jan 2012 16:54:28 -0200 Subject: [PATCH 2/5] add SimpleForm.default_wrapper --- .../templates/config/initializers/simple_form.rb.tt | 2 ++ lib/simple_form.rb | 3 +++ lib/simple_form/form_builder.rb | 2 +- test/generators/simple_form_generator_test.rb | 2 +- 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt b/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt index 468c3b8f..c1d9d83d 100644 --- a/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt +++ b/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt @@ -49,6 +49,8 @@ SimpleForm.setup do |config| # Check the Bootstrap docs (http://twitter.github.com/bootstrap) # to learn about the different styles for forms and inputs, # buttons and other elements. + config.default_wrapper :bootstrap + config.wrappers :bootstrap, :tag => 'fieldset', :class => 'control-group', :error_class => 'error' do |b| b.use :placeholder b.use :label, :class => 'control-label' diff --git a/lib/simple_form.rb b/lib/simple_form.rb index 76d332ed..fd1a64b2 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -113,6 +113,9 @@ module SimpleForm @@button_class = 'button' ## WRAPPER CONFIGURATION + # The default wrapper to be used by the FormBuilder. + mattr_accessor :default_wrapper + @@default_wrapper = :default @@wrappers = {} # Retrieves a given wrapper diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index 3eb9f873..d9b67d70 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -32,7 +32,7 @@ module SimpleForm def initialize(*) #:nodoc: super @defaults = options[:defaults] - @wrapper = SimpleForm.wrapper(options[:wrapper] || :default) + @wrapper = SimpleForm.wrapper(options[:wrapper] || SimpleForm.default_wrapper) end # Basic input helper, combines all components in the stack to generate diff --git a/test/generators/simple_form_generator_test.rb b/test/generators/simple_form_generator_test.rb index 4f49c20a..02ee2f63 100644 --- a/test/generators/simple_form_generator_test.rb +++ b/test/generators/simple_form_generator_test.rb @@ -17,7 +17,7 @@ class SimpleFormGeneratorTest < Rails::Generators::TestCase 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/ + assert_file 'config/initializers/simple_form.rb', /config\.wrappers :bootstrap/, /config\.default_wrapper :bootstrap/ end %W(erb haml slim).each do |engine| From 7f16eaaa045b95ce3f4c732864eed58e81652b72 Mon Sep 17 00:00:00 2001 From: Lucas Mazza Date: Tue, 24 Jan 2012 18:04:01 -0200 Subject: [PATCH 3/5] This should be an assignment --- .../simple_form/templates/config/initializers/simple_form.rb.tt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt b/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt index c1d9d83d..c00e18e7 100644 --- a/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt +++ b/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt @@ -49,7 +49,7 @@ SimpleForm.setup do |config| # Check the Bootstrap docs (http://twitter.github.com/bootstrap) # to learn about the different styles for forms and inputs, # buttons and other elements. - config.default_wrapper :bootstrap + config.default_wrapper = :bootstrap config.wrappers :bootstrap, :tag => 'fieldset', :class => 'control-group', :error_class => 'error' do |b| b.use :placeholder From dc2518691d469b858aad7c78c28ebb949e7a9902 Mon Sep 17 00:00:00 2001 From: Lucas Mazza Date: Tue, 24 Jan 2012 18:08:12 -0200 Subject: [PATCH 4/5] fix the generator test --- test/generators/simple_form_generator_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/generators/simple_form_generator_test.rb b/test/generators/simple_form_generator_test.rb index 02ee2f63..cc9c28e9 100644 --- a/test/generators/simple_form_generator_test.rb +++ b/test/generators/simple_form_generator_test.rb @@ -17,7 +17,7 @@ class SimpleFormGeneratorTest < Rails::Generators::TestCase 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/, /config\.default_wrapper :bootstrap/ + assert_file 'config/initializers/simple_form.rb', /config\.wrappers :bootstrap/, /config\.default_wrapper = :bootstrap/ end %W(erb haml slim).each do |engine| From ac81d71e9192bb988f015b94878f95612eb92cea Mon Sep 17 00:00:00 2001 From: Lucas Mazza Date: Tue, 24 Jan 2012 18:46:31 -0200 Subject: [PATCH 5/5] prints a message then the `--bootstrap` option is used. closes #376. --- lib/generators/simple_form/install_generator.rb | 6 ++++++ lib/generators/simple_form/templates/README | 12 ++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 lib/generators/simple_form/templates/README diff --git a/lib/generators/simple_form/install_generator.rb b/lib/generators/simple_form/install_generator.rb index 82df4ea9..9cbfabef 100644 --- a/lib/generators/simple_form/install_generator.rb +++ b/lib/generators/simple_form/install_generator.rb @@ -14,6 +14,12 @@ module SimpleForm engine = options[:template_engine] copy_file "_form.html.#{engine}", "lib/templates/#{engine}/scaffold/_form.html.#{engine}" end + + def show_readme + if behavior == :invoke && options.bootstrap? + readme "README" + end + end end end end diff --git a/lib/generators/simple_form/templates/README b/lib/generators/simple_form/templates/README new file mode 100644 index 00000000..e8742f22 --- /dev/null +++ b/lib/generators/simple_form/templates/README @@ -0,0 +1,12 @@ +=============================================================================== + + Be sure to have a copy of the Bootstrap stylesheet available on your app, + you can get in on http://twitter.github.com/bootstrap. + + Inside your views, use the 'simple_form_for' with one of the Bootstrap form + classes, '.form-horizontal', '.form-inline', '.form-search' or + '.form-vertical', as the following: + + = simple_form_for(@user, :html => {:class => 'form-horizontal' }) do |form| + +=============================================================================== \ No newline at end of file