OSDN Git Service

fix Manifest
[pettanr/pettanr.git] / lib / manifest / profiler.rb
1   class Profiler
2     attr :item_name, :conf, :manifest, :columns, :lists, :belongs_to, :has_many, :has_one
3     def initialize item_name, conf, manifest
4       @item_name = item_name
5       @conf = conf || {}
6       @manifest = manifest
7       @columns = @conf['columns'] || {}
8       @lists = @conf['lists'] || {}
9       @associations_conf = @conf['associations'] || {}
10       @belongs_to = @associations_conf['belongs_to'] || []
11       @has_many = @associations_conf['has_many'] || []
12       @has_one = @associations_conf['has_one'] || []
13     end
14     
15   end
16   
17
18 module Pettanr
19   class ProfilerManager
20     class Profiler
21       class Column
22         def initialize item_name, column_name, item, operators, manifest
23           @item_name = item_name
24           @column_name = column_name
25           @item = item
26           @operators = operators
27           @manifest = manifest
28           
29           @model = @item_name.classify.constantize
30           @model_attributes = @manifest.model(@item_name).attributes
31           @column_conf = @model_attributes[@column_name]
32         end
33         
34         def label view
35           @model.human_attribute_name(@column_name)
36         end
37         
38         def date?
39           case @column_conf['type']
40           when 'datetime'
41             if self.value
42               true
43             else
44               false
45             end
46           else
47             false
48           end
49         end
50         
51         def value
52           @item.attributes[@column_name]
53         end
54         
55         def disp_value view
56           if self.date?
57             view.l self.value
58           else
59             self.value
60           end
61         end
62         
63         def note?
64           if @column_conf['source']
65             case @column_conf['source']['type']
66             when 'magic_number'
67               true
68             when 'model'
69               false
70             else
71               false
72             end
73           else
74             false
75           end
76         end
77         
78         def note view
79           if self.note?
80             '(' + view.t_selected_item(@column_conf['source']['key'], self.value) + ')'
81           else
82           end
83         end
84         
85       end
86       
87       def initialize item_name, item, operators, conf, manifest
88         @item_name = item_name
89         @item = item
90         @operators = operators
91         @conf = conf
92         @manifest = manifest
93         
94         @lists = @manifest.profiler(@item_name).lists
95         @belongs_to_conf = @manifest.profiler(@item_name).belongs_to
96         @has_many_conf = @manifest.profiler(@item_name).has_many
97         @has_one_conf = @manifest.profiler(@item_name).has_one
98       end
99       
100       def modelize str
101         str.classify.constantize
102       end
103       
104       def header_filer
105         @manifest.filer_managers[@item_name].open @item_name, [@item], @operators, nil
106       end
107       
108       def each_column
109         @conf['columns'].each do |column_name|
110           column = Column.new @item_name, column_name, @item, @operators, @manifest
111           yield column
112         end
113       end
114       
115       def parent_item parent_model, model_belongs_to_conf
116         r = nil
117         id_column = model_belongs_to_conf['id_column']
118         begin
119           r = parent_model.show(@item.attributes[id_column], @operators)
120         rescue ActiveRecord::RecordNotFound, ActiveRecord::Forbidden
121         end
122         r
123       end
124       
125       def each_belongs_to
126         @belongs_to_conf.each do |association_model_name|
127           model_belongs_to_conf = @manifest.model(@item_name).belongs_to[association_model_name]
128           parent_model = self.modelize association_model_name
129           parent = self.parent_item parent_model, model_belongs_to_conf
130           if parent
131             parent_filer = @manifest.filer_managers[parent_model.item_name].open parent_model.item_name, [parent], @operators, nil
132             yield parent_filer
133           end
134         end
135       end
136       
137       def each_has_many
138         @has_many_conf.each do |profiler_list_name|
139           raise "undefined lists for #{profiler_list_name} in #{@lists}\n" unless @lists[profiler_list_name]
140           model_name = @lists[profiler_list_name]['model_name']
141           list_name = @lists[profiler_list_name]['list_name']
142           raise "undefined has_many list for #{model_name}::#{list_name}\nconf:#{@controller_conf}\n" unless @manifest.list_managers[model_name]
143           list = @manifest.list_managers[model_name].open(list_name, 1, 3, @operators)
144           items = list.items @item
145 #p [model_name, list_name, @item, list.class, list.includes, list.where(@item )]
146           has_many_filer = @manifest.filer_managers[model_name].open model_name, items, @operators, nil
147           model = model_name.classify.constantize
148           more_url = "/#{model.table_name}/#{list_name}/#{@item.id}"
149           yield has_many_filer, more_url
150         end
151       end
152       
153       def each_has_one 
154         @has_one_conf.each do |profiler_list_name|
155           raise "undefined lists for #{profiler_list_name} in #{@lists}\n" unless @lists[profiler_list_name]
156           model_name = @lists[profiler_list_name]['model_name']
157           list_name = @lists[profiler_list_name]['list_name']
158           raise "undefined has_one list for #{model_name}::#{list_name}\nconf:#{@controller_conf}\n" unless @manifest.list_managers[model_name]
159           list = @manifest.list_managers[model_name].open(list_name, 1, 1, @operators)
160           items = list.items @item
161           has_one_filer = @manifest.filer_managers[model_name].open model_name, items, @operators, nil
162           yield has_one_filer
163         end
164       end
165       
166     end
167     
168     def initialize profiler_manifest
169       @profiler_manifest = profiler_manifest
170       @item_name = @profiler_manifest.item_name
171       @manifest = profiler_manifest.manifest
172       @profiler_conf = @profiler_manifest.conf
173     end
174     
175     def open item, operators
176       Profiler.new @item_name, item, operators, @profiler_conf, @manifest
177     end
178     
179   end
180   
181 end
182