OSDN Git Service

change page status
[pettanr/pettanr.git] / lib / locmare / list_group / list / base.rb
1 module Locmare
2   module ListGroupModule
3     class Base
4       attr :list_group_name, :list_name, :options, :item_name, 
5         :controller_manifest, :action_manifest, :list_manifest, :model,
6         :page_status
7       
8       def initialize list_group_name, list_name, operators, options = {}
9         @list_group_name = list_group_name
10         @list_name = list_name
11         @operators = operators
12         @options = options
13         
14         @list_group_manifest = LocalManifest.manifest.list_groups[@list_group_name]
15         @list_manifest = @list_group_manifest.lists[@list_name]
16         @where = @list_manifest.where
17         @includes = @list_manifest.includes
18         
19         @controller_name = @list_group_name
20         @action_name = @list_name
21         @controller_manifest = ::Manifest.manifest.controllers[@controller_name]
22         @action_manifest = @controller_manifest.actions[@action_name]
23         @item_name = @action_manifest.item_name
24         @model = ::Manifest.item_name_to_model @item_name
25         @table_name = @model.table_name
26         self.init
27       end
28       
29       def init
30         @page_status = LibModule::PageStatus.load self, self.total, @options
31         self.boost items
32       end
33       
34       def model_name
35         @model.model_name
36       end
37       
38       def limited?
39         self.max_page_size > 0
40       end
41       
42       def unlimited?
43         !self.limited?
44       end
45       
46       def default_page_size
47         @action_manifest.default_page_size
48       end
49       
50       def max_page_size
51         @action_manifest.max_page_size
52       end
53       
54       def order
55         order = @options['order'] || @options[:order] || @action_manifest.order
56         order = 'updated_at' unless ::Manifest.manifest.models[@item_name].attributes[order]
57         @table_name + '.' + order
58       end
59       
60       def direction
61         direction = @options['direction'] || @options[:direction] || @action_manifest.direction
62         if direction < 0
63           'desc'
64         elsif direction > 0
65           'asc'
66         else
67           ''
68         end
69       end
70       
71       def filter_id
72         @options['id'] || @options[:id]
73       end
74       
75       def order_by
76         self.order + ' ' + self.direction
77       end
78       
79       def base_where_condition
80         method_name = @where.conditions do |name|
81           name
82         end
83         @model.__send__ method_name
84       end
85       
86       def where_condition
87         base_where_condition
88       end
89       
90       def include_hash
91         @includes.includes
92       end
93       
94       def items
95         @items ||= @model.where(
96           self.where_condition
97         ).includes(
98           self.include_hash
99         ).order(
100           self.order_by
101         ).offset(@page_status.offset).limit(@page_status.limit)
102       end
103       
104       def total
105         @model.where(
106           self.where_condition
107         ).includes(
108           self.include_hash
109         ).count
110         # return string : =>"25"
111       end
112       
113       def boost_manifests
114         @model.my_peta.boost.select do |boost_name, boost_manifest|
115           boost_manifest.level == 'show'
116         end
117       end
118       
119       def boost items
120         manifests = self.boost_manifests
121         items.each do |item|
122           manifests.each do |boost_manifest|
123             item.boost_manifest # ?
124           end
125         end
126       end
127       
128     end
129     
130   end
131 end