OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / app / models / balloon.rb
index 5c15e63..110a3aa 100644 (file)
@@ -1,30 +1,68 @@
 class Balloon < ActiveRecord::Base
+  include ElementPart
   belongs_to :speech_balloon
   belongs_to :system_picture
   
   validates :speech_balloon_id, :numericality => {:allow_blank => true}
-  validates :system_picture_id, :presence => true, :numericality => true, :existence => true
+  validates :system_picture_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :x, :presence => true, :numericality => true
   validates :y, :presence => true, :numericality => true
   validates :width, :presence => true, :numericality => true, :natural_number => true
   validates :height, :presence => true, :numericality => true, :natural_number => true
+  validates :r, :presence => true, :numericality => true
 #  validates :caption, :presence => true
-#  validates :settings, :presence => true
+  validates :settings, :extend_balloon => true
 
+  before_validation :valid_encode
+  
+  def valid_encode
+    ['settings'].each do |a|
+      next if attributes[a] == nil
+      raise Pettanr::BadRequest unless attributes[a].valid_encoding?
+    end
+  end
+  
+  def self.colum_structures
+    @@colum_structures ||= {
+      :r => {
+        :helper => 'panels/tail_angle_helper'
+      }
+    }
+  end
+  
   def url
     '/system_pictures/' + self.system_picture.filename
   end
   
-  def visible? au
-    if au == nil
-      return false if MagicNumber['run_mode'] == 1
-    elsif au.is_a?(Author)
-    elsif au.is_a?(Admin)
-      return true
+  def visible? roles
+    if MagicNumber['run_mode'] == 0
+      return false unless guest_role_check(roles)
     else
-      return false
+      return false unless reader_role_check(roles)
     end
-    self.speech_balloon.visible? au
+    return true if self.speech_balloon.panel.own?(roles)
+    self.speech_balloon.panel.visible? roles
+  end
+  
+  def supply_default
+    self.x = 0
+    self.y = 0
+    self.width = 100
+    self.height = 100
+    self.r = 0
+self.system_picture_id = 1
+  end
+  
+  def symbol_option
+    self.get_parent.speech_balloon_template.symbol_option
+  end
+  
+  def get_parent
+    self.speech_balloon || @new_parent
+  end
+  
+  def tag_element_part_type
+    'balloon'
   end
   
   def self.default_page_size
@@ -48,20 +86,24 @@ class Balloon < ActiveRecord::Base
     page_size
   end
   
+  def self.list_where
+    'panels.publish > 0'
+  end
+  
   def self.list page = 1, page_size = self.default_page_size
-    opt = {}
-    opt.merge!(Balloon.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => 'panels.publish > 0', :order => 'balloons.updated_at desc'})
-    Balloon.find(:all, opt)
+    Balloon.where(self.list_where()).includes(Balloon.list_opt).order('balloons.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(Balloon.where(self.list_where()).includes(Balloon.list_opt).count, nil)).page(page).per(page_size)
   end
   
   def self.list_opt
-    {:include => {:speech_balloon => {:panel => {:author => {}}, :speeches => {}, :speech_balloon_template => {} }}}
+    {:speech_balloon => {:panel => {:author => {}}, :speech => {}, :speech_balloon_template => {} }}
   end
   
   def self.list_json_opt
-    {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speeches => {}, :speech_balloon_template => {} }}}}
+    {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speech => {}, :speech_balloon_template => {} }}}}
   end
   
   def self.show cid, au
@@ -73,11 +115,30 @@ class Balloon < ActiveRecord::Base
   end
   
   def self.show_opt
-    {:include => {:speech_balloon => {:panel => {:author => {}}, :speeches => {}, :speech_balloon_template => {} }}}
+    {:include => {:speech_balloon => {:panel => {:author => {}}, :speech => {}, :speech_balloon_template => {} }}}
   end
   
   def self.show_json_opt
-    {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speeches => {}, :speech_balloon_template => {} }}}}
+    {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speech => {}, :speech_balloon_template => {} }}}}
+  end
+  
+  def copy_attributes
+    r = self.attributes
+    r.delete 'id'
+    r.delete 'speech_balloon_id'
+    r.delete 'created_at'
+    r.delete 'updated_at'
+    r
+  end
+  
+  def self.panelize balloon_attributes
+    {'balloon_attributes' => balloon_attributes}
+  end
+  
+  def scenario
+  end
+  
+  def plain_scenario
   end
   
 end