OSDN Git Service

add: short cut
[pettanr/pettanr.git] / lib / locmare / bucket.rb
1
2 module Locmare
3   class Bucket
4     attr :manifest, :bucket_name, :use_name, :item, :mounted, :submit, :operators, :template_dir, 
5       :fields
6     
7     def self.factory bucket_or_form_name, use_name, item, mounted, submit, operators
8       bucket = LocalManifest.manifest.buckets[bucket_or_form_name]
9       c = if bucket and bucket.has_member?
10         Locmare::Bucket
11       else
12         Locmare::Form
13       end
14       c.new bucket_or_form_name, use_name, item, mounted, submit, operators
15     end
16     
17     def initialize bucket_name, use_name, item, mounted, submit, operators
18       @bucket_name = bucket_name
19       @use_name = use_name
20       @item = item
21       @mounted = mounted
22       @submit = submit
23       @operators = operators
24       
25       @item.boosts 'post'
26       @manifest = LocalManifest.manifest.buckets[@bucket_name]
27       @template_dir = 'templates/r/form/'
28       @form_fields = []
29       @forms = {}
30       @forms[@item.item_name] = Locmare::Form.new @item.item_name, @use_name, @item, @mounted, false, @operators
31       self.init_forms @item.item_name, @manifest, @item
32       self.init_fields
33     end
34     
35     def fold_extend_settings params
36       @item.class.fold_extend_settings params[@item.item_name]
37       self.members_fold_extend_settings @item, params[@item.item_name], @manifest
38     end
39     
40     def members_fold_extend_settings fold_item, attr, man
41       man.members.each do |member_name, member_manifest|
42         member_item = fold_item.__send__ member_name
43         member_item.class.fold_extend_settings attr[member_name + '_attributes']
44         self.members_fold_extend_settings member_item, attr[member_name + '_attributes'], member_manifest
45       end
46     end
47     
48     def each_field
49       @form_fields.each do |form_field|
50         yield form_field.field_name, form_field
51       end
52     end
53     
54     def push_form key, form_name, item
55     end
56     
57     def init_forms key, man, item
58       man.members.each do |member_name, member_manifest|
59         form_name = member_name   # write exchange function if you want
60         member_item = item.__send__(form_name)
61         new_key = key + '.' + form_name
62         @forms[new_key] = Locmare::Form.new new_key, member_item, @mounted, false, @operators
63         self.init_forms new_key, member_manifest, member_item
64       end
65     end
66     
67     def init_fields
68       @manifest.form_field_names.each do |form_field_name|
69         r = form_field_name.form_name.blank? ? '' : '.' + form_field_name.form_name
70         key = @item.item_name + r
71         form = @forms[key]
72         field = form.fields[form_field_name.field_name]
73         @form_fields << field
74       end
75     end
76     
77     def template_file_name
78       "form"
79     end
80     
81     def template_name
82       self.template_dir + self.template_file_name
83     end
84     
85     def fields_template_name
86       self.template_dir + 'fields'
87     end
88     
89     def image_dir
90       '/images/'
91     end
92     
93     def submit_name
94       if @item.new_record?
95         @item.table_name + '.submit.new'
96       else
97         @item.table_name + '.submit.edit'
98       end
99     end
100     
101     def submit_template_dir
102       self.template_dir + 'submit/'
103     end
104     
105     def submit_template_name
106       if @submit
107         self.submit_template_dir + 'submit'
108       else
109         self.submit_template_dir + 'none'
110       end
111     end
112     
113     def form_name
114       @bucket_name
115     end
116     
117   end
118   
119 end
120