OSDN Git Service

t#32025:comic rename
[pettanr/pettanr.git] / app / models / balloon.rb
index 391b933..fbfc8bf 100644 (file)
 class Balloon < ActiveRecord::Base
-  has_many :speeches
-  belongs_to :panel
+  include ElementPart
+  belongs_to :speech_balloon
   belongs_to :system_picture
-  accepts_nested_attributes_for :speeches
+  
+  validates :speech_balloon_id, :numericality => {:allow_blank => 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, :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? roles
+    if MagicNumber['run_mode'] == 0
+      return false unless guest_role_check(roles)
+    else
+      return false unless reader_role_check(roles)
+    end
+    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 get_parent
+    self.speech_balloon || @new_parent
+  end
+  
+  def tag_element_part_type
+    'balloon'
+  end
+  
+  def self.default_page_size
+    25
+  end
+  
+  def self.max_page_size
+    100
+  end
+  
+  def self.page prm = nil
+    page = prm.to_i
+    page = 1 if page < 1
+    page
+  end
+  
+  def self.page_size prm = self.default_page_size
+    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
+  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)
+  end
+  
+  def self.list_opt
+    {:include => {:speech_balloon => {:panel => {:author => {}}, :speech => {}, :speech_balloon_template => {} }}}
+  end
+  
+  def self.list_json_opt
+    {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :speech => {}, :speech_balloon_template => {} }}}}
+  end
+  
+  def self.show cid, au
+    opt = {}
+    opt.merge!(Balloon.show_opt)
+    res = Balloon.find(cid, opt)
+    raise ActiveRecord::Forbidden unless res.visible?(au)
+    res
+  end
+  
+  def self.show_opt
+    {:include => {:speech_balloon => {:panel => {:author => {}}, :speech => {}, :speech_balloon_template => {} }}}
+  end
+  
+  def self.show_json_opt
+    {: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