OSDN Git Service

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