OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / lib / locmare / list_group / list / base.rb
1 module Locmare
2   module ListGroupModule
3     class Base
4       attr_accessor :list_group_name, :list_name, :operators, :options, :item_name, 
5         :controller_manifest, :action_manifest, :list_manifest, :model, :table_name,
6         :page_status, :item_wheres, :list_wheres
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         @wheres = @list_manifest.wheres
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       end
31       
32       def reset
33         @page_status = nil
34         @item = nil
35         @total = nil
36       end
37       
38       def page_status
39         @page_status ||= LibModule::PageStatus.load(self, self.total, @options)
40       end
41       
42       def model_name
43         @model.model_name
44       end
45       
46       def limited?
47         self.max_page_size > 0
48       end
49       
50       def unlimited?
51         !self.limited?
52       end
53       
54       def default_page_size
55         @action_manifest.default_page_size
56       end
57       
58       def max_page_size
59         @action_manifest.max_page_size
60       end
61       
62       def order
63         order = @options['order'] || @options[:order] || @action_manifest.order
64         order = 'updated_at' unless ::Manifest.manifest.models[@item_name].attributes[order]
65         @table_name + '.' + order
66       end
67       
68       def direction
69         direction = @options['direction'] || @options[:direction] || @action_manifest.direction
70         if direction < 0
71           'desc'
72         elsif direction > 0
73           'asc'
74         else
75           ''
76         end
77       end
78       
79       def filter_id
80         @options['id'] || @options[:id]
81       end
82       
83       def order_by
84         self.order + ' ' + self.direction
85       end
86       
87       def include_hash
88         @includes.includes
89       end
90       
91       def arel
92         r = @model
93         @wheres.each do |key|
94           n = key + '_list_where'
95           r = r.where(@model.respond_to?(n) ? @model.__send__(n, self) : self.__send__(n))
96         end
97         r.includes(
98           self.include_hash
99         )
100       end
101       
102       def items
103         self.total  # get total
104         return [] unless self.page_status  #.pager
105         @items ||= self.arel.order(
106           self.order_by
107         ).offset(self.page_status.offset).limit(self.page_status.limit)
108         self.boost
109         @items
110       end
111       
112       def total
113         @total ||= self.arel.count
114       end
115       
116       def boost_manifests
117         @model.my_peta.boost.select do |boost_name, boost_manifest|
118           boost_manifest.level == 'show'
119         end
120       end
121       
122       def boost
123         manifests = self.boost_manifests
124         @items.each do |item|
125           manifests.each do |boost_manifest|
126             item.boost_manifest # ?
127           end
128         end
129       end
130       
131       def private_list_where
132         owner_model = if @model.element?
133           @model.root_model
134         else
135           @model
136         end
137         case owner_model.owner_type
138         when :author
139           operator = @operators.author
140         when :artist
141           operator = @operators.artist
142         end
143         [owner_model.table_name + '.' + owner_model.owner_type.to_s + '_id = ?', operator.id]
144       end
145       
146     end
147     
148   end
149 end