bootstrap-form 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in bootstrap-form.gemspec
4
+ gemspec
@@ -0,0 +1,54 @@
1
+ # Bootstrap Form
2
+
3
+ Helpers to easily use the twitter bootstrap CSS framework on your forms.
4
+
5
+ For more info about [Twitter
6
+ Bootstrap go here](http://twitter.github.com/bootstrap).
7
+
8
+ ## Usage
9
+
10
+ Add the gem to your Gemfile
11
+
12
+ gem 'bootstrap-form'
13
+
14
+ Bundle install
15
+
16
+ bundle install
17
+
18
+ ## Text Field
19
+
20
+ Will add all the divs around the textfield and the classes required to
21
+ work with bootstrap.
22
+
23
+ form_for @account do |f|
24
+ f.bootstrap_text_field :name
25
+ end
26
+
27
+
28
+ Will generate something like:
29
+
30
+ <div class="clearfix">
31
+ <label for="account_name">Name</label>
32
+ <div class="input">
33
+ <input class="xlarge" id="account_name" name="account_name" size="30" type="text">
34
+ </div>
35
+ </div>
36
+
37
+ ## Password Field
38
+
39
+ Same as above, but, with a password_field
40
+
41
+ form_for @account do |f|
42
+ f.bootstrap_password_field :password
43
+ f.bootstrap_password_field :password_confirmation
44
+ end
45
+
46
+ ## Error handling
47
+
48
+ All fields will automatically add the classes to show errors with the Twitter
49
+ bootstrap styles.
50
+
51
+ # TODO
52
+
53
+ * Refactor the code, there's lots of things that can be done better
54
+ * More form inputs
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ task :default => :test
6
+
7
+ Rake::TestTask.new(:test) do |test|
8
+ test.libs << 'lib' << 'test'
9
+ test.pattern = 'test/**/*_test.rb'
10
+ test.verbose = true
11
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "bootstrap-form/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "bootstrap-form"
7
+ s.version = Bootstrap::Form::VERSION
8
+ s.authors = ["David Padilla"]
9
+ s.email = ["david@crowdint.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Twitter Bootstrap Form helpers}
12
+ s.description = %q{Twitter Bootstrap Form helpers}
13
+
14
+ s.rubyforge_project = "bootstrap-form"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency 'rails', '~> 3.0'
22
+
23
+ s.add_development_dependency "minitest"
24
+ s.add_development_dependency "rr"
25
+ end
@@ -0,0 +1,8 @@
1
+ require "bootstrap-form/version"
2
+ require "bootstrap-form/form_helper"
3
+
4
+ module Bootstrap
5
+ module Form
6
+ # Your code goes here...
7
+ end
8
+ end
@@ -0,0 +1,45 @@
1
+ module ActionView
2
+ module Helpers
3
+ module FormHelper
4
+ def bootstrap_text_field(object_name, method, options={})
5
+ error_messages = options[:object].errors[method]
6
+ clearfix_tag = error_messages.empty? ? 'clearfix' : 'clearfix error'
7
+
8
+ content_tag(:div,
9
+ label(object_name, method) +
10
+ content_tag(:div,
11
+ text_field(object_name, method, options) +
12
+ inline_help_tag(error_messages),
13
+ class: 'input'),
14
+ class: clearfix_tag)
15
+ end
16
+
17
+ def bootstrap_password_field(object_name, method, options={})
18
+ error_messages = options[:object].errors[method]
19
+ clearfix_tag = error_messages.empty? ? 'clearfix' : 'clearfix error'
20
+
21
+ content_tag(:div,
22
+ label(object_name, method) +
23
+ content_tag(:div,
24
+ password_field(object_name, method, options) +
25
+ inline_help_tag(error_messages),
26
+ class: 'input'),
27
+ class: clearfix_tag)
28
+ end
29
+
30
+ def inline_help_tag(messages)
31
+ messages.empty? ? '' : content_tag(:span, messages.join(','), class: 'help-inline')
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ class ActionView::Helpers::FormBuilder #:nodoc:
38
+ def bootstrap_text_field(method, options = {})
39
+ @template.bootstrap_text_field(@object_name, method, objectify_options(options))
40
+ end
41
+
42
+ def bootstrap_password_field(method, options = {})
43
+ @template.bootstrap_password_field(@object_name, method, objectify_options(options))
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ module Bootstrap
2
+ module Form
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+
3
+ class FormHelperTest < ActionView::TestCase
4
+ def test_bootstrap_text_field
5
+ object = mock
6
+ errors = { :name => [] }
7
+ stub(object).errors { errors }
8
+ stub(object).name { 'Object Name' }
9
+ options = { :object => object }
10
+
11
+ expected_code = %{<div class="clearfix"><label for="post_name">Name</label><div class="input"><input id="post_name" name="post[name]" size="30" type="text" value="Object Name" /></div></div>}
12
+ assert_equal(expected_code, bootstrap_text_field(:post, :name, options))
13
+ end
14
+
15
+ def test_bootstrap_text_field_with_errors
16
+ object = mock
17
+ errors = {:name => ["can't be blank"]}
18
+ stub(object).errors { errors }
19
+ stub(object).name { 'Object Name' }
20
+ options = { :object => object }
21
+
22
+ expected_code = %{<div class="clearfix error"><label for="post_name">Name</label><div class="input"><input id="post_name" name="post[name]" size="30" type="text" value="Object Name" /><span class="help-inline">can't be blank</span></div></div>}
23
+ assert_equal(expected_code, bootstrap_text_field(:post, :name, options))
24
+ end
25
+
26
+ def test_bootstrap_password_field
27
+ object = mock
28
+ errors = { :name => [] }
29
+ stub(object).errors { errors }
30
+ stub(object).name { 'Object Name' }
31
+ options = { :object => object }
32
+
33
+ expected_code = %{<div class="clearfix"><label for="post_name">Name</label><div class="input"><input id="post_name" name="post[name]" size="30" type="password" /></div></div>}
34
+ assert_equal(expected_code, bootstrap_password_field(:post, :name, options))
35
+ end
36
+
37
+ def test_bootstrap_password_field_with_errors
38
+ object = mock
39
+ errors = {:name => ["can't be blank"]}
40
+ stub(object).errors { errors }
41
+ stub(object).name { 'Object Name' }
42
+ options = { :object => object }
43
+
44
+ expected_code = %{<div class="clearfix error"><label for="post_name">Name</label><div class="input"><input id="post_name" name="post[name]" size="30" type="password" /><span class="help-inline">can't be blank</span></div></div>}
45
+ assert_equal(expected_code, bootstrap_password_field(:post, :name, options))
46
+ end
47
+ end
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+
7
+ ENV["RAILS_ENV"] = "test"
8
+
9
+ require 'minitest/autorun'
10
+ require 'rails'
11
+ require 'action_controller'
12
+ require 'action_view'
13
+ require 'rails/test_help'
14
+ require 'bootstrap-form'
15
+ require 'rr'
16
+
17
+ module BootstrapForm
18
+ class Application < Rails::Application ; end
19
+ end
20
+
21
+ BootstrapForm::Application.routes.draw do
22
+ match '/:controller(/:action(/:id))'
23
+ end
24
+
25
+ ActionController::Base.send :include, BootstrapForm::Application.routes.url_helpers
26
+
27
+ class Test::Unit::TestCase
28
+ include RR::Adapters::TestUnit
29
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bootstrap-form
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Padilla
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70143631856780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70143631856780
25
+ - !ruby/object:Gem::Dependency
26
+ name: minitest
27
+ requirement: &70143631856360 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70143631856360
36
+ - !ruby/object:Gem::Dependency
37
+ name: rr
38
+ requirement: &70143631855900 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70143631855900
47
+ description: Twitter Bootstrap Form helpers
48
+ email:
49
+ - david@crowdint.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - README.md
57
+ - Rakefile
58
+ - bootsrap-form.gemspec
59
+ - lib/bootstrap-form.rb
60
+ - lib/bootstrap-form/form_helper.rb
61
+ - lib/bootstrap-form/version.rb
62
+ - test/lib/action_view/helpers/form_helper_test.rb
63
+ - test/test_helper.rb
64
+ homepage: ''
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project: bootstrap-form
84
+ rubygems_version: 1.8.6
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Twitter Bootstrap Form helpers
88
+ test_files:
89
+ - test/lib/action_view/helpers/form_helper_test.rb
90
+ - test/test_helper.rb