OSDN Git Service

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