OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / app / models / panel.rb
index 84a6685..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,186 +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 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
+  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.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)
+  # ground_picture element template 
+  def style_wh
+    {
+      'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px'
+    }
   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)
+  def self.public_list_where
+    'panels.publish > 0'
   end
   
-  def self.himlist 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 = ? and panels.publish > 0', au.id], :order => 'panels.updated_at desc'})
-    Panel.find(:all, opt)
+  def self.list_order
+    'panels.updated_at desc'
   end
   
   def self.list_opt
-    {:include => {
-      :panel_pictures => {
-        :picture => {:artist => {}, :license => {}}
-      }, 
-      :speech_balloons => {:balloons => {}, :speeches => {}},
-      :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 => {:balloons => {}, :speeches => {}},
-      :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 || [])).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 grounds
-    ((self.ground_colors || []) + (self.ground_pictures || [])).compact
-  end
-  
   def panel_elements
-    parts + grounds
-  end
-  
-  @@elm_json_opt = {
-    'PanelPicture' => {
-      :picture => {:artist => {}, :license => {}}
-    }, 
-    'SpeechBalloon' => {:balloons => {}, :speeches => {}},
-    'GroundPicture' => {
-      :picture => {:artist => {}, :license => {}}
-    }, 
-    'GroundColor' => {
-    }, 
-  }
-  
-  def self.elm_json_opt e
-    @@elm_json_opt[e.class.to_s]
+    res = []
+    self.parts_element.each do |e|
+      res[e.t] = e
+    end
+    res
   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
   
@@ -206,6 +127,14 @@ class Panel < ActiveRecord::Base
     '[' + ary.map {|i| i.panel_elements_as_json }.join(',') + ']'
   end
   
+  def new_t
+    self.panel_elements.size
+  end
+  
+  def new_z
+    self.panel_elements.size + 1
+  end
+  
   def scenario
     panel_elements.map { |e|
       e.scenario
@@ -220,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
@@ -232,63 +162,90 @@ class Panel < ActiveRecord::Base
   
   def self.collect_element_value elements, name
     elements.map {|e|
-      e.map {|o|
-        o[name]
-      }
-    }.flatten
+      if e['_destroy'] or e[:_destroy]
+        nil
+      else
+        e[name]
+      end
+    }
   end
   
-  def self.validate_t ary
-    i = 0
-    ary.compact.sort.each do |t|
-      break false unless t == i
+  def self.validate_serial ary, offset = 0
+    i = offset
+    ary.compact.sort.each do |n|
+      break false unless n == i
       i += 1
     end
-    ary.compact.size == i
+    ary.compact.size == i - offset
   end
   
-  def self.validate_element_t elements, name
-    Panel.validate_t(Panel.collect_element_value(elements, name))
+  def self.validate_element_serial elements, name, offset = 0
+    Panel.validate_serial(Panel.collect_element_value(elements, name), offset)
   end
   
-  def self.validate_elements_t c
+  def self.validate_elements_serial c
     c.map {|conf|
-      Panel.validate_element_t(conf[:elements], conf[:name]) ? nil : false
+      Panel.validate_element_serial(conf[:elements], conf[:name], conf[:offset]) ? nil : false
     }.compact.empty?
   end
   
-  def validate_t_list
+  def validate_serial_list
     [
-      {:elements => [self.panel_pictures, self.speech_balloons], :name => :t}
+      {:elements => self.elements_items, :name => :t, :offset => 0}, 
+      {:elements => self.elements_items, :name => :z, :offset => 1}
     ]
   end
   def validate_child
 #    r1 = Panel.validate_elements_id validate_id_list
-    Panel.validate_elements_t validate_t_list
+    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
         self.errors.add :base, I18n.t('errors.invalid_t')
         raise ActiveRecord::Rollback
       end
-      res = self.save
     end
     res
   end
   
+  def remove_element target, operators
+    ct = target.t
+    cz = target.z
+    panel_attributes = {}
+    self.panel_elements.each do |elm|
+      attr = elm.attributes
+      if elm == target
+        attr['_destroy'] = true
+      end
+      if elm.t > ct
+        attr['t']  -= 1 
+      end
+      if elm.z > cz
+        attr['z']  -= 1 
+      end
+      panel_attributes[elm.class.to_s.tableize + '_attributes'] ||= {}
+      panel_attributes[elm.class.to_s.tableize + '_attributes'][elm.id] = attr
+    end
+    self.store(panel_attributes, operators)
+  end
+  
   def destroy_with_elements
     res = false
     Panel.transaction do
-      self.panel_elements.each do |element|
+      self.parts_element.each do |element|
         raise ActiveRecord::Rollback unless element.destroy
       end
       raise ActiveRecord::Rollback unless self.destroy
@@ -297,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|