module Pettanr
class FormManager
class Form
+ class Field
+ class Text
+ def initialize conf, field
+ @conf = conf
+ @field = field
+ end
+
+ def tag_options
+ opt = @field.tag_options.merge(@conf['options'] || {})
+ @field.element.field_tag_attributes(@field.column, @field.tag_mounted, opt)
+ end
+
+ def render view
+ view.text_field_tag @field.tag_name, @field.tag_value, self.tag_options
+ end
+
+ end
+
+ class TextArea < Text
+ def render view
+ view.text_area_tag @field.tag_name, @field.tag_value, self.tag_options
+ end
+
+ end
+
+ class Number < Text
+ def render view
+ view.number_field_tag @field.tag_name, @field.tag_value, self.tag_options
+ end
+
+ end
+
+ class Hidden < Text
+ def render view
+ view.hidden_field_tag @field.tag_name, @field.tag_value, self.tag_options
+ end
+
+ end
+
+ class Select < Text
+ def initialize conf, field
+ super
+ @model = Pettanr::Application::manifest.model(@field.model_name)
+ @source_conf = @model.attributes[@field.column]['source']
+ end
+
+ def items
+ Pettanr::Application::manifest.select_items[@source_conf['type']][@source_conf['key']]
+ end
+
+ def render view
+ view.select_tag @field.tag_name,
+ view.options_for_select(view.t_select_items(self.items), :selected => @field.tag_value.to_s ), self.tag_options
+ end
+
+ end
+
+ class Helper
+ attr :field
+ def initialize field, name, conf
+ @conf = conf
+ @field = field
+ @name = name
+ end
+
+ def wrapper
+ @conf['wrapper']
+ end
+
+ def path
+ @conf['path']
+ end
+
+ def options
+ @conf['options']
+ end
+
+ end
+
+ attr :part, :column, :type, :mounted, :model_name, :tag_options, :helpers, :form
+ @@type = {
+ 'text' => Text, 'text_area' => TextArea, 'number' => Number,
+ 'hidden' => Hidden, 'select' => Select
+ }
+ def initialize form, mounted, conf, manifest
+ @form = form
+ @mounted = mounted
+ @conf = conf
+ @manifest = manifest
+
+ @model_name = @conf['model'] || @form.model_name
+ @part = @conf['part']
+ @column = @conf['column']
+ @label_conf = @conf['label'] || {}
+ @field_conf = @conf['field']
+ @helper_confs = @conf['helpers'] || {}
+
+ @tag_options = {'data-model' => @model_name}
+
+ @model_manifest = Pettanr::Application::manifest.model(@model_name)
+ model_attribute_conf = @model_manifest.attributes[self.column] || {}
+ @primary_key = model_attribute_conf['primary_key']
+
+ @helpers = @helper_confs.map do |helper_name, helper_conf|
+ Helper.new self, helper_name, helper_conf
+ end
+ end
+
+ def element
+ @element ||= if self.part?
+ @form.element.__send__ self.part
+ else
+ @form.element
+ end
+ end
+
+ def part?
+ self.part != nil
+ end
+
+ def ignore_field?
+ @primary_key and self.element.new_record?
+ end
+
+ def label?
+ @label_conf['type'] == nil or @label_conf['type'] != 'hidden'
+ end
+
+ def label view
+ view.t 'activerecord.attributes.' + self.model_name + '.' + self.column
+ end
+
+ def label_break?
+ @label_conf['row_break'] == true
+ end
+
+ def tag_name
+ if self.part?
+ @form.model_name + '[' + self.model_name + '_attributes][' + self.column + ']'
+ else
+ @form.model_name + '[' + self.column + ']'
+ end
+ end
+
+ def tag_value
+ self.element.attributes[self.column]
+ end
+
+ def tag_mounted
+ self.mounted ? 0 : 1
+ end
+
+ def field
+ @field ||= @@type[@field_conf['type']].new @field_conf, self
+ end
+
+ def has_helper?
+ !@helpers.empty?
+ end
+
+ def row_break?
+ @conf['row_break'] == true
+ end
+
+ end
+
+ attr :element, :mounted, :attributes, :fields, :model_name
+ def initialize element, operators, mounted, manifest
+ @element = element
+ @operators = operators
+ @mounted = mounted
+ @manifest = manifest
+
+ @base = @manifest.base
+ @model_name = @base || element.model_name
+
+ base_field_confs = if @base
+ base_form = Pettanr::Application::manifest.form(@base)
+ base_form.fields
+ else
+ {}
+ end
+ extend_field_confs = @manifest.fields
+ @field_confs = base_field_confs.merge(extend_field_confs)
+ @fields = @manifest.field_names.map do |field_name|
+ field_conf = @field_confs[field_name]
+ Field.new self, @mounted, field_conf, manifest
+ end
+ end
+
+ def name
+ @element.form_name
+ end
+
+ def each_field
+ @fields.each do |field|
+ next if field.ignore_field?
+ yield field
+ end
+ end
+
end
- attr :form_manifest, :item_name, :manifest, :form_conf,
+ attr :form_manifest, :item_name, :manifest, :form_conf
def initialize form_manifest
@form_manifest = form_manifest
@item_name = @form_manifest.item_name
@manifest = @form_manifest.manifest
@form_conf = @form_manifest.conf
- attributes = @form_manifest.conf
end
- def attributes
- end
-
- def open list_name, item, operators
- Form.new item_name, item, operators, @manifest
+ def open element, operators, mounted
+ Form.new element, operators, mounted, @form_manifest
end
end