lib/yaks/format/html.rb in yaks-html-0.8.0.alpha vs lib/yaks/format/html.rb in yaks-html-0.8.0.beta1

- old
+ new

@@ -1,11 +1,11 @@ # -*- coding: utf-8 -*- module Yaks class Format class HTML < self - include Adamantium + include Adamantium, Util register :html, :html, 'text/html' def template Hexp.parse(File.read(File.expand_path('../template.html', __FILE__))) @@ -83,21 +83,43 @@ form = form.attr('name', form_control.name) if form_control.name form = form.attr('method', form_control.method) if form_control.method form = form.attr('action', form_control.action) if form_control.action form = form.attr('enctype', form_control.media_type) if form_control.media_type - rows = form_control.fields.map do |field| - H[:tr, - H[:td, H[:label, {for: field.name}, field.label || '']], - H[:td, case field.type - when /\A(button|checkbox|file|hidden|image|password|radio|reset|submit|text)\z/ - H[:input, type: field.type, value: field.value, name: field.name] - when /textarea/ - H[:textarea, { name: field.name }, field.value || ''] - end] - ] - end + rows = form_control.fields.map(&method(:render_field)) + form.content(H[:table, form_control.title || '', *rows, H[:tr, H[:td, H[:input, {type: 'submit'}]]]]) + end + + def render_field(field) + return render_fieldset(field) if field.type == :fieldset + extra_info = reject_keys(field.to_h_compact, :type, :name, :value, :label, :options) + H[:tr, + H[:td, + H[:label, {for: field.name}, [field.label, field.required ? '*' : ''].join]], + H[:td, + case field.type + when /select/ + H[:select, reject_keys(field.to_h_compact, :options), render_select_options(field.options)] + when /textarea/ + H[:textarea, reject_keys(field.to_h_compact, :value), field.value || ''] + when /legend/ + H[:legend, field.to_h_compact] + else + H[:input, field.to_h_compact] + end], + H[:td, extra_info.empty? ? '' : extra_info.inspect] + ] + end + + def render_fieldset(fieldset) + H[:fieldset, fieldset.fields.map(&method(:render_field))] + end + + def render_select_options(options) + options.map do |o| + H[:option, reject_keys(o.to_h_compact, :label), o.label] + end end end end end