Class: Dao::Form
- Inherits:
-
Object
- Object
- Dao::Form
- Defined in:
- lib/dao/form.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Builder
Instance Attribute Summary collapse
-
#object ⇒ Object
instance methods.
Class Method Summary collapse
- .for(*args, &block) ⇒ Object
- .key_for(*keys) ⇒ Object
- .name_for(name, *keys) ⇒ Object
- .prefix_for(name) ⇒ Object
Instance Method Summary collapse
- #attr_for(string) ⇒ Object
- #button(*args, &block) ⇒ Object
- #checkbox(*args, &block) ⇒ Object
- #class_for(keys, klass = nil) ⇒ Object
- #data_attr_for(string) ⇒ Object
-
#element(which, *args, &block) ⇒ Object
html generation methods.
- #error_for(keys, klass = nil) ⇒ Object
- #errors_on(keys) ⇒ Object
- #errors_on?(*keys) ⇒ Boolean
- #form(*args, &block) ⇒ Object
- #hidden(*args, &block) ⇒ Object
-
#id_for(keys) ⇒ Object
html generation support methods.
-
#initialize(*args) ⇒ Form
constructor
A new instance of Form.
- #input(*args, &block) ⇒ Object
- #key_for(*keys) ⇒ Object
- #label(*args, &block) ⇒ Object
- #name_for(*keys) ⇒ Object
- #options_for(*hashes) ⇒ Object
- #radio_button(*args, &block) ⇒ Object
- #reset(*args) ⇒ Object
- #select(*args, &block) ⇒ Object
- #slug_for(string) ⇒ Object
- #submit(*args, &block) ⇒ Object
- #textarea(*args, &block) ⇒ Object
- #titleize(string) ⇒ Object
- #upload(*args, &block) ⇒ Object
- #value_for(map, keys) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Form
Returns a new instance of Form.
43 44 45 |
# File 'lib/dao/form.rb', line 43 def initialize(*args) @object = args.shift end |
Instance Attribute Details
#object ⇒ Object
instance methods
41 42 43 |
# File 'lib/dao/form.rb', line 41 def object @object end |
Class Method Details
.for(*args, &block) ⇒ Object
34 35 36 |
# File 'lib/dao/form.rb', line 34 def for(*args, &block) new(*args, &block) end |
.key_for(*keys) ⇒ Object
528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'lib/dao/form.rb', line 528 def Form.key_for(*keys) keys.flatten.compact.map do |key| case when Integer === key key when key =~ /^\d+$/ "~#{ key }" else key end end.join('.') end |
.name_for(name, *keys) ⇒ Object
524 525 526 |
# File 'lib/dao/form.rb', line 524 def Form.name_for(name, *keys) "#{ prefix_for(name) }[#{ key_for(*keys) }]" end |
.prefix_for(name) ⇒ Object
520 521 522 |
# File 'lib/dao/form.rb', line 520 def Form.prefix_for(name) "dao[#{ name }]" end |
Instance Method Details
#attr_for(string) ⇒ Object
572 573 574 |
# File 'lib/dao/form.rb', line 572 def attr_for(string) slug_for(string).gsub(/_/, '-') end |
#button(*args, &block) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/dao/form.rb', line 213 def (*args, &block) = args.. keys = args.flatten type = .delete(:type) || :button name = .delete(:name) || name_for(keys) id = .delete(:id) || id_for(keys) klass = class_for(keys, .delete(:class)) error = error_for(keys, .delete(:error)) value = .has_key?(:value) ? .delete(:value) : value_for(attributes, keys) content = (block ? block.call : (.delete(:content) || 'Submit')) ((, :type => type, :name => name, :value => value, :class => klass, :id => id, :data_error => error)){ content } end |
#checkbox(*args, &block) ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/dao/form.rb', line 255 def checkbox(*args, &block) = args.. keys = args.flatten type = .delete(:type) || :checkbox name = .delete(:name) || name_for(keys) id = .delete(:id) || id_for(keys) klass = class_for(keys, .delete(:class)) error = error_for(keys, .delete(:error)) values = .delete(:values) || .delete(:checked) unless .has_key?(:checked) checked = Coerce.boolean(attributes.get(keys)) [:checked] = checked if checked end value_for = case values when false, nil {true => '1', false => '0'} when Hash h = {} values.map{|k, v| h[ k =~ /t|1|on|yes/ ? true : false ] = v} h else t, f, *ignored = Array(values).flatten.compact {true => t, false => f} end value_for[true] ||= '1' value_for[false] ||= '0' = .dup.tap{|o| o.delete(:checked)} tagz{ input_((, :type => :hidden, :name => name, :value => value_for[false])){} __ input_( ( , :type => :checkbox, :name => name, :value => value_for[true], :class => klass, :id => id, :data_error => error ) ){} } end |
#class_for(keys, klass = nil) ⇒ Object
488 489 490 491 492 493 494 495 496 |
# File 'lib/dao/form.rb', line 488 def class_for(keys, klass = nil) klass = if errors_on?(keys) [klass, 'dao', 'errors'].compact.join(' ') else [klass, 'dao'].compact.join(' ') end klass end |
#data_attr_for(string) ⇒ Object
576 577 578 |
# File 'lib/dao/form.rb', line 576 def data_attr_for(string) "data-#{ attr_for(string) }" end |
#element(which, *args, &block) ⇒ Object
html generation methods
138 139 140 |
# File 'lib/dao/form.rb', line 138 def element(which, *args, &block) send(which, *args, &block) end |
#error_for(keys, klass = nil) ⇒ Object
498 499 500 501 502 503 504 |
# File 'lib/dao/form.rb', line 498 def error_for(keys, klass = nil) if errors_on?(keys) title = Array(keys).join(' ').titleize = Array(errors.get(keys)).join(', ') "#{ title }: #{ }" end end |
#errors_on(keys) ⇒ Object
480 481 482 |
# File 'lib/dao/form.rb', line 480 def errors_on(keys) errors.get(keys) end |
#errors_on?(*keys) ⇒ Boolean
484 485 486 |
# File 'lib/dao/form.rb', line 484 def errors_on?(*keys) !errors_on(keys).blank? end |
#form(*args, &block) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/dao/form.rb', line 142 def form(*args, &block) = args.. keys = args.flatten action = .delete(:action) || './' method = .delete(:method) || 'post' id = .delete(:id) || id_for(keys) klass = class_for(keys, .delete(:class)) error = error_for(keys, .delete(:error)) content = if block.nil? and !.has_key?(:content) '' else block ? block.call(form=self) : .delete(:content) end form_((, :action => action, :method => method, :class => klass, :id => id, :data_error => error)){ content } end |
#hidden(*args, &block) ⇒ Object
308 309 310 311 312 313 |
# File 'lib/dao/form.rb', line 308 def hidden(*args, &block) = args.. [:type] = :hidden args.push() input(*args, &block) end |
#id_for(keys) ⇒ Object
html generation support methods
475 476 477 478 |
# File 'lib/dao/form.rb', line 475 def id_for(keys) id = [name, keys.join('-')].compact.join('_') slug_for(id) end |
#input(*args, &block) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/dao/form.rb', line 181 def input(*args, &block) = args.. keys = args.flatten type = .delete(:type) || :text name = .delete(:name) || name_for(keys) id = .delete(:id) || id_for(keys) klass = class_for(keys, .delete(:class)) error = error_for(keys, .delete(:error)) value = if block.nil? and !.has_key?(:value) value_for(attributes, keys) else block ? block.call(attributes.get(keys)) : .delete(:value) end input_((, :type => type, :name => name, :value => value, :class => klass, :id => id, :data_error => error)){} end |
#key_for(*keys) ⇒ Object
541 542 543 |
# File 'lib/dao/form.rb', line 541 def key_for(*keys) Form.key_for(name, *keys) end |
#label(*args, &block) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/dao/form.rb', line 162 def label(*args, &block) = args.. keys = args.flatten block ||= proc do .delete(:content) || .delete(:value) || keys.map{|key| key.to_s.titleize}.join(' ') end id = .delete(:id) || id_for(keys + [:label]) klass = class_for(keys, .delete(:class)) error = error_for(keys, .delete(:error)) target = .delete(:for) || id_for(keys) label_((, :for => target, :class => klass, :id => id, :data_error => error), &block) end |
#name_for(*keys) ⇒ Object
545 546 547 |
# File 'lib/dao/form.rb', line 545 def name_for(*keys) Form.name_for(name, *keys) end |
#options_for(*hashes) ⇒ Object
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 |
# File 'lib/dao/form.rb', line 549 def (*hashes) map = Map.new hashes.flatten.each do |h| case((data = h.delete(:data) || h.delete('data'))) when Hash data.each{|k,v| map[data_attr_for(k)] = v unless v.nil?} else h[:data] = data end h.each do |k,v| map[attr_for(k)] = v unless v.nil? end end %w( readonly disabled autofocus checked multiple ).each do |attr| map.delete(attr) unless Coerce.boolean(map[attr]) end map end |
#radio_button(*args, &block) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/dao/form.rb', line 230 def (*args, &block) = args.. keys = args.flatten type = .delete(:type) || :radio name = .delete(:name) || name_for(keys) id = .delete(:id) || id_for(keys) klass = class_for(keys, .delete(:class)) error = error_for(keys, .delete(:error)) unless .has_key?(:checked) checked = if .has_key?(:value) and attributes.has?(keys) a = attributes.get(keys) b = [:value] a==b or a.to_s==b.to_s else false end [:checked] = checked if checked end input_((, :type => :radio, :name => name, :class => klass, :id => id, :data_error => error)){} end |
#reset(*args) ⇒ Object
315 316 317 318 319 320 |
# File 'lib/dao/form.rb', line 315 def reset(*args) = args.. [:type] = :reset args.push() (*args) end |
#select(*args, &block) ⇒ Object
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/dao/form.rb', line 341 def select(*args, &block) = args.. keys = args.flatten name = .delete(:name) || name_for(keys) values = .delete(:values) || .delete(:options) || .delete(:from) has_blank = .has_key?(:blank) && [:blank] != false blank = .delete(:blank) id = .delete(:id) || id_for(keys) klass = class_for(keys, .delete(:class)) error = error_for(keys, .delete(:error)) if values.nil? key = keys.map{|key| "#{ key }"} key.last << "_options" values = attributes.get(*key) if attributes.has?(*key) end if [:multiple] name += '[]' end list = Array(values).map{|value| value.dup rescue value} # ensure list is dup'd case list.first when Hash, Array nil else list.flatten! list.compact! list.map!{|element| [element, element]} end if has_blank case blank when false blank = nil when nil, true blank = [nil, nil] else blank = [Array(blank).first, ''] end end selected_value = if .has_key?(:selected) .delete(:selected) else attributes.get(keys) end selected_values = {} Array(selected_value).flatten.compact.each do |val| selected_values[val.to_s] = true end select_((, :name => name, :class => klass, :id => id, :data_error => error)){ if blank content = blank.first || '' value = blank.last value.nil? ? option_(){ content } : option_(:value => value){ content } end unless list.empty? list.each do |pair| returned = block ? Dao.call(block, :call, pair.first, pair.last, selected_value) : pair opts = Map.new selected = nil case returned when Array content, value, selected, *ignored = returned if value.is_a?(Hash) map = Map.for(value) value = map.delete(:value) selected = map.delete(:selected) opts.update(map) end when Hash content = returned[:content] value = returned[:value] selected = returned[:selected] else content = returned value = returned selected = nil end if selected.nil? selected = selected_values.has_key?(value.to_s) end opts[:value] = (value.nil? ? content : value) opts[:selected] = Coerce.boolean(selected) if selected option_(opts){ content } end end } end |
#slug_for(string) ⇒ Object
580 581 582 583 584 585 586 |
# File 'lib/dao/form.rb', line 580 def slug_for(string) string = string.to_s words = string.to_s.scan(%r/\w+/) words.map!{|word| word.gsub(%r/[^0-9a-zA-Z_:-]/, '')} words.delete_if{|word| word.nil? or word.strip.empty?} words.join('-').downcase.sub(/_+$/, '') end |
#submit(*args, &block) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/dao/form.rb', line 201 def submit(*args, &block) = args.. content = block ? block.call : (args.first || 'Submit') [:name] ||= :submit [:type] ||= :submit [:value] ||= content input_(()){} end |
#textarea(*args, &block) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/dao/form.rb', line 322 def textarea(*args, &block) = args.. keys = args.flatten name = .delete(:name) || name_for(keys) id = .delete(:id) || id_for(keys) klass = class_for(keys, .delete(:class)) error = error_for(keys, .delete(:error)) value = if block.nil? and !.has_key?(:value) value_for(attributes, keys) else block ? block.call(attributes.get(keys)) : .delete(:value) end textarea_((, :name => name, :class => klass, :id => id, :data_error => error)){ value } end |
#titleize(string) ⇒ Object
588 589 590 591 592 |
# File 'lib/dao/form.rb', line 588 def titleize(string) string = string.to_s string = string.titleize if string.respond_to?(:titleize) string end |
#upload(*args, &block) ⇒ Object
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'lib/dao/form.rb', line 448 def upload(*args, &block) = args.. keys = args.flatten cache_key = keys + [:cache] file_key = keys + [:file] cache_name = .delete(:cache_name) || name_for(cache_key) file_name = .delete(:file_name) || .delete(:name) || name_for(file_key) id = .delete(:id) || id_for(keys) klass = class_for(keys, .delete(:class)) error = error_for(keys, .delete(:error)) cache_value = attributes.get(cache_key) tagz{ input_(:name => cache_name, :value => cache_value, :type => :hidden){ } __ input_((, :name => file_name, :class => klass, :id => id, :data_error => error, :type => :file)){ } } end |
#value_for(map, keys) ⇒ Object
506 507 508 509 510 511 512 513 514 515 516 517 518 |
# File 'lib/dao/form.rb', line 506 def value_for(map, keys) return nil unless map.has?(keys) value = map.get(keys) value = case value when Hash, Array value.to_json else value end end |