5 def initialize page, page_size, operators, item_name, list_name, conf, manifest
11 @default_page_size = @conf['default_page_size'] || 25
12 @max_page_size = @conf['max_page_size'] || 100
13 @page = self.page_number(page)
14 @page_size = self.page_size page_size
16 @model = @item_name.classify.constantize
17 @offset = (@page -1) * @page_size
18 @table_name = @model.table_name
21 def page_number prm = nil
29 page_size = @max_page_size if page_size > @max_page_size
30 page_size = @default_page_size if page_size < 1
47 @model.where(self.where()).includes(self.includes).order(self.order).offset(@offset).limit(@page_size)
51 Kaminari.paginate_array(Array.new(@model.where(self.where()).includes(self.includes).count, nil)).page(@offset).per(@page_size)
56 class PublicList < BaseList
59 class SystemResourceList < BaseList
61 @model.where(self.where()).includes(@model.list_opt).order(@model.order).offset(@offset).limit(@page_size).delete_if {|sbt|
62 Pettanr::Application.manifest.system_resources.speech_balloon_templates[sbt.classname] == nil
67 Kaminari.paginate_array(Array.new(@model.where(self.where()).includes(@model.list_opt).count, nil)).page(@offset).per(@page_size)
72 class PrivateList < BaseList
74 case @model.owner_type
76 operator = @operators.author
78 operator = @operators.artist
80 [@model.plural + '.' + @model.owner_type.to_s + '_id = ?', operator.id]
85 class HasManyList < BaseList
86 def initialize page, page_size, operators, item_name, list_name, conf, manifest
88 @association_name = @conf['association_name']
89 @has_many_conf = @manifest.model(@item_name).associations['has_many'] || {}
90 @association_conf = @has_many_conf[@association_name] || {}
91 @association_model_name = @association_conf['model']
92 @association_model = @association_model_name.classify.constantize
93 @foreign_key = @association_conf['foreign_key']
98 w += ' and ' unless w.blank?
99 [w + @association_model_name + '.' + @foreign_key + ' = ?', parent_item.id]
102 def items parent_item
103 @model.where(self.where(parent_item)).includes(self.includes).order(self.order).offset(@offset).limit(@page_size)
106 def paginate parent_item
107 Kaminari.paginate_array(Array.new(@model.where(self.where(parent_item)).includes(self.includes).count, nil)).page(@offset).per(@page_size)
112 class FilterList < BaseList
113 def initialize page, page_size, operators, item_name, list_name, conf, manifest
115 @list_list_conf = @manifest.list(@item_name).lists[list_name] || {}
116 @from = @list_list_conf['from']
117 @filter_key = @list_list_conf['filter_key']
120 def where filter_item
121 w = @model.list_where
122 w += ' and ' unless w.blank?
123 [w + @table_name + '.' + @filter_key + ' = ?', filter_item.id]
130 def items filter_item
131 @model.where(self.where(filter_item)).includes(self.includes).order(self.order).offset(@offset).limit(@page_size)
134 def paginate filter_item
135 Kaminari.paginate_array(Array.new(@model.where(self.where(filter_item)).includes(self.includes).count, nil)).page(@offset).per(@page_size)
140 class ThroughFilterList < FilterList
141 def initialize page, page_size, operators, item_name, list_name, conf, manifest
143 @through = @list_list_conf['through']
147 {@through => {@from => {}} }
150 def where filter_item
151 w = @model.list_where
152 w += ' and ' unless w.blank?
153 [w + @through + '.' + @filter_key + ' = ?', filter_item.id]
159 'public' => PublicList, 'system_resource' => SystemResourceList, 'private' => PrivateList,
160 'has_many' => HasManyList, 'filter' => FilterList, 'through_filter' => ThroughFilterList
162 def initialize list_manifest
163 @list_manifest = list_manifest
164 @item_name = @list_manifest.item_name
165 @manifest = @list_manifest.manifest
166 @list_conf = @list_manifest.conf
168 @list_manifest.lists.each {|list_name, conf|
170 @lists[list_name] = @@types[type]
174 def open list_name, page, page_size, operators
175 @lists[list_name].new page, page_size, operators, @item_name, list_name, @list_conf, @manifest