Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from claudiob/add-form-for
Add form for
- Loading branch information
Showing
38 changed files
with
1,089 additions
and
30 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 |
---|---|---|
|
@@ -19,4 +19,5 @@ tmp | |
*.so | ||
*.o | ||
*.a | ||
spec/dummy | ||
mkmf.log |
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
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
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
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
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
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,41 @@ | ||
require 'bh/helpers/form/check_box_helper' | ||
require 'bh/helpers/form/field_helper' | ||
require 'bh/helpers/form/fieldset_helper' | ||
require 'bh/helpers/form/fields_for_helper' | ||
require 'bh/helpers/form/legend_helper' | ||
require 'bh/helpers/form/radio_button_helper' | ||
require 'bh/helpers/form/select_helper' | ||
require 'bh/helpers/form/static_control_helper' | ||
require 'bh/helpers/form/submit_helper' | ||
|
||
module Bh | ||
class FormBuilder < ActionView::Helpers::FormBuilder | ||
include Form::CheckBoxHelper | ||
include Form::FieldHelper | ||
include Form::FieldsetHelper | ||
include Form::FieldsForHelper | ||
include Form::LegendHelper | ||
include Form::RadioButtonHelper | ||
include Form::SelectHelper | ||
include Form::StaticControlHelper | ||
include Form::SubmitHelper | ||
|
||
# @note: field_helpers are returned as symbols in ActionView 4 and as | ||
# strings in ActionView 3 | ||
def self.textual_field_helpers | ||
non_textual_field_helpers = %w(label hidden_field range_field check_box | ||
radio_button select submit fields_for label) | ||
field_helpers.map(&:to_s) - non_textual_field_helpers | ||
end | ||
|
||
# Use the same template for all the textual field helpers such as | ||
# email_field, password_field, etc. | ||
# Exclude the ones that should not have additional styles. | ||
# Do not show error icons on number_field not to cover the sliders. | ||
textual_field_helpers.each do |field_type| | ||
define_method field_type do |method, options = {}| | ||
field(method, field_type, options) { super method, options } | ||
end | ||
end | ||
end | ||
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
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,17 @@ | ||
require 'action_view' | ||
|
||
module Bh | ||
module BaseHelper | ||
include ActionView::Helpers::TagHelper # for content_tag | ||
include ActionView::Context # for capture | ||
include ActionView::Helpers::OutputSafetyHelper # for safe_join | ||
include ActionView::Helpers::RenderingHelper # for render | ||
|
||
private | ||
|
||
def append_class!(hash, new_class) | ||
existing_class = hash[:class] | ||
hash[:class] = [existing_class, new_class].compact.join ' ' | ||
end | ||
end | ||
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,112 @@ | ||
require 'bh/helpers/glyphicon_helper' | ||
|
||
module Bh | ||
module Form | ||
module BaseHelper | ||
include GlyphiconHelper # for glyphicon | ||
|
||
private | ||
|
||
def base_field(method, field_type, options = {}, &block) | ||
errors = (object.errors.get method if object) || {} | ||
label = label_for method, errors, options | ||
field = field_container(options) do | ||
field_tags errors, field_type, &block | ||
end | ||
label_and_field = safe_join [label, field].compact | ||
label_and_field_container label_and_field, field_type, errors | ||
end | ||
|
||
|
||
def field_container(options = {}, &block) | ||
if horizontal_form? | ||
klass = [('col-sm-offset-3' if options[:offset]), 'col-sm-9'] | ||
content_tag :div, class: klass.compact.join(' '), &block | ||
else | ||
yield | ||
end | ||
end | ||
|
||
def field_tags(errors, field_type, &block) | ||
tags = [@template.capture(&block)] | ||
tags << error_icon_tag if errors.any? && show_error_icon?(field_type) | ||
tags << error_help_tag(errors) if errors.any? && show_error_help? | ||
safe_join tags | ||
end | ||
|
||
def label_and_field_container(label_and_field, field_type, errors = {}) | ||
klass = ['form-group'] | ||
if errors.any? | ||
klass << 'has-error' | ||
klass << 'has-feedback' if show_error_icon?(field_type) | ||
end | ||
content_tag :div, label_and_field, class: klass.join(' ') | ||
end | ||
|
||
|
||
def show_error_icon?(field_type) | ||
hide = [:checkbox, :number_field, :radio_button, :select, :legend].include? field_type | ||
@options.fetch(:errors, {}).fetch(:icons, true) && !hide | ||
end | ||
|
||
|
||
def error_icon_tag | ||
glyphicon :remove, class: 'form-control-feedback' | ||
end | ||
|
||
def error_help_tag(errors) | ||
klass = ['help-block', 'text-left'] | ||
klass << 'sr-only' if inline_form? | ||
content_tag :span, errors.to_sentence, class: klass.join(' ') | ||
end | ||
|
||
def label_for(method, errors, options) | ||
if options.delete(:use_label) { true } | ||
args = [method, options.delete(:label), label_options(errors)] | ||
label *args.compact | ||
end | ||
end | ||
|
||
def label_options(errors = {}) | ||
klass = [] | ||
klass << 'sr-only' if inline_form? | ||
klass << 'col-sm-3' if horizontal_form? | ||
klass << 'control-label' if horizontal_form? | ||
klass << 'control-label' if basic_form? && errors.any? | ||
{class: klass.join(' ')} if klass.any? | ||
end | ||
|
||
# Rails adds <div class='field_with_errors'> which messes up | ||
# Bootstrap inline form unless the label is inserted within | ||
# the div itself. | ||
def label_and_field(container_class, method, options = {}, &block) | ||
label_and_field = @template.capture(&block) | ||
label = options.delete(:label) | ||
if index = label_and_field =~ %r{</div>$} | ||
label_and_field.insert index, " #{label}" | ||
else | ||
label_and_field.concat " #{label}" | ||
end | ||
content_tag :div, class: container_class do | ||
content_tag :label, label_and_field | ||
end | ||
end | ||
|
||
def show_error_help? | ||
@options.fetch(:errors, {}).fetch(:messages, :inline).to_s == 'inline' | ||
end | ||
|
||
def inline_form? | ||
@options[:layout].to_s == 'inline' | ||
end | ||
|
||
def horizontal_form? | ||
@options[:layout].to_s == 'horizontal' | ||
end | ||
|
||
def basic_form? | ||
!inline_form? && !horizontal_form? | ||
end | ||
end | ||
end | ||
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,35 @@ | ||
require 'bh/helpers/form/base_helper' | ||
|
||
module Bh | ||
module Form | ||
module CheckBoxHelper | ||
include BaseHelper | ||
|
||
def check_box(method, options = {}, checked_value = '1', unchecked_value = '0') | ||
block = -> { super method, options, checked_value, unchecked_value } | ||
if options.delete(:inline_label) { true } | ||
check_box_with_inline_label method, options, &block | ||
else | ||
check_box_with_block_label method, options, &block | ||
end | ||
end | ||
|
||
private | ||
|
||
def check_box_with_block_label(method, options = {}, &block) | ||
append_class! options, 'form-control' unless inline_form? | ||
append_class! options, 'checkbox' if horizontal_form? | ||
options[:label] ||= method.to_s.humanize | ||
base_field method, :checkbox, options, &block | ||
end | ||
|
||
def check_box_with_inline_label(method, options = {}, &block) | ||
options.merge! offset: true, use_label: false | ||
options[:label] ||= method.to_s.humanize | ||
base_field method, :checkbox, options do | ||
label_and_field 'checkbox', method, options, &block | ||
end | ||
end | ||
end | ||
end | ||
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,15 @@ | ||
require 'bh/helpers/form/base_helper' | ||
|
||
module Bh | ||
module Form | ||
module FieldHelper | ||
include BaseHelper | ||
|
||
def field(method, field_type, options = {}, &block) | ||
options[:placeholder] ||= method.to_s.humanize | ||
append_class! options, 'form-control' | ||
base_field method, field_type, options, &block | ||
end | ||
end | ||
end | ||
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,17 @@ | ||
require 'bh/helpers/form/fieldset_helper' | ||
|
||
module Bh | ||
module Form | ||
module FieldsForHelper | ||
include BaseHelper | ||
include FieldsetHelper # for fieldset | ||
|
||
def fields_for(record_object = nil, fields_options = {}, &block) | ||
fields_options[:layout] ||= @options[:layout] | ||
fields_options[:errors] ||= @options[:errors] | ||
title = fields_options.delete(:title) { record_object.to_s.humanize } | ||
fieldset(title) { super record_object, fields_options, &block } | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.