module Perus::Server class Form include Helpers def initialize(record) @record = record end def field(field, type = nil, options = nil) field = field.to_s if type.nil? if @record.class.association_reflections.include?(field.to_sym) type = 'association' else type = @record.db_schema[field.to_sym][:db_type] end end html = "
" case type when 'varchar(255)' html << input(field, options) when 'text' html << textarea(field, options) when 'association' html << association(field, options) when 'select' html << select(field, options) end # return the field plus any errors html << "
" << errors_for(field) end def input(field, options) value = escape_quotes(@record.send(field)) "" end def textarea(field, options) "" end def association(field, options) reflection = @record.class.association_reflections[field.to_sym] other_model = reflection[:class_name].constantize id_field = reflection[:key] values = other_model.all.collect do |record| [record.id, record.name] end select(id_field, values) end def select(field, options) existing = @record.send(field) option_rows = options.collect do |(value, name)| selected = existing == value ? 'selected' : '' "" end "" end def errors_for(field) errors = @record.errors.on(field.to_sym) return '' unless errors field_name = field.titlecase descriptions = errors.map {|error| "#{field_name} #{error}"} "#{descriptions.join(', ')}
" end end end