forked from sethvargo/bootstrap_forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6332260
Showing
9 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.gem | ||
.bundle | ||
Gemfile.lock | ||
pkg/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source "http://rubygems.org" | ||
|
||
# Specify your gem's dependencies in bootstrap-forms.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require "bundler/gem_tasks" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# -*- encoding: utf-8 -*- | ||
$:.push File.expand_path("../lib", __FILE__) | ||
|
||
Gem::Specification.new do |s| | ||
s.name = "bootstrap-forms" | ||
s.version = "0.0.1" | ||
s.author = "Seth Vargo" | ||
s.email = "[email protected]" | ||
s.homepage = "https://github.com/sethvargo/bootstrap_forms" | ||
s.summary = %q{Bootstrap Forms makes Twitter's Bootstrap on Rails easy!} | ||
s.description = %q{Bootstrap Forms makes Twitter's Bootstrap on Rails easy to use by creating helpful form builders that minimize markup in your views.} | ||
|
||
s.rubyforge_project = "bootstrap-forms" | ||
|
||
s.files = `git ls-files`.split("\n") | ||
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") | ||
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } | ||
s.require_paths = ["lib"] | ||
|
||
# specify any dependencies here; for example: | ||
# s.add_development_dependency "rspec" | ||
# s.add_runtime_dependency "rest-client" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module BootstrapForms | ||
# Your code goes here... | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Description: | ||
The bootstrap_forms generator copies the form builder into `app/form_builders` and the initializer in `config/initializers`. |
10 changes: 10 additions & 0 deletions
10
lib/generators/bootstrap_forms/install/install_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module BootstrapForms | ||
class InstallGenerator < Rails::Generators::Base | ||
source_root File.expand_path('../templates', __FILE__) | ||
|
||
def generate_insall | ||
copy_file 'bootstrap_form_builder.rb', 'app/form_builders/bootstrap_form_builder.rb' | ||
copy_file 'bootstrap_forms.rb', 'config/initializers/bootstrap_forms.rb' | ||
end | ||
end | ||
end |
143 changes: 143 additions & 0 deletions
143
lib/generators/bootstrap_forms/install/templates/bootstrap_form_builder.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
class BootstrapFormBuilder < ActionView::Helpers::FormBuilder | ||
delegate :content_tag, :hidden_field_tag, :check_box_tag, :radio_button_tag, :link_to, :to => :@template | ||
|
||
%w(select check_box email_field file_field number_field password_field phone_field radio_button range_field search_field telephone_field text_area text_field url_field).each do |method_name| | ||
define_method(method_name) do |name, *args| | ||
@name = name | ||
@options = args.extract_options! | ||
@args = args | ||
|
||
clearfix_div do | ||
label_field + input_div do | ||
extras {super} | ||
end | ||
end | ||
end | ||
end | ||
|
||
def collection_check_boxes(attribute, records, record_id, record_name, *args) | ||
@name = attribute | ||
@options = args.extract_options! | ||
@args = args | ||
|
||
clearfix_div do | ||
label_field + input_div do | ||
extras do | ||
content_tag(:ul, :class => 'inputs-list') do | ||
records.collect do |record| | ||
element_id = "#{object_name}_#{attribute}_#{record.send(record_id)}" | ||
checkbox = check_box_tag("#{object_name}[#{attribute}][]", record.send(record_id), object.send(attribute).include?(record.send(record_id)), :id => element_id) | ||
|
||
content_tag(:li) do | ||
content_tag(:label) do | ||
checkbox + content_tag(:span, record.send(record_name)) | ||
end | ||
end | ||
end.join('').html_safe | ||
end | ||
end | ||
end + hidden_field_tag("#{object_name}[#{attribute}][]") | ||
end | ||
end | ||
|
||
def collection_radio_buttons(attribute, records, record_id, record_name, *args) | ||
@name = attribute | ||
@options = args.extract_options! | ||
@args = args | ||
|
||
clearfix_div do | ||
label_field + input_div do | ||
extras do | ||
content_tag(:ul, :class => 'inputs-list') do | ||
records.collect do |record| | ||
element_id = "#{object_name}_#{attribute}_#{record.send(record_id)}" | ||
radiobutton = radio_button_tag("#{object_name}[#{attribute}][]", record.send(record_id), object.send(attribute) == record.send(record_id), :id => element_id) | ||
|
||
content_tag(:li) do | ||
content_tag(:label) do | ||
radiobutton + content_tag(:span, record.send(record_name)) | ||
end | ||
end | ||
end.join('').html_safe | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
def uneditable_field(name, *args) | ||
@name = name | ||
@options = args.extract_options! | ||
@args = args | ||
|
||
clearfix_div do | ||
label_field + input_div do | ||
extras do | ||
content_tag(:span, :class => 'uneditable-input') do | ||
@options[:value] || object.send(@name.to_sym) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
def submit(name = nil, *args) | ||
@name = name | ||
@options = args.extract_options! | ||
@args = args | ||
|
||
@options[:class] = 'btn primary' | ||
|
||
content_tag(:div, :class => 'actions') do | ||
super(name, *args << @options) + | ||
link_to('Back', :back, :class => 'btn') | ||
end | ||
end | ||
|
||
private | ||
def clearfix_div(&block) | ||
klasses = ['clearfix'] | ||
klasses << @options[:class] if @options[:class] | ||
klasses << 'error' if @options[:error] | ||
klasses << 'success' if @options[:success] | ||
klasses << 'warning' if @options[:warning] | ||
klass = klasses.join(' ') | ||
|
||
content_tag(:div, :class => klass, &block) | ||
end | ||
|
||
def input_div(&block) | ||
content_tag(:div, :class => 'input') do | ||
if @options[:append] || @options[:prepend] | ||
klass = 'input-prepend' if @options[:prepend] | ||
klass = 'input-append' if @options[:append] | ||
content_tag(:div, :class => klass, &block) | ||
else | ||
yield if block_given? | ||
end | ||
end | ||
end | ||
|
||
def label_field(&block) | ||
required = object.class.validators_on(@name).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator } | ||
label(@name, block_given? ? block : @options[:label], :class => ('required' if required)) | ||
end | ||
|
||
%w(help_inline error success warning help_block append prepend).each do |method_name| | ||
define_method(method_name) do |*args| | ||
return '' unless value = @options[method_name.to_sym] | ||
klass = 'help-inline' | ||
klass = 'help-block' if method_name == 'help_block' | ||
klass = 'add-on' if method_name == 'append' || method_name == 'prepend' | ||
content_tag(:span, value, :class => klass) | ||
end | ||
end | ||
|
||
def extras(&block) | ||
[prepend, (yield if block_given?), append, help_inline, error, success, warning, help_block].join('').html_safe | ||
end | ||
|
||
def objectify_options(options) | ||
super.except(:label, :help_inline, :error, :success, :warning, :help_block, :prepend, :append) | ||
end | ||
end |
18 changes: 18 additions & 0 deletions
18
lib/generators/bootstrap_forms/install/templates/bootstrap_forms.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module ActionView | ||
module Helpers | ||
module FormHelper | ||
def form_for_with_bootstrap(record, options = {}, &proc) | ||
options[:builder] = BootstrapFormBuilder | ||
form_for_without_bootstrap(record, options, &proc) | ||
end | ||
|
||
def fields_for_with_bootstrap(record_name, record_object = nil, options = {}, &block) | ||
options[:builder] = BootstrapFormBuilder | ||
fields_for_without_bootstrap(record_name, record_object, options, &block) | ||
end | ||
|
||
alias_method_chain :form_for, :bootstrap | ||
alias_method_chain :fields_for, :bootstrap | ||
end | ||
end | ||
end |