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
- #escape_html(string) ⇒ Object
- #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
558 559 560 561 562 563 564 565 566 567 568 569 |
# File 'lib/dao/form.rb', line 558 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
554 555 556 |
# File 'lib/dao/form.rb', line 554 def Form.name_for(name, *keys) "#{ prefix_for(name) }[#{ key_for(*keys) }]" end |
.prefix_for(name) ⇒ Object
550 551 552 |
# File 'lib/dao/form.rb', line 550 def Form.prefix_for(name) "dao[#{ name }]" end |
Instance Method Details
#attr_for(string) ⇒ Object
602 603 604 |
# File 'lib/dao/form.rb', line 602 def attr_for(string) slug_for(string).gsub(/_/, '-') end |
#button(*args, &block) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/dao/form.rb', line 232 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')) value = escape_html(value) content = escape_html(content) ((, :type => type, :name => name, :value => value, :class => klass, :id => id, :data_error => error)){ content } end |
#checkbox(*args, &block) ⇒ Object
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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/dao/form.rb', line 277 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) 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
512 513 514 515 516 517 518 519 520 |
# File 'lib/dao/form.rb', line 512 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
606 607 608 |
# File 'lib/dao/form.rb', line 606 def data_attr_for(string) "data-#{ attr_for(string) }" end |
#element(which, *args, &block) ⇒ Object
html generation methods
151 152 153 |
# File 'lib/dao/form.rb', line 151 def element(which, *args, &block) send(which, *args, &block) end |
#error_for(keys, klass = nil) ⇒ Object
522 523 524 525 526 527 528 |
# File 'lib/dao/form.rb', line 522 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
504 505 506 |
# File 'lib/dao/form.rb', line 504 def errors_on(keys) errors.get(keys) end |
#errors_on?(*keys) ⇒ Boolean
508 509 510 |
# File 'lib/dao/form.rb', line 508 def errors_on?(*keys) !errors_on(keys).blank? end |
#escape_html(string) ⇒ Object
546 547 548 |
# File 'lib/dao/form.rb', line 546 def escape_html(string) Tagz.escape_html(string) end |
#form(*args, &block) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/dao/form.rb', line 155 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
330 331 332 333 334 335 |
# File 'lib/dao/form.rb', line 330 def hidden(*args, &block) = args.. [:type] = :hidden args.push() input(*args, &block) end |
#id_for(keys) ⇒ Object
html generation support methods
499 500 501 502 |
# File 'lib/dao/form.rb', line 499 def id_for(keys) id = [name, keys.join('-')].compact.join('_') slug_for(id) end |
#input(*args, &block) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/dao/form.rb', line 194 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 value = escape_html(value) input_((, :type => type, :name => name, :value => value, :class => klass, :id => id, :data_error => error)){} end |
#key_for(*keys) ⇒ Object
571 572 573 |
# File 'lib/dao/form.rb', line 571 def key_for(*keys) Form.key_for(name, *keys) end |
#label(*args, &block) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/dao/form.rb', line 175 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
575 576 577 |
# File 'lib/dao/form.rb', line 575 def name_for(*keys) Form.name_for(name, *keys) end |
#options_for(*hashes) ⇒ Object
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 |
# File 'lib/dao/form.rb', line 579 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
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/dao/form.rb', line 252 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
337 338 339 340 341 342 |
# File 'lib/dao/form.rb', line 337 def reset(*args) = args.. [:type] = :reset args.push() (*args) end |
#select(*args, &block) ⇒ Object
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 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/dao/form.rb', line 365 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
610 611 612 613 614 615 616 |
# File 'lib/dao/form.rb', line 610 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
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/dao/form.rb', line 216 def submit(*args, &block) = args.. keys = args.flatten keys.push(:submit) if keys.empty? name = .delete(:name) || name_for(keys) content = block ? block.call : (args.first || 'Submit') [:name] ||= name [:type] ||= :submit [:value] ||= content input_(()){} end |
#textarea(*args, &block) ⇒ Object
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/dao/form.rb', line 344 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 value = escape_html(value) textarea_((, :name => name, :class => klass, :id => id, :data_error => error)){ value } end |
#titleize(string) ⇒ Object
618 619 620 621 622 |
# File 'lib/dao/form.rb', line 618 def titleize(string) string = string.to_s string = string.titleize if string.respond_to?(:titleize) string end |
#upload(*args, &block) ⇒ Object
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
# File 'lib/dao/form.rb', line 472 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
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/dao/form.rb', line 530 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 escape_html(value) end |