OSDN Git Service

t#31470:create pager
[pettanr/pettanr.git] / app / models / panel.rb
index 421a279..d7dbfc0 100644 (file)
@@ -7,12 +7,10 @@ class Panel < ActiveRecord::Base
   has_many :speech_balloons, :dependent => :destroy
   has_many :ground_pictures, :dependent => :destroy
   has_many :ground_colors, :dependent => :destroy
-  has_many :panel_colors, :dependent => :destroy
   accepts_nested_attributes_for :panel_pictures, :allow_destroy => true
   accepts_nested_attributes_for :speech_balloons, :allow_destroy => true
   accepts_nested_attributes_for :ground_pictures, :allow_destroy => true
   accepts_nested_attributes_for :ground_colors, :allow_destroy => true
-  accepts_nested_attributes_for :panel_colors, :allow_destroy => true
 
   validates :width, :presence => true, :numericality => true, :natural_number => true
   validates :height, :presence => true, :numericality => true, :natural_number => true
@@ -87,16 +85,44 @@ class Panel < ActiveRecord::Base
     page_size
   end
   
+  def self.list_where
+    'panels.publish > 0'
+  end
+  
+  def self.mylist_where au
+    ['panels.author_id = ?', au.id]
+  end
+  
+  def self.himlist_where au
+    ['panels.author_id = ? and panels.publish > 0', au.id]
+  end
+  
   def self.list page = 1, page_size = self.default_page_size
-    opt = {}
-    opt.merge!(self.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => 'panels.publish > 0', :order => 'panels.updated_at desc'})
-    Panel.find(:all, opt)
+    Panel.where(self.list_where()).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.mylist au, page = 1, page_size = Author.default_panel_page_size
+    Panel.where(self.mylist_where(au)).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.himlist au, page = 1, page_size = Author.default_panel_page_size
+    Panel.where(self.himlist_where(au)).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.list_paginate page = 1, page_size = self.default_page_size
+    Kaminari.paginate_array(Array.new(Panel.where(self.list_where()).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.mylist_paginate au, page = 1, page_size = Author.default_panel_page_size
+    Kaminari.paginate_array(Array.new(Panel.where(self.mylist_where(au)).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.himlist_paginate au, page = 1, page_size = Author.default_panel_page_size
+    Kaminari.paginate_array(Array.new(Panel.where(self.himlist_where(au)).count, nil)).page(page).per(page_size)
   end
   
   def self.list_opt
-    {:include => {
+    {
       :panel_pictures => {
         :picture => {:artist => {}, :license => {}}
       }, 
@@ -105,20 +131,9 @@ class Panel < ActiveRecord::Base
         :picture => {:artist => {}, :license => {}}
       }, 
       :ground_colors => {
-        :color => {}
-      }, 
-      :panel_colors => {
       }, 
       :author => {}
-    }}
-  end
-  
-  def self.mylist au, page = 1, page_size = Author.default_panel_page_size
-    opt = {}
-    opt.merge!(self.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'panels.updated_at desc'})
-    Panel.find(:all, opt)
+    }
   end
   
   def self.show rid, roles
@@ -147,9 +162,6 @@ class Panel < ActiveRecord::Base
         :picture => {:artist => {}, :license => {}}
       }, 
       :ground_colors => {
-        :color => {}
-      }, 
-      :panel_colors => {
       }, 
       :author => {}
     }}
@@ -168,7 +180,7 @@ class Panel < ActiveRecord::Base
   end
   
   def grounds
-    ((self.ground_colors || []) + (self.ground_pictures || []) + (self.panel_colors || [])).compact
+    ((self.ground_colors || []) + (self.ground_pictures || [])).compact
   end
   
   def panel_elements
@@ -184,10 +196,7 @@ class Panel < ActiveRecord::Base
       :picture => {:artist => {}, :license => {}}
     }, 
     'GroundColor' => {
-      :color => {}
     }, 
-    'PanelColor' => {
-    } 
   }
   
   def self.elm_json_opt e
@@ -236,8 +245,12 @@ class Panel < ActiveRecord::Base
   def self.collect_element_value elements, name
     elements.map {|e|
       e.map {|o|
-        o[name]
-      }
+        if o['_destroy'] or o[:_destroy]
+          nil
+        else
+          o[name]
+        end
+      }.compact
     }.flatten
   end
   
@@ -279,11 +292,11 @@ class Panel < ActiveRecord::Base
     self.overwrite au
     res = false
     Panel.transaction do
+      res = self.save
       unless validate_child
         self.errors.add :base, I18n.t('errors.invalid_t')
         raise ActiveRecord::Rollback
       end
-      res = self.save
     end
     res
   end