OSDN Git Service

3912b038b703673a86de102ce8aeccf8b382b297
[pettanr/pettanr.git] / lib / locmare / list_group / list / play.rb
1 module Locmare
2   module ListGroupModule
3     class PlayList < FilterList
4       
5       def offset cnt, prm = nil
6         offset = prm.to_i
7         offset = cnt - 1 if offset >= cnt
8         offset = cnt - offset.abs if offset < 0
9         offset = 0 if offset < 0
10         offset
11       end
12       
13       def limit prm
14         prm ||= self.default_page_size
15         count = prm.to_i
16         count = self.max_page_size if count > self.max_page_size
17         count = self.default_page_size if count < 1
18         count
19       end
20       
21       def where_condition filter_item_id, my_play = false
22         base_where = if my_play
23           ''
24         else
25           w = self.base_where_condition
26           w += ' and ' unless w.blank?
27           w
28         end
29         [base_where + @table_name + '.' + @filter_key + ' = ?', filter_item_id] 
30       end
31       
32       def items operators, options, offset, limit
33         filter_item_id = options[:id]
34         my_play = options[:my_play]
35         @model.where(self.where_condition(filter_item_id, my_play)).includes(self.include_hash).order(self.order).offset(offset).limit(limit)
36       end
37       
38       def count operators, options
39         filter_item_id = options[:id]
40         my_play = options[:my_play]
41         @model.where(self.where_condition(filter_item_id, my_play)).includes(self.include_hash).count
42       end
43       
44       def open operators, options
45         count = self.count operators, options
46         offset = self.offset count, options[:offset]
47         limit = self.limit options[:count]
48         items = self.items operators, options, offset, limit
49         self.boost items
50         ListResult.new self, items, nil, operators, options
51       end
52       
53     end
54     
55   end
56 end