Class: Tennpipes::Helpers::FormBuilder::AbstractFormBuilder
- Inherits:
-
Object
- Object
- Tennpipes::Helpers::FormBuilder::AbstractFormBuilder
- Defined in:
- lib/tennpipes-helper/form_builder/abstract_form_builder.rb
Overview
Base class for Tennpipes Form Builder
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes_name ⇒ Object
readonly
Returns the value of attribute attributes_name.
-
#is_nested ⇒ Object
readonly
Returns the value of attribute is_nested.
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
-
#multipart ⇒ Object
Returns the value of attribute multipart.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#nested_index ⇒ Object
readonly
Returns the value of attribute nested_index.
-
#object ⇒ Object
Returns the value of attribute object.
-
#parent_form ⇒ Object
readonly
Returns the value of attribute parent_form.
-
#template ⇒ Object
Returns the value of attribute template.
Instance Method Summary collapse
- #check_box(field, options = {}) ⇒ Object
- #check_box_group(field, options = {}) ⇒ Object
- #csrf_token_field ⇒ Object
- #email_field(field, options = {}) ⇒ Object
- #error_message_on(field, options = {}) ⇒ Object
- #error_messages(*params) ⇒ Object
-
#fields_for(child_association, collection = nil, options = {}, &block) ⇒ Object
Supports nested fields for a child model within a form.
- #file_field(field, options = {}) ⇒ Object
- #hidden_field(field, options = {}) ⇒ Object
- #image_submit(source, options = {}) ⇒ Object
-
#initialize(template, object, options = {}) ⇒ AbstractFormBuilder
constructor
A new instance of AbstractFormBuilder.
- #label(field, options = {}, &block) ⇒ Object
- #number_field(field, options = {}) ⇒ Object
- #password_field(field, options = {}) ⇒ Object
- #radio_button(field, options = {}) ⇒ Object
- #radio_button_group(field, options = {}) ⇒ Object
- #search_field(field, options = {}) ⇒ Object
- #select(field, options = {}) ⇒ Object
- #submit(*args) ⇒ Object
- #telephone_field(field, options = {}) ⇒ Object (also: #phone_field)
- #text_area(field, options = {}) ⇒ Object
- #text_field(field, options = {}) ⇒ Object
- #url_field(field, options = {}) ⇒ Object
Constructor Details
#initialize(template, object, options = {}) ⇒ AbstractFormBuilder
Returns a new instance of AbstractFormBuilder.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 9 def initialize(template, object, ={}) @template = template fail "FormBuilder template must be initialized" unless template @object = object.kind_of?(Symbol) ? build_object(object) : object fail "FormBuilder object must be present. If there's no object, use a symbol instead (i.e. :user)" unless object @options = @namespace = [:namespace] @model_name = [:as] || @object.class.to_s.underscore.tr('/', '_') nested = [:nested] if @is_nested = nested && (nested_parent = nested[:parent]) && nested_parent.respond_to?(:object) @parent_form = nested_parent @nested_index = nested[:index] @attributes_name = "#{nested[:association]}_attributes" end end |
Instance Attribute Details
#attributes_name ⇒ Object (readonly)
Returns the value of attribute attributes_name.
7 8 9 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 7 def attributes_name @attributes_name end |
#is_nested ⇒ Object (readonly)
Returns the value of attribute is_nested.
7 8 9 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 7 def is_nested @is_nested end |
#model_name ⇒ Object (readonly)
Returns the value of attribute model_name.
7 8 9 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 7 def model_name @model_name end |
#multipart ⇒ Object
Returns the value of attribute multipart.
6 7 8 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 6 def multipart @multipart end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
7 8 9 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 7 def namespace @namespace end |
#nested_index ⇒ Object (readonly)
Returns the value of attribute nested_index.
7 8 9 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 7 def nested_index @nested_index end |
#object ⇒ Object
Returns the value of attribute object.
6 7 8 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 6 def object @object end |
#parent_form ⇒ Object (readonly)
Returns the value of attribute parent_form.
7 8 9 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 7 def parent_form @parent_form end |
#template ⇒ Object
Returns the value of attribute template.
6 7 8 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 6 def template @template end |
Instance Method Details
#check_box(field, options = {}) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 92 def check_box(field, ={}) = (field, , :value => '1') [:checked] = true if is_checked?(field, ) name = field_name(field) html = @template.hidden_field_tag(name, :value => .delete(:uncheck_value) || '0') html << @template.check_box_tag(name, ) end |
#check_box_group(field, options = {}) ⇒ Object
80 81 82 83 84 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 80 def check_box_group(field, ={}) labeled_group(field, ) do |attributes| @template.check_box_tag(field_name(field)+'[]', attributes) end end |
#csrf_token_field ⇒ Object
138 139 140 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 138 def csrf_token_field @template.csrf_token_field end |
#email_field(field, options = {}) ⇒ Object
56 57 58 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 56 def email_field(field, ={}) @template.email_field_tag field_name(field), (field, ) end |
#error_message_on(field, options = {}) ⇒ Object
29 30 31 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 29 def (field, ={}) @template. object, field, end |
#error_messages(*params) ⇒ Object
25 26 27 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 25 def (*params) @template. object, *params end |
#fields_for(child_association, collection = nil, options = {}, &block) ⇒ Object
Supports nested fields for a child model within a form. f.fields_for :addresses f.fields_for :addresses, address f.fields_for :addresses, @addresses f.fields_for :addresses, address, index: i
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 126 def fields_for(child_association, collection=nil, ={}, &block) default_collection = self.object.send(child_association) collection ||= default_collection include_index = default_collection.respond_to?(:each) = { :parent => self, :association => child_association } Array(collection).each_with_index.inject(ActiveSupport::SafeBuffer.new) do |all,(child_instance,index)| [:index] = [:index] || (include_index ? index : nil) all << @template.fields_for(child_instance, { :nested => , :builder => self.class }, &block) << "\n" end end |
#file_field(field, options = {}) ⇒ Object
107 108 109 110 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 107 def file_field(field, ={}) self.multipart = true @template.file_field_tag field_name(field), (field, ).except(:value) end |
#hidden_field(field, options = {}) ⇒ Object
39 40 41 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 39 def hidden_field(field, ={}) @template.hidden_field_tag field_name(field), (field, ) end |
#image_submit(source, options = {}) ⇒ Object
116 117 118 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 116 def image_submit(source, ={}) @template.image_submit_tag source, end |
#label(field, options = {}, &block) ⇒ Object
33 34 35 36 37 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 33 def label(field, ={}, &block) [:id] ||= nil [:caption] ||= I18n.t("#{model_name}.attributes.#{field}", :count => 1, :default => field.to_s.humanize, :scope => :models) + ': ' @template.label_tag(field_id(field), (field, ), &block) end |
#number_field(field, options = {}) ⇒ Object
47 48 49 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 47 def number_field(field, ={}) @template.number_field_tag field_name(field), (field, ) end |
#password_field(field, options = {}) ⇒ Object
72 73 74 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 72 def password_field(field, ={}) @template.password_field_tag field_name(field), (field, ) end |
#radio_button(field, options = {}) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 100 def (field, ={}) = (field, ) [:checked] = true if is_checked?(field, ) [:id] = field_id(field, [:value]) @template. field_name(field), end |
#radio_button_group(field, options = {}) ⇒ Object
86 87 88 89 90 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 86 def (field, ={}) labeled_group(field, ) do |attributes| @template.(field_name(field), attributes) end end |
#search_field(field, options = {}) ⇒ Object
60 61 62 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 60 def search_field(field, ={}) @template.search_field_tag field_name(field), (field, ) end |
#select(field, options = {}) ⇒ Object
76 77 78 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 76 def select(field, ={}) @template.select_tag field_name(field), (field, ) end |
#submit(*args) ⇒ Object
112 113 114 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 112 def submit(*args) @template.submit_tag *args end |
#telephone_field(field, options = {}) ⇒ Object Also known as: phone_field
51 52 53 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 51 def telephone_field(field, ={}) @template.telephone_field_tag field_name(field), (field, ) end |
#text_area(field, options = {}) ⇒ Object
68 69 70 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 68 def text_area(field, ={}) @template.text_area_tag field_name(field), (field, ) end |
#text_field(field, options = {}) ⇒ Object
43 44 45 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 43 def text_field(field, ={}) @template.text_field_tag field_name(field), (field, ) end |
#url_field(field, options = {}) ⇒ Object
64 65 66 |
# File 'lib/tennpipes-helper/form_builder/abstract_form_builder.rb', line 64 def url_field(field, ={}) @template.url_field_tag field_name(field), (field, ) end |