OSDN Git Service

change page status
[pettanr/pettanr.git] / lib / locmare / list_group / list / base.rb
index b5a447a..e899fd4 100644 (file)
@@ -1,83 +1,79 @@
 module Locmare
   module ListGroupModule
     class Base
-      class ListResult
-        attr :list, :items, :paginate
-        
-        def initialize list, items, paginate, operators, options
-          @list = list
-          @items = items
-          @paginate = paginate
-          @operators = operators
-          @options = options
-        end
-        
-        def path
-          if list_manifest.has_id?
-            filter_item_id = @options[:id]
-            if list_manifest.pre_id?
-              list_manifest.model.path_name + '/' + filter_item_id.to_s + '/' + list_manifest.action_name
-            else
-              list_manifest.model.path_name + '/' + list_manifest.action_name + '/' + filter_item_id.to_s
-            end
-          else
-            list_manifest.model.path_name + '/' + list_manifest.action_name
-          end
-        end
-        
-        def list_manifest
-          @list.list_manifest
-        end
-        
-      end
+      attr :list_group_name, :list_name, :options, :item_name, 
+        :controller_manifest, :action_manifest, :list_manifest, :model,
+        :page_status
       
-      attr :item_name, :list_name, 
-        :model_manifest, :model_list_manifest, :list_manifest, :model, 
-        :default_page_size, :max_page_size
-      def initialize item_name, list_name
-        @item_name = item_name
+      def initialize list_group_name, list_name, operators, options = {}
+        @list_group_name = list_group_name
         @list_name = list_name
-        self.init
-      end
-      
-      def init
-        @list_group_manifest = LocalManifest.manifest.list_groups[@item_name]
+        @operators = operators
+        @options = options
+        
+        @list_group_manifest = LocalManifest.manifest.list_groups[@list_group_name]
         @list_manifest = @list_group_manifest.lists[@list_name]
         @where = @list_manifest.where
         @includes = @list_manifest.includes
-        @model_manifest = ::Manifest.manifest.models[@item_name]
-        @model_list_manifest = @model_manifest.list
+        
+        @controller_name = @list_group_name
+        @action_name = @list_name
+        @controller_manifest = ::Manifest.manifest.controllers[@controller_name]
+        @action_manifest = @controller_manifest.actions[@action_name]
+        @item_name = @action_manifest.item_name
         @model = ::Manifest.item_name_to_model @item_name
         @table_name = @model.table_name
+        self.init
       end
       
-      def model_name
-        @model.model_name
+      def init
+        @page_status = LibModule::PageStatus.load self, self.total, @options
+        self.boost items
       end
       
-      def model_manifest
-        @model.model_manifest
+      def model_name
+        @model.model_name
       end
       
-      def page_number prm = nil
-        page = prm.to_i
-        page = 1 if page < 1
-        page
+      def limited?
+        self.max_page_size > 0
       end
       
-      def page_size prm
-        page_size = prm.to_i
-        page_size = self.max_page_size if page_size > self.max_page_size
-        page_size = self.default_page_size if page_size < 1
-        page_size
+      def unlimited?
+        !self.limited?
       end
       
       def default_page_size
-        @model_list_manifest.default_page_size
+        @action_manifest.default_page_size
       end
       
       def max_page_size
-        @model_list_manifest.max_page_size
+        @action_manifest.max_page_size
+      end
+      
+      def order
+        order = @options['order'] || @options[:order] || @action_manifest.order
+        order = 'updated_at' unless ::Manifest.manifest.models[@item_name].attributes[order]
+        @table_name + '.' + order
+      end
+      
+      def direction
+        direction = @options['direction'] || @options[:direction] || @action_manifest.direction
+        if direction < 0
+          'desc'
+        elsif direction > 0
+          'asc'
+        else
+          ''
+        end
+      end
+      
+      def filter_id
+        @options['id'] || @options[:id]
+      end
+      
+      def order_by
+        self.order + ' ' + self.direction
       end
       
       def base_where_condition
@@ -95,21 +91,23 @@ module Locmare
         @includes.includes
       end
       
-      def order
-        @model.list_order
+      def items
+        @items ||= @model.where(
+          self.where_condition
+        ).includes(
+          self.include_hash
+        ).order(
+          self.order_by
+        ).offset(@page_status.offset).limit(@page_status.limit)
       end
       
-      def items operators, options, offset, page_size
-        @model.where(self.where_condition).includes(self.include_hash).order(self.order).offset(offset).limit(page_size)
-      end
-      
-      def count operators, options
-        @model.where(self.where_condition).includes(self.include_hash).count
-      end
-      
-      def paginate count, offset, page_size
-        c = count ? count.to_i : 0
-        Kaminari.paginate_array(Array.new(c, nil)).page(offset).per(page_size)
+      def total
+        @model.where(
+          self.where_condition
+        ).includes(
+          self.include_hash
+        ).count
+        # return string : =>"25"
       end
       
       def boost_manifests
@@ -122,22 +120,11 @@ module Locmare
         manifests = self.boost_manifests
         items.each do |item|
           manifests.each do |boost_manifest|
-            item.boost_manifest
+            item.boost_manifest # ?
           end
         end
       end
       
-      def open operators, options
-        page = self.page_number(options[:page]) 
-        page_size = self.page_size options[:page_size]
-        offset = (page -1) * page_size
-        items = self.items operators, options, offset, page_size
-        count = self.count operators, options
-        pg = self.paginate count, offset, page_size
-        self.boost items
-        ListResult.new self, items, pg, operators, options
-      end
-      
     end
     
   end