OSDN Git Service

fix leaf view
[pettanr/pettanr.git] / lib / locmare / list_group / list / base.rb
1 module Locmare
2   module ListGroupModule
3     class Base
4       class ListResult
5         attr :list, :items, :count, :paginate
6         
7         def initialize list, items, count, paginate, operators, options
8           @list = list
9           @items = items
10           @count = count  # total count
11           @paginate = paginate
12           @operators = operators
13           @options = options
14         end
15         
16         def path
17           if list_manifest.has_id?
18             filter_item_id = @options[:id]
19             if list_manifest.pre_id?
20               '/' + list_manifest.model.path_name + '/' + filter_item_id.to_s + '/' + list_manifest.action_name
21             else
22               '/' + list_manifest.model.path_name + '/' + list_manifest.action_name + '/' + filter_item_id.to_s
23             end
24           else
25             '/' + list_manifest.model.path_name + '/' + list_manifest.action_name
26           end
27         end
28         
29         def list_manifest
30           @list.list_manifest
31         end
32         
33       end
34       
35       attr :item_name, :list_name, 
36         :model_manifest, :model_list_manifest, :list_manifest, :model, 
37         :default_page_size, :max_page_size
38       def initialize item_name, list_name
39         @item_name = item_name
40         @list_name = list_name
41         self.init
42       end
43       
44       def init
45         @list_group_manifest = LocalManifest.manifest.list_groups[@item_name]
46         @list_manifest = @list_group_manifest.lists[@list_name]
47         @where = @list_manifest.where
48         @includes = @list_manifest.includes
49         @order = @list_manifest.order
50         @model_manifest = ::Manifest.manifest.models[@item_name]
51         @model_list_manifest = @model_manifest.list
52         @model = ::Manifest.item_name_to_model @item_name
53         @table_name = @model.table_name
54       end
55       
56       def model_name
57         @model.model_name
58       end
59       
60       def model_manifest
61         @model.model_manifest
62       end
63       
64       def page_number prm = nil
65         page = prm.to_i
66         page = 1 if page < 1
67         page
68       end
69       
70       def page_size prm
71         page_size = prm.to_i
72         page_size = self.max_page_size if page_size > self.max_page_size
73         page_size = self.default_page_size if page_size < 1
74         page_size
75       end
76       
77       def default_page_size
78         @model_list_manifest.default_page_size
79       end
80       
81       def max_page_size
82         @model_list_manifest.max_page_size
83       end
84       
85       def base_where_condition
86         method_name = @where.conditions do |name|
87           name
88         end
89         @model.__send__ method_name
90       end
91       
92       def where_condition
93         base_where_condition
94       end
95       
96       def include_hash
97         @includes.includes
98       end
99       
100       def order
101         @model.__send__ @order.order
102       end
103       
104       def items operators, options, offset, page_size
105         @model.where(self.where_condition).includes(self.include_hash).order(self.order).offset(offset).limit(page_size)
106       end
107       
108       def count operators, options
109         @model.where(self.where_condition).includes(self.include_hash).count
110       end
111       
112       def paginate count, page, page_size
113         c = count ? count.to_i : 0
114         Kaminari.paginate_array(Array.new(c, nil)).page(page).per(page_size)
115       end
116       
117       def boost_manifests
118         @model.my_peta.boost.select do |boost_name, boost_manifest|
119           boost_manifest.level == 'show'
120         end
121       end
122       
123       def boost items
124         manifests = self.boost_manifests
125         items.each do |item|
126           manifests.each do |boost_manifest|
127             item.boost_manifest # ?
128           end
129         end
130       end
131       
132       def open operators, options
133         page = self.page_number(options[:page]) 
134         page_size = self.page_size options[:page_size]
135         offset = (page -1) * page_size
136         items = self.items operators, options, offset, page_size
137         count = self.count operators, options
138         pg = self.paginate count, page, page_size
139         self.boost items
140         ListResult.new self, items, count, pg, operators, options
141       end
142       
143     end
144     
145   end
146 end