OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / app / models / panel.rb
index 108681a..bbdf9ed 100644 (file)
@@ -1,8 +1,9 @@
 #コマ
-class Panel < ActiveRecord::Base
+class Panel < Peta::Root
+  load_manifest
   belongs_to :author
-  belongs_to :resource_picture
-  has_many :stories
+  has_many :scroll_panels
+  has_many :sheet_panels
   has_many :panel_pictures, :dependent => :destroy
   has_many :speech_balloons, :dependent => :destroy
   has_many :ground_pictures, :dependent => :destroy
@@ -15,218 +16,106 @@ class Panel < ActiveRecord::Base
   validates :width, :presence => true, :numericality => true, :natural_number => true
   validates :height, :presence => true, :numericality => true, :natural_number => true
   validates :border, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
-  validates :x, :numericality => {:allow_blank => true}
-  validates :y, :numericality => {:allow_blank => true}
-  validates :z, :numericality => {:allow_blank => true, :greater_than => 0}
   validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :publish, :presence => true, :numericality => true
   
-  before_validation :valid_encode
-  
-  def valid_encode
-    ['caption'].each do |a|
-      next if attributes[a] == nil
-      raise Pettanr::BadRequest unless attributes[a].valid_encoding?
-    end
-  end
-  
   def supply_default
     self.border = 2
     self.publish = 0
   end
   
-  def overwrite au
-    self.author_id = au.id
+  def overwrite operators
+    return false unless operators.author
+    self.author_id = operators.author.id
   end
   
-  def own? roles
-    roles = [roles] unless roles.respond_to?(:each)
-    au = Panel.get_author_from_roles roles
-    return false unless au
-    self.author_id == au.id
-  end
-  
-  def visible? roles
-    if MagicNumber['run_mode'] == 0
-      return false unless guest_role_check(roles)
+  def visible? operators
+    case super
+    when nil # super return
+      return true
+    when false
+      return false
     else
-      return false unless reader_role_check(roles)
+      return true if self.new_record?
+      self.publish?
     end
-    return true if self.own?(roles)
-    self.publish?
   end
   
-  def usable? au
-    visible? au
+  def usable? operators
+    self.visible? operators
   end
   
   def publish?
     self.publish > 0
   end
   
-  def tag_id
-    'panel' + self.tag_panel_id
-  end
-  
-  def tag_panel_id
-    self.new_record? ? '0' : self.id.to_s
-  end
-  
-  def field_tag_id f
-    self.tag_id + f.to_s
-  end
-  
-  def field_tree f
-    'panels-' + tag_panel_id + '-' + f.to_s
-  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
+  def style
+    {
+      'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px', 
+      'border-style' => 'solid', 'border-width' => self.border.to_s + 'px', 
+      'border-color' => 'black', 'background-color' => 'white'
+    }
   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
+  # ground_picture element template 
+  def style_wh
+    {
+      'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px'
+    }
   end
   
-  def self.list_where
+  def self.public_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
-    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)
+  def self.list_order
+    'panels.updated_at desc'
   end
   
   def self.list_opt
-    {
-      :panel_pictures => {
-        :picture => {:artist => {}, :license => {}}
-      }, 
-      :speech_balloons => {:balloon => {}, :speech => {}},
-      :ground_pictures => {
-        :picture => {:artist => {}, :license => {}}
-      }, 
-      :ground_colors => {
-      }, 
+    r = {
       :author => {}
     }
-  end
-  
-  def self.show rid, roles
-    opt = {}
-    opt.merge!(Panel.show_opt)
-    res = Panel.find(rid, opt)
-    raise ActiveRecord::Forbidden unless res.visible?(roles)
-    res
-  end
-  
-  def self.edit rid, au
-    opt = {}
-    opt.merge!(Panel.show_opt)
-    res = Panel.find(rid, opt)
-    raise ActiveRecord::Forbidden unless res.own?(au)
-    res
+    self.child_models.each do |child_model|
+      r.merge!(child_model.list_opt_for_panel)
+    end
+    r
   end
   
   def self.show_opt
-    {:include => {
-      :panel_pictures => {
-        :picture => {:artist => {}, :license => {}}
-      }, 
-      :speech_balloons => {:balloon => {}, :speech => {}},
-      :ground_pictures => {
-        :picture => {:artist => {}, :license => {}}
-      }, 
-      :ground_colors => {
-      }, 
+    r = {
       :author => {}
-    }}
+    }
+    self.child_models.each do |child_model|
+      r.merge!(child_model.show_opt_for_panel)
+    end
+    {:include => r}
   end
   
   def parts_element
-    ((self.panel_pictures || []) + (self.speech_balloons || []) + (self.ground_colors || []) + (self.ground_pictures || [])).compact
+    self.elements_items
   end
   
-  def parts
+  def zorderd_elements
     res = []
     self.parts_element.each do |e|
-      res[e.t] = e
+      res[e.z-1] = e
     end
     res
   end
   
-  def zorderd_elements
+  def panel_elements
     res = []
     self.parts_element.each do |e|
-      res[e.z-1] = e
+      res[e.t] = e
     end
     res
   end
   
-  def panel_elements
-    parts
-  end
-  
-  @@elm_json_opt = {
-    'PanelPicture' => {
-      :picture => {:artist => {}, :license => {}}
-    }, 
-    'SpeechBalloon' => {:balloon => {}, :speech => {}},
-    'GroundPicture' => {
-      :picture => {:artist => {}, :license => {}}
-    }, 
-    'GroundColor' => {
-    }, 
-  }
-  
-  def self.elm_json_opt e
-    @@elm_json_opt[e.class.to_s]
-  end
-  
   def elements
     self.panel_elements.map {|e|
       #(-_-;)<... kore wa hidoi
-      JSON.parse e.to_json({:include => Panel.elm_json_opt(e)})
+      JSON.parse e.to_json({:include => e.class.json_opt_for_panel})
     }
   end
   
@@ -239,11 +128,11 @@ class Panel < ActiveRecord::Base
   end
   
   def new_t
-    self.parts.size
+    self.panel_elements.size
   end
   
   def new_z
-    self.parts.size + 1
+    self.panel_elements.size + 1
   end
   
   def scenario
@@ -260,7 +149,8 @@ class Panel < ActiveRecord::Base
   
   def licensed_pictures
     r = {}
-    ((self.panel_pictures || []) + (self.ground_pictures || [])).compact.each do |elm|
+    self.panel_elements.each do |elm|
+      next unless elm.class.has_picture?
       r[elm.picture_id] = elm.picture unless r[elm.picture_id]
     end
     r
@@ -272,14 +162,12 @@ class Panel < ActiveRecord::Base
   
   def self.collect_element_value elements, name
     elements.map {|e|
-      e.map {|o|
-        if o['_destroy'] or o[:_destroy]
-          nil
-        else
-          o[name]
-        end
-      }.compact
-    }.flatten
+      if e['_destroy'] or e[:_destroy]
+        nil
+      else
+        e[name]
+      end
+    }
   end
   
   def self.validate_serial ary, offset = 0
@@ -303,8 +191,8 @@ class Panel < ActiveRecord::Base
   
   def validate_serial_list
     [
-      {:elements => [self.panel_pictures, self.speech_balloons, self.ground_colors, self.ground_pictures], :name => :t, :offset => 0}, 
-      {:elements => [self.panel_pictures, self.speech_balloons, self.ground_colors, self.ground_pictures], :name => :z, :offset => 1}
+      {:elements => self.elements_items, :name => :t, :offset => 0}, 
+      {:elements => self.elements_items, :name => :z, :offset => 1}
     ]
   end
   def validate_child
@@ -312,15 +200,17 @@ class Panel < ActiveRecord::Base
     Panel.validate_elements_serial validate_serial_list
   end
   
-  def store attr, au
+  def store attr, operators
     if attr == false
       self.errors.add :base, I18n.t('errors.invalid_json')
       return false
     end
     self.attributes = attr
-    self.overwrite au
+    self.overwrite operators
     res = false
     Panel.transaction do
+      self.boosts 'post'
+#self.publish = nil
       res = self.save
       unless validate_child
         res = false
@@ -331,7 +221,7 @@ class Panel < ActiveRecord::Base
     res
   end
   
-  def remove_element target, au
+  def remove_element target, operators
     ct = target.t
     cz = target.z
     panel_attributes = {}
@@ -349,7 +239,7 @@ class Panel < ActiveRecord::Base
       panel_attributes[elm.class.to_s.tableize + '_attributes'] ||= {}
       panel_attributes[elm.class.to_s.tableize + '_attributes'][elm.id] = attr
     end
-    self.store(panel_attributes, au)
+    self.store(panel_attributes, operators)
   end
   
   def destroy_with_elements
@@ -364,6 +254,13 @@ class Panel < ActiveRecord::Base
     res
   end
   
+  def self.panelize panel
+    attr = panel.attributes
+    attr.delete 'id'
+    attr
+  end
+  
+  
 =begin
   def self.validate_id ary, pid
     ary.map {|v|