OSDN Git Service

t#31896:form conf
[pettanr/pettanr.git] / app / views / panels / _element_form.html.erb
diff --git a/app/views/panels/_element_form.html.erb b/app/views/panels/_element_form.html.erb
new file mode 100644 (file)
index 0000000..2ca3be2
--- /dev/null
@@ -0,0 +1,97 @@
+<%= form_for(elm) do |f| %>
+  <% element_name = elm.class.to_s.underscore %>
+  <% model = @configurations['models'][element_name] %>
+  <% view = if model['extend'] %>
+    <% elm.attributes[model['extend']] %>
+    <% @configurations['forms'][elm.attributes[model['extend']]] %>
+  <% else %>
+    <% @configurations['forms'][element_name] %>
+  <% end %>
+  <% form = if view.is_a? String %>
+    <% @configurations['forms'][view] %>
+  <% else %>
+    <% view %>
+  <% end %>
+  <% form['fields'].each do |field_conf| %>
+    <% e = if field_conf['part'] %>
+      <% @configurations['models'][field_conf['part']] %>
+    <% else %>
+      <% model %>
+    <% end %>
+    <% column = field_conf['column'] %>
+    <% primary_key = e['attributes'][column]['primary_key'] %>
+    
+    <% next if primary_key and elm.new_record? %>
+    
+    <% options = field_conf['options'] || {} %>
+    <% opt = {} %>
+    <% form_model_name = if form['base'] %>
+      <% form['base'] %>
+    <% else %>
+      <% element_name %>
+    <% end %>
+    <% field_model_name = if field_conf['part'] %>
+      <% field_conf['part'] %>
+    <% else %>
+      <% form_model_name %>
+    <% end %>
+    <% field_elm = if field_conf['part'] %>
+      <% elm.__send__ field_conf['part'] %>
+    <% else %>
+      <% elm %>
+    <% end %>
+    <% name = if field_conf['part'] %>
+      <% form_model_name + '[' + field_model_name + '_attributes][' + column + ']' %>
+    <% else %>
+      <% form_model_name + '[' + column + ']' %>
+    <% end %>
+    <div class="field">
+      <% unless field_conf['type'] == 'hidden' %>
+        <%= t 'activerecord.attributes.' + field_model_name + '.' +  column %>
+      <% end %>
+      <% if options['label_break'] %>
+        <br>
+      <% end %>
+      <% case field_conf['type'] %>
+      <% when 'text' %>
+        <% opt['size'] = field_conf['options']['size'] if field_conf['options']['size'] %>
+        <%= text_field_tag name, field_elm.attributes[column], field_elm.field_tag_attributes(column, no_attr, opt) %>
+      <% when 'text_area' %>
+        <% opt['size'] = field_conf['options']['size'] if field_conf['options']['size'] %>
+        <%= text_area_tag name, field_elm.attributes[column], field_elm.field_tag_attributes(column, no_attr, opt) %>
+      <% when 'number' %>
+        <% opt['size'] = field_conf['options']['size'] if field_conf['options']['size'] %>
+        <%= number_field_tag name, field_elm.attributes[column], field_elm.field_tag_attributes(column, no_attr, opt) %>
+      <% when 'hidden' %>
+        <%= hidden_field_tag name, field_elm.attributes[column], field_elm.field_tag_attributes(column, no_attr, opt) %>
+      <% when 'select' %>
+        <% items = case options['source'] %>
+        <% when 'model' %>
+          <% select_model = options['model'] %>
+          <% select_class = @configurations['models'][select_model]['class_name'] %>
+          <% l = select_class.constantize.__send__ options['method'] %>
+          <% l.map {|i| [i.attributes[options['caption']], i.id] } %>
+        <% when 'magic_number' %>
+          <% t_select_items(MagicNumber[options['key']]) %>
+        <% else %>
+        <% end %>
+        <%= select_tag name, options_for_select(items, field_elm.attributes[column]), field_elm.field_tag_attributes(column, no_attr) %>
+      <% end %>
+      <% if field_conf['helpers'] %>
+        <% field_conf['helpers'].each do |n, helper| %>
+          <% if helper['wrapper'] %>
+            <div class="<%= helper['wrapper'] -%>">
+          <% end %>
+          <%= render helper['path'], :elm => field_elm, :no_attr => no_attr, :opt => helper['options'] %>
+          <% if helper['wrapper'] %>
+            </div>
+          <% end %>
+        <% end %>
+      <% end %>
+    </div>
+    <% if options['row_break'] %>
+      <div class="row_break">
+      </div>
+    <% end %>
+  <% end %>
+<% end %>