OSDN Git Service

fix Manifest
[pettanr/pettanr.git] / lib / manifest / form.rb
1   class Form
2     attr :item_name, :conf, :manifest, :base, :fields, :field_names
3     def initialize item_name, conf, manifest
4       @item_name = item_name
5       @conf = conf || {}
6       @manifest = manifest
7       @base = @conf['base']
8       @fields = @conf['fields'] || {}
9       @field_names = @conf['field_names'] || []
10     end
11     
12   end
13   
14
15 module Pettanr
16   class FormManager
17     class Form
18       class Field
19         class Text
20           def initialize conf, field
21             @conf = conf
22             @field = field
23           end
24           
25           def tag_options
26             opt = @field.tag_options.merge(@conf['options'] || {})
27             @field.element.field_tag_attributes(@field.column, @field.tag_mounted, opt)
28           end
29           
30           def render view
31             view.text_field_tag @field.tag_name, @field.tag_value, self.tag_options
32           end
33           
34         end
35         
36         class TextArea < Text
37           def render view
38             view.text_area_tag @field.tag_name, @field.tag_value, self.tag_options
39           end
40           
41         end
42         
43         class Number < Text
44           def render view
45             view.number_field_tag @field.tag_name, @field.tag_value, self.tag_options
46           end
47           
48         end
49         
50         class Hidden < Text
51           def render view
52             view.hidden_field_tag @field.tag_name, @field.tag_value, self.tag_options
53           end
54           
55         end
56         
57         class Select < Text
58           def initialize conf, field
59             super
60             @model = Pettanr::Application::manifest.model(@field.model_name)
61             @source_conf = @model.attributes[@field.column]['source']
62           end
63           
64           def items
65             Pettanr::Application::manifest.select_items[@source_conf['type']][@source_conf['key']]
66           end
67           
68           def render view
69             view.select_tag @field.tag_name, 
70               view.options_for_select(view.t_select_items(self.items), :selected => @field.tag_value.to_s ), self.tag_options
71           end
72           
73         end
74         
75         class Helper
76           attr :field
77           def initialize field, name, conf
78             @conf = conf
79             @field = field
80             @name = name
81           end
82           
83           def wrapper
84             @conf['wrapper']
85           end
86           
87           def path
88             @conf['path']
89           end
90           
91           def options
92             @conf['options']
93           end
94           
95         end
96         
97         attr :part, :column, :type, :mounted, :model_name, :tag_options, :helpers, :form
98         @@type = {
99           'text' => Text, 'text_area' => TextArea, 'number' => Number, 
100           'hidden' => Hidden, 'select' => Select
101         }
102         def initialize form, mounted, conf, manifest
103           @form = form
104           @mounted = mounted
105           @conf = conf
106           @manifest = manifest
107           
108           @model_name = @conf['model'] || @form.model_name
109           @part = @conf['part']
110           @column = @conf['column']
111           @label_conf = @conf['label'] || {}
112           @field_conf = @conf['field']
113           @helper_confs = @conf['helpers'] || {}
114           
115           @tag_options = {'data-model' => @model_name}
116           
117           @model_manifest =  Pettanr::Application::manifest.model(@model_name)
118           model_attribute_conf =  @model_manifest.attributes[self.column] || {}
119           @primary_key = model_attribute_conf['primary_key']
120           
121           @helpers = @helper_confs.map do |helper_name, helper_conf|
122             Helper.new self, helper_name, helper_conf
123           end
124         end
125         
126         def element
127           @element ||= if self.part?
128             @form.element.__send__ self.part
129           else
130             @form.element
131           end
132         end
133         
134         def part?
135           self.part != nil
136         end
137         
138         def ignore_field?
139           @primary_key and self.element.new_record?
140         end
141         
142         def label?
143           @label_conf['type'] == nil or @label_conf['type'] != 'hidden'
144         end
145         
146         def label view
147           view.t 'activerecord.attributes.' + self.model_name + '.' +  self.column
148         end
149         
150         def label_break?
151           @label_conf['row_break'] == true
152         end
153         
154         def tag_name
155           if self.part?
156             @form.model_name + '[' + self.model_name + '_attributes][' + self.column + ']'
157           else
158             @form.model_name + '[' + self.column + ']'
159           end
160         end
161         
162         def tag_value
163           self.element.attributes[self.column]
164         end
165         
166         def tag_mounted
167           self.mounted ? 0 : 1
168         end
169         
170         def field
171           @field ||= @@type[@field_conf['type']].new @field_conf, self
172         end
173         
174         def has_helper?
175           !@helpers.empty?
176         end
177         
178         def row_break?
179           @conf['row_break'] == true
180         end
181         
182       end
183       
184       attr :element, :mounted, :attributes, :fields, :model_name
185       def initialize element, operators, mounted, manifest
186         @element = element
187         @operators = operators
188         @mounted = mounted
189         @manifest = manifest
190         
191         @base = @manifest.base
192         @model_name = @base || element.model_name
193         
194         base_field_confs = if @base
195           base_form = Pettanr::Application::manifest.form(@base)
196           base_form.fields
197         else
198           {}
199         end
200         extend_field_confs = @manifest.fields
201         @field_confs = base_field_confs.merge(extend_field_confs)
202         @fields = @manifest.field_names.map do |field_name|
203           field_conf = @field_confs[field_name]
204           Field.new self, @mounted, field_conf, manifest
205         end
206       end
207       
208       def name
209         @element.form_name
210       end
211       
212       def each_field
213         @fields.each do |field|
214           next if field.ignore_field?
215           yield field
216         end
217       end
218       
219     end
220     
221     attr :form_manifest, :item_name, :manifest, :form_conf
222     def initialize form_manifest
223       @form_manifest = form_manifest
224       @item_name = @form_manifest.item_name
225       @manifest = @form_manifest.manifest
226       @form_conf = @form_manifest.conf
227       
228     end
229     
230     def open element, operators, mounted
231       Form.new element, operators, mounted, @form_manifest
232     end
233     
234   end
235 end
236