OSDN Git Service

add scroll player
[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         @page_status = LibModule::PageStatus.load self, self.total, @options
31       end
32       
33       def model_name
34         @model.model_name
35       end
36       
37       def limited?
38         self.max_page_size > 0
39       end
40       
41       def unlimited?
42         !self.limited?
43       end
44       
45       def default_page_size
46         @action_manifest.default_page_size
47       end
48       
49       def max_page_size
50         @action_manifest.max_page_size
51       end
52       
53       def order
54         order = @options['order'] || @options[:order] || @action_manifest.order
55         order = 'updated_at' unless ::Manifest.manifest.models[@item_name].attributes[order]
56         @table_name + '.' + order
57       end
58       
59       def direction
60         direction = @options['direction'] || @options[:direction] || @action_manifest.direction
61         if direction < 0
62           'desc'
63         elsif direction > 0
64           'asc'
65         else
66           ''
67         end
68       end
69       
70       def filter_id
71         @options['id'] || @options[:id]
72       end
73       
74       def order_by
75         self.order + ' ' + self.direction
76       end
77       
78       def include_hash
79         @includes.includes
80       end
81       
82       def arel
83         r = @model
84         @wheres.each do |key|
85           n = key + '_list_where'
86           r = r.where(@model.respond_to?(n) ? @model.__send__(n, self) : self.__send__(n))
87         end
88         r.includes(
89           self.include_hash
90         )
91       end
92       
93       def items
94         return [] unless @page_status
95         @items ||= self.arel.order(
96           self.order_by
97         ).offset(@page_status.offset).limit(@page_status.limit)
98         self.boost
99         @items
100       end
101       
102       def total
103         self.arel.count
104         # return string : =>"25"
105       end
106       
107       def boost_manifests
108         @model.my_peta.boost.select do |boost_name, boost_manifest|
109           boost_manifest.level == 'show'
110         end
111       end
112       
113       def boost
114         manifests = self.boost_manifests
115         @items.each do |item|
116           manifests.each do |boost_manifest|
117             item.boost_manifest # ?
118           end
119         end
120       end
121       
122       def private_list_where
123         owner_model = if @model.element?
124           @model.root_model
125         else
126           @model
127         end
128         case owner_model.owner_type
129         when :author
130           operator = @operators.author
131         when :artist
132           operator = @operators.artist
133         end
134         [owner_model.table_name + '.' + owner_model.owner_type.to_s + '_id = ?', operator.id]
135       end
136       
137     end
138     
139   end
140 end