Class: NForm::Builder
Constant Summary
Constants included from HTML
HTML::BOOL_ATTRIBUTES, HTML::VOID_ELEMENTS
Instance Attribute Summary collapse
-
#form_class ⇒ Object
readonly
Returns the value of attribute form_class.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Instance Method Summary collapse
- #action ⇒ Object
- #association_select(association, key_method: :id, label_method: :name, label: nil, **args) ⇒ Object
- #base_error(e) ⇒ Object
- #base_errors ⇒ Object
- #bool_field(k, label: nil, **args) ⇒ Object
- #collection_name ⇒ Object
- #date_attrs(k, time, ph, min, max, val) ⇒ Object
- #date_input(k, label: nil, start_year: nil, end_year: nil, default: {}) ⇒ Object
- #error_for(k) ⇒ Object
- #errors ⇒ Object
- #form_id ⇒ Object
- #hidden_field(k, **args) ⇒ Object
- #http_method ⇒ Object
-
#initialize(object, id: nil, form_class: nil, action: nil, method: nil) ⇒ Builder
constructor
A new instance of Builder.
- #input_for(k, type: "text", default: nil, **args) ⇒ Object
-
#label_for(k, label: nil) ⇒ Object
allow “label: false” to prevent label being generated so that function that call label_for can all consistently omit the label when label value is given as false.
- #method_tag ⇒ Object
- #new_object? ⇒ Boolean
- #number_field(k, label: nil, default: nil, **args) ⇒ Object
- #object_name ⇒ Object
- #option_for(k, value, text) ⇒ Object
- #param(*args) ⇒ Object
- #password_field(k, label: nil, **args) ⇒ Object
- #render ⇒ Object
- #select(k, options:, label: nil, blank: true, **args) ⇒ Object
- #submit_button(**args) ⇒ Object
- #text_area(k, label: nil, default: nil, **args) ⇒ Object
- #text_field(k, label: nil, default: nil, **args) ⇒ Object
- #title ⇒ Object
Methods included from HTML
#attr_key, #attr_string, #attrs, #njoin, #sjoin, #tag, #zjoin
Constructor Details
#initialize(object, id: nil, form_class: nil, action: nil, method: nil) ⇒ Builder
Returns a new instance of Builder.
9 10 11 12 13 14 15 |
# File 'lib/nform/builder.rb', line 9 def initialize(object, id: nil, form_class: nil, action: nil, method: nil) @object = object @form_id = id @form_class = form_class @action = action @http_method = method end |
Instance Attribute Details
#form_class ⇒ Object (readonly)
Returns the value of attribute form_class.
8 9 10 |
# File 'lib/nform/builder.rb', line 8 def form_class @form_class end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
8 9 10 |
# File 'lib/nform/builder.rb', line 8 def object @object end |
Instance Method Details
#action ⇒ Object
21 22 23 24 |
# File 'lib/nform/builder.rb', line 21 def action @action or "/#{collection_name.dasherize}" + (new_object? ? "" : "/#{object.id}") end |
#association_select(association, key_method: :id, label_method: :name, label: nil, **args) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/nform/builder.rb', line 153 def association_select(association,key_method: :id, label_method: :name, label: nil, **args) aname = detect_object_name(association) key = (aname.underscore + "_id").to_sym = Hash[ association.map{|i| [i.send(key_method), i.send(label_method)]}] unless label == false label ||= aname.underscore.titleize end select(key,options: , label: label, **args) end |
#base_error(e) ⇒ Object
66 67 68 |
# File 'lib/nform/builder.rb', line 66 def base_error(e) tag(:li){ e } end |
#base_errors ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/nform/builder.rb', line 54 def base_errors if e = errors[:base] tag(:ul, class: 'base errors') do if e.respond_to?(:map) zjoin e.map{|m| base_error(m) } else base_error(e) end end end end |
#bool_field(k, label: nil, **args) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/nform/builder.rb', line 127 def bool_field(k, label: nil,**args) checked = ( !object.send(k) || object.send(k) == "false" ) ? false : true zjoin label_for(k, label: label), tag(:input, type:'hidden',name:param(k), value:"false"), tag(:input, type:'checkbox', id: k.to_s.dasherize, name:param(k), value:"true", checked:checked,**args), error_for(k) end |
#collection_name ⇒ Object
38 39 40 |
# File 'lib/nform/builder.rb', line 38 def collection_name object_name.pluralize end |
#date_attrs(k, time, ph, min, max, val) ⇒ Object
178 179 180 181 |
# File 'lib/nform/builder.rb', line 178 def date_attrs(k,time,ph,min,max,val) { class: "date-#{time}", type: "number", name: param(k,time), placeholder: ph, min: min, max: max, step: 1, value: val } end |
#date_input(k, label: nil, start_year: nil, end_year: nil, default: {}) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/nform/builder.rb', line 165 def date_input(k, label: nil, start_year: nil, end_year: nil, default:{}) start_year ||= Date.today.year end_year ||= start_year+20 val = get_value(object,k) tag :div, class: "date-input" do zjoin label_for(nil,label:(label||k.to_s.titleize)), tag(:input, date_attrs(k,:month,"MM",01,12,get_value(val,:month, default[:month]))), tag(:input, date_attrs(k,:day,"DD",01,31,get_value(val,:day, default[:day]))), tag(:input, date_attrs(k,:year,"YYYY",start_year,end_year,get_value(val,:year, default[:year]))), error_for(k) end end |
#error_for(k) ⇒ Object
97 98 99 |
# File 'lib/nform/builder.rb', line 97 def error_for(k) tag(:span, class: 'error'){ errors[k] } if errors[k] end |
#errors ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/nform/builder.rb', line 46 def errors @errors = if object.respond_to?(:errors) object.errors else {} end end |
#form_id ⇒ Object
17 18 19 |
# File 'lib/nform/builder.rb', line 17 def form_id @form_id || object_name.dasherize end |
#hidden_field(k, **args) ⇒ Object
114 115 116 |
# File 'lib/nform/builder.rb', line 114 def hidden_field(k,**args) input_for(k, type: "hidden",**args) end |
#http_method ⇒ Object
26 27 28 |
# File 'lib/nform/builder.rb', line 26 def http_method @http_method || (new_object? ? "POST" : "PATCH") end |
#input_for(k, type: "text", default: nil, **args) ⇒ Object
91 92 93 94 95 |
# File 'lib/nform/builder.rb', line 91 def input_for(k, type: "text", default: nil, **args) val = object.send(k) || default opts = {type:type, id:k.to_s.dasherize, name:param(k), value:val}.merge(args) tag(:input,opts) end |
#label_for(k, label: nil) ⇒ Object
allow “label: false” to prevent label being generated so that function that call label_for can all consistently omit the label when label value is given as false
87 88 89 |
# File 'lib/nform/builder.rb', line 87 def label_for(k, label: nil) tag(:label, for: k.to_s.dasherize){ label || k.to_s.titleize } unless label == false end |
#method_tag ⇒ Object
42 43 44 |
# File 'lib/nform/builder.rb', line 42 def method_tag tag(:input, type:"hidden", name:"_method", value:http_method) if http_method != "POST" end |
#new_object? ⇒ Boolean
30 31 32 |
# File 'lib/nform/builder.rb', line 30 def new_object? object.respond_to?(:new?) ? object.new? : true end |
#number_field(k, label: nil, default: nil, **args) ⇒ Object
105 106 107 108 |
# File 'lib/nform/builder.rb', line 105 def number_field(k, label: nil, default: nil, **args) opts = {type:'number', pattern: '\d*'}.merge(args) zjoin label_for(k, label:label), input_for(k,type:'number',default:default,**opts), error_for(k) end |
#object_name ⇒ Object
34 35 36 |
# File 'lib/nform/builder.rb', line 34 def object_name @object_name || detect_object_name(object).underscore end |
#option_for(k, value, text) ⇒ Object
147 148 149 150 151 |
# File 'lib/nform/builder.rb', line 147 def option_for(k,value,text) opts = {value: value} opts[:selected] = true if object.send(k) == value tag(:option, opts){text ? text : value} end |
#param(*args) ⇒ Object
81 82 83 |
# File 'lib/nform/builder.rb', line 81 def param(*args) object_name + args.map{|a|"[#{a}]"}.join end |
#password_field(k, label: nil, **args) ⇒ Object
110 111 112 |
# File 'lib/nform/builder.rb', line 110 def password_field(k, label: nil, **args) zjoin label_for(k, label:label), input_for(k,type:"password",**args), error_for(k) end |
#render ⇒ Object
70 71 72 73 74 75 |
# File 'lib/nform/builder.rb', line 70 def render tag(:form, id: form_id, class: form_class, action:action, method:"POST") do body = yield(self) if block_given? zjoin(method_tag,base_errors,body) end end |
#select(k, options:, label: nil, blank: true, **args) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/nform/builder.rb', line 135 def select(k, options:, label: nil, blank: true,**args) opts = .map{|value,text| option_for(k,value,text) } opts.unshift option_for(k,nil,nil) if blank zjoin( label_for(k, label: label), tag(:select, id:k.to_s.dasherize, name:param(k), **args){ zjoin opts }, error_for(k) ) end |
#submit_button(**args) ⇒ Object
183 184 185 186 |
# File 'lib/nform/builder.rb', line 183 def (**args) text = args.delete(:text) tag(:button,**args){ text || (new_object? ? "Create" : "Save") } end |
#text_area(k, label: nil, default: nil, **args) ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/nform/builder.rb', line 118 def text_area(k, label: nil, default: nil,**args) val = object.send(k) || default zjoin( label_for(k, label:label), tag(:textarea, id:k.to_s.dasherize, name:param(k),**args){ "#{val}" if val }, error_for(k) ) end |
#text_field(k, label: nil, default: nil, **args) ⇒ Object
101 102 103 |
# File 'lib/nform/builder.rb', line 101 def text_field(k, label: nil, default: nil, **args) zjoin label_for(k, label:label), input_for(k,default:default,**args), error_for(k) end |
#title ⇒ Object
77 78 79 |
# File 'lib/nform/builder.rb', line 77 def title sjoin (new_object? ? "Create" : "Edit"), object_name.titleize end |