OSDN Git Service

t#31896:form conf
[pettanr/pettanr.git] / app / views / panels / _element_form.html.erb
1 <%= form_for(elm) do |f| %>
2   <% element_name = elm.class.to_s.underscore %>
3   <% model = @configurations['models'][element_name] %>
4   <% view = if model['extend'] %>
5     <% elm.attributes[model['extend']] %>
6     <% @configurations['forms'][elm.attributes[model['extend']]] %>
7   <% else %>
8     <% @configurations['forms'][element_name] %>
9   <% end %>
10   <% form = if view.is_a? String %>
11     <% @configurations['forms'][view] %>
12   <% else %>
13     <% view %>
14   <% end %>
15   <% form['fields'].each do |field_conf| %>
16     <% e = if field_conf['part'] %>
17       <% @configurations['models'][field_conf['part']] %>
18     <% else %>
19       <% model %>
20     <% end %>
21     <% column = field_conf['column'] %>
22     <% primary_key = e['attributes'][column]['primary_key'] %>
23     
24     <% next if primary_key and elm.new_record? %>
25     
26     <% options = field_conf['options'] || {} %>
27     <% opt = {} %>
28     <% form_model_name = if form['base'] %>
29       <% form['base'] %>
30     <% else %>
31       <% element_name %>
32     <% end %>
33     <% field_model_name = if field_conf['part'] %>
34       <% field_conf['part'] %>
35     <% else %>
36       <% form_model_name %>
37     <% end %>
38     <% field_elm = if field_conf['part'] %>
39       <% elm.__send__ field_conf['part'] %>
40     <% else %>
41       <% elm %>
42     <% end %>
43     <% name = if field_conf['part'] %>
44       <% form_model_name + '[' + field_model_name + '_attributes][' + column + ']' %>
45     <% else %>
46       <% form_model_name + '[' + column + ']' %>
47     <% end %>
48     <div class="field">
49       <% unless field_conf['type'] == 'hidden' %>
50         <%= t 'activerecord.attributes.' + field_model_name + '.' +  column %>
51       <% end %>
52       <% if options['label_break'] %>
53         <br>
54       <% end %>
55       <% case field_conf['type'] %>
56       <% when 'text' %>
57         <% opt['size'] = field_conf['options']['size'] if field_conf['options']['size'] %>
58         <%= text_field_tag name, field_elm.attributes[column], field_elm.field_tag_attributes(column, no_attr, opt) %>
59       <% when 'text_area' %>
60         <% opt['size'] = field_conf['options']['size'] if field_conf['options']['size'] %>
61         <%= text_area_tag name, field_elm.attributes[column], field_elm.field_tag_attributes(column, no_attr, opt) %>
62       <% when 'number' %>
63         <% opt['size'] = field_conf['options']['size'] if field_conf['options']['size'] %>
64         <%= number_field_tag name, field_elm.attributes[column], field_elm.field_tag_attributes(column, no_attr, opt) %>
65       <% when 'hidden' %>
66         <%= hidden_field_tag name, field_elm.attributes[column], field_elm.field_tag_attributes(column, no_attr, opt) %>
67       <% when 'select' %>
68         <% items = case options['source'] %>
69         <% when 'model' %>
70           <% select_model = options['model'] %>
71           <% select_class = @configurations['models'][select_model]['class_name'] %>
72           <% l = select_class.constantize.__send__ options['method'] %>
73           <% l.map {|i| [i.attributes[options['caption']], i.id] } %>
74         <% when 'magic_number' %>
75           <% t_select_items(MagicNumber[options['key']]) %>
76         <% else %>
77         <% end %>
78         <%= select_tag name, options_for_select(items, field_elm.attributes[column]), field_elm.field_tag_attributes(column, no_attr) %>
79       <% end %>
80       <% if field_conf['helpers'] %>
81         <% field_conf['helpers'].each do |n, helper| %>
82           <% if helper['wrapper'] %>
83             <div class="<%= helper['wrapper'] -%>">
84           <% end %>
85           <%= render helper['path'], :elm => field_elm, :no_attr => no_attr, :opt => helper['options'] %>
86           <% if helper['wrapper'] %>
87             </div>
88           <% end %>
89         <% end %>
90       <% end %>
91     </div>
92     <% if options['row_break'] %>
93       <div class="row_break">
94       </div>
95     <% end %>
96   <% end %>
97 <% end %>