Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add small fixes for bootstrap 3. Add tests. #85

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d862498
Add small fixes for bootstrap 3. Add tests.
schneikai Nov 19, 2013
0f5c8a2
Fix typo in FormBuilder.
schneikai Nov 20, 2013
847cace
Use relative path for screenshot.
schneikai Nov 20, 2013
e61a7f6
use a project-specific .gitignore
schneikai Nov 21, 2013
de298e9
remove unused variable
schneikai Nov 26, 2013
0712888
use html_options for helpers that use this instead of just options
schneikai Nov 26, 2013
e6d0b5c
Update readme. Use official Twitter bootstrap-sass gem. Allow to add …
schneikai Feb 8, 2014
4370845
Make it more clear how to pass html attributes to the input type
schneikai Feb 8, 2014
25ca7bf
add a way to display error messages from base or hidden fields
schneikai Feb 11, 2014
5100190
use I18n to translate error messages; allow html options for errors h…
schneikai Feb 11, 2014
dec35c9
do not escape html_safe labels
schneikai Feb 27, 2014
ecd7bff
do not escape html_safe labels
schneikai Feb 27, 2014
476d7e8
fix checkbox options rendered as label if label is not given
schneikai Mar 1, 2014
4f58e04
make labels work the same for checkboxes and fields. use I18n transla…
schneikai Mar 3, 2014
ac4ce58
allow HTML in I18n attribute translations.
schneikai Mar 3, 2014
5524cec
use bootstrap-sass in dummy app. fix input-groups
schneikai Mar 5, 2014
82d838d
update readme. update screenshot. add documentation about checkboxes.
schneikai Mar 5, 2014
757eac8
highlight required fields. update readme and screenshots.
schneikai Mar 5, 2014
78de96a
fix error messages markup on check boxes and radio buttons
schneikai Mar 5, 2014
94b8064
use default Rails engine setup for the Gem. add datepicker for date i…
schneikai Mar 8, 2014
48bd22c
update example screenshot
schneikai Mar 8, 2014
790a01a
reorganize engine code so it's more like the Rails default.
schneikai Mar 18, 2014
2801e5e
update gitignore
schneikai Apr 6, 2014
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/.bundle
/db/*.sqlite3
/db/*.sqlite3-journal
/log/*.log
/tmp
/test/dummy/db/*.sqlite3
/test/dummy/db/*.sqlite3-journal
/test/dummy/log/*.log
/test/dummy/tmp
.DS_Store
.rvmrc
/bin
Gemfile.lock
50 changes: 0 additions & 50 deletions CHANGELOG.markdown

This file was deleted.

14 changes: 14 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
source "https://rubygems.org"

# Declare your gem's dependencies in hive.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec

# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.

# To use debugger
# gem 'debugger'
139 changes: 91 additions & 48 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
Twitter Bootstrap Form For
==========================

*_WARNING! This is for Rails 4 and Bootstrap 3 (current in RC)._*

`twitter_bootstrap_form_for` is a Rails FormBuilder DSL that, like Formtastic,
makes it easier to create semantically awesome, readily-stylable, and
wonderfully accessible HTML forms in your Rails applications. It abides by
Expand All @@ -16,82 +14,127 @@ Formtastic does), it only lightly wraps the existing Rails form tag helpers.
## Dependencies ##

* Rails 4
* Bootstrap-sass, pull from branch 3 off github

`
gem 'bootstrap-sass', :git => 'git://github.com/thomas-mcdonald/bootstrap-sass.git', :branch => '3'
`
* Bootstrap 3 (Checkout the [bootstrap-sass] gem.)

## Syntax ##

```haml
= twitter_bootstrap_form_for @user, layout: 'vertical (default) OR horizontal OR inline', default_div_class: 'col-lg-10 (default)', role: 'form (default)' do |user|
= twitter_bootstrap_form_for @user, layout: [:vertical (default) OR :horizontal OR :inline], default_div_class: 'col-lg-10 (default)', role: 'form (default)' do |f|
// By default error messages on fields are rendered inline with the field and you
// don't have to do something special to display them. But if you have error messages
// on your model that have no corresponding field in the form such as hidden fields
// or if you use *model.errors.add :base* for example you can use the following helper.
// To prevent errors from beeing listed twice you can use the *only* or *except*
// option like this *= f.errors only: :base* for example.
= f.errors

/ wraps a section in a fieldset with the provided legend text
= user.inputs 'Sign up', :class => 'sign_up' do
= f.inputs 'Sign up', class: 'sign_up' do

/ generate a email field
= f.email_field :email

/ required fields are marked with an * prepended to their labels.
/ A field is required if the model responds with "true" to "[attribute]_required?"
/ or if you specify "required: true|false" on the form field.
= f.email_field :email, required: true

/ generates a standard email field; also showing overriding the div_class around the input element on a horizontal form
= user.email_field :email, :placeholder => '[email protected]', div_class: 'col-md-6'
/ generate a text field with a custom label, a placeholder text, custom attributes
/ (class and id) for the input element, a custom class for the wrapper around the input element
/ and custom attributes for the form-group wrapper.
= f.text_field :name, 'Username', placeholder: 'Choose your user name.', class: 'input-lg', id: 'username-input',
div_class: 'col-md-6',
form_group_html: { id: 'bar', class: 'foo' }

/ generates a password field with a descriptive aside
= user.password_field :password do
/ generate a password field with a descriptive aside
= f.password_field :password do
%span.help-block
Must be no larger than 6 characters<br/>
Must contain only the letters 'x' or 'p'

/ a field with a custom label
= user.password_field :password_confirmation, 'Confirm Password'
/ generate a field with a custom label
= f.password_field :password_confirmation, 'Confirm Password'

/ input fields with custom add-ons
= user.text_field :twitter_id, 'Twitter', :class => 'medium', :add_on => :prepend do
%span.add-on @
/ generate a field with custom add-ons
= f.text_field :twitter_id, 'Twitter', add_on: :prepend do
%span.input-group-addon @

/ select fields now have the second parameter as a label
= user.date_select :born_on, 'Born on', {}, :class => 'small'
/ generate a datepicker (to generate a regulare date_select field use
/ "datepicker: false" as a option)
= f.date_select :born_on

/ inline inputs are not automatically labeled
= user.inline 'Interests' do |inline|
#{inline.text_field :interest_1, :class => 'small'},
#{inline.text_field :interest_2, :class => 'small'}, and
#{inline.text_field :interest_3, :class => 'small'}
/ generate a single checkbox
= f.check_box :agree

/ group of radio buttons
= user.toggles 'Email Preferences' do
= user.radio_button :email, 'HTML Email', :html, :checked => true
= user.radio_button :email, 'Plain Text', :plain
/ generate a group of checkboxes using the *toggles* method and add custom labels
= f.toggles 'Agreements' do
= f.check_box :agree, 'I agree to the abusive Terms and Conditions'
= f.check_box :spam, 'I agree to receive all sorts of spam'
= f.check_box :spammer, 'I agree to let the site spam others through my Twitter account'

/ group of checkboxes
= user.toggles 'Agreements' do
= user.check_box :agree, 'I agree to the abusive Terms and Conditions'
= user.check_box :spam, 'I agree to receive all sorts of spam'
= user.check_box :spammer, 'I agree to let the site spam others through my Twitter account'
/ generate a group of radio buttons using the *toggles* method and add custom labels
= f.toggles 'Email Preferences' do
= f.radio_button :email, 'HTML Email', :html, checked: true
= f.radio_button :email, 'Plain Text', :plain

/ wraps buttons in a distinctive style
= user.actions do
= user.submit 'Sign up'
= button_tag 'Cancel', :type => 'reset', :class => 'btn'
= f.actions do
= f.submit 'Sign up'
= button_tag 'Cancel', type: 'reset', class: 'btn'
```

That code produces the following output, with no custom stylesheets.

![](https://github.com/stouset/twitter_bootstrap_form_for/raw/master/examples/screenshot.png)
![](examples/screenshot.png?raw=true)

That's it. All of the Rails field helpers you know and love work just like
their normal FormBuilder counterparts, but with minor extensions to expose
the functionality anticipated by Twitter Bootstrap.

## Form Helper Changes ##
## Datepicker ##

By default a datepicker is rendered for date and/or time inputs via the
bootstrap-datepicker-rails Gem. You need require "twitter_bootstrap_form_for"
in your stylesheet and javascript manifest files to make it work.

In "app/assets/javascripts/application.js" add

//= require twitter_bootstrap_form_for

In "app/assets/stylesheets/application.css" add

*= require twitter_bootstrap_form_for

To create a datepicker in your form use

= f.date_select :born_on

To customize the datepicker use

= f.date_select :born_on, datepicker: { language: 'de', today_btn: 'linked', today_highlight: true, autoclose: true }

Datepicker options are documented at https://github.com/eternicode/bootstrap-datepicker but
make sure you use the Ruby dash syntax when specifying the options via the form helper.
Write "today_btn" instead of "todayBtn". More examples via http://eternicode.github.io/bootstrap-datepicker/

If you set a language different from english "en" you need to include the localized
javascript files in your "app/assets/javascripts/application.js" file right where you
require "twitter_bootstrap_form_for" like this:

//= require bootstrap-datepicker/locales/bootstrap-datepicker.de.js
//= require bootstrap-datepicker/locales/bootstrap-datepicker.es.js
//= require bootstrap-datepicker/locales/bootstrap-datepicker.fr.js
...

The changes this `FormBuilder` effects to the existing Rails form helpers is
simple:
To disable the datepicker and render a regular input field use

* the second parameter becomes the label (pass false to disable, nil for default)
* the last options hash accepts an `:add_on` key
* if a block is passed, the HTML it outputs is placed immediately after the input
= f.date_select :born_on, datepicker: false

## Known Bugs ##
Important! The datepicker uses the Rails default date format from I18n.t('date.formats.default').
If change that via the "format" option on the datepicker make sure Rails can parse that
custom format. You might need to overwrite the attribute setter on your model for that.

- inline fields don't receive error markup ([issue #28])

[Twitter Bootstrap]: http://twitter.github.com/bootstrap/
[issue #28]: https://github.com/stouset/twitter_bootstrap_form_for/issues/28
[Twitter Bootstrap]: http://twitter.github.com/bootstrap
[bootstrap-sass]: https://github.com/twbs/bootstrap-sass
[bootstrap-datepicker-rails]: https://github.com/Nerian/bootstrap-datepicker-rails
31 changes: 31 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end

require 'rdoc/task'

RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'TwitterBootstrapFormFor'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('lib/**/*.rb')
end

APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constant isn't used anywhere.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the default Rake file you get when you do rails plugin new --full to get the dummy app and test directories. I could remove stuff in there but maybe it is good for something? :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. Thanks.

load 'rails/tasks/engine.rake'

Bundler::GemHelper.install_tasks

require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end

task default: :test
23 changes: 23 additions & 0 deletions app/assets/javascripts/twitter_bootstrap_form_for.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//= require bootstrap-datepicker/core

$(function() {
// Initialize the datepicker.
$('[data-behaviour~=datepicker]').each(function(){
var element = $(this);
var options = {};

// Convert data attributes whoes names begin with "datepicker" to options
// on the datepicker.
$.each(element.data(), function(k,v) {
if (k.match("^datepicker")) {
// Remove "datepicker" from the name and make the first letter lower case
// "datepickerAutoclose" becomes "autoclose"
option = k.replace(/^datepicker/, '');
option = option.charAt(0).toLowerCase() + option.slice(1);
options[option] = v;
}
});

element.datepicker(options);
});
});
3 changes: 3 additions & 0 deletions app/assets/stylesheets/twitter_bootstrap_form_for.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
*= require bootstrap-datepicker
*/
Binary file modified examples/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions lib/twitter_bootstrap_form_for.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'action_view'
require 'bootstrap-datepicker-rails'

module TwitterBootstrapFormFor
autoload :FormBuilder, 'twitter_bootstrap_form_for/form_builder'
autoload :FormHelpers, 'twitter_bootstrap_form_for/form_helpers'
autoload :Railtie, 'twitter_bootstrap_form_for/railtie'
autoload :Datepicker, 'twitter_bootstrap_form_for/datepicker'
autoload :VERSION, 'twitter_bootstrap_form_for/version'
end

TwitterBootstrapFormFor::Railtie # trigger loading the Railtie
require 'twitter_bootstrap_form_for/engine'
Loading