OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / app / models / panel.rb
index c383e1a..d19cc62 100644 (file)
-class Panel < ActiveRecord::Base
-  belongs_to :comic
+#コマ
+class Panel < Peta::Root
+  load_manifest
   belongs_to :author
+  has_many :scroll_panels
+  has_many :sheet_panels
   has_many :panel_pictures, :dependent => :destroy
-  has_many :balloons, :dependent => :destroy
+  has_many :speech_balloons, :dependent => :destroy
+  has_many :ground_pictures, :dependent => :destroy
+  has_many :ground_colors, :dependent => :destroy
   accepts_nested_attributes_for :panel_pictures, :allow_destroy => true
-  accepts_nested_attributes_for :balloons, :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
 
-  def self.max_t comic_id
-    Panel.maximum(:t, :conditions => ['comic_id = ?', comic_id]).to_i
+  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 :author_id, :presence => true, :numericality => true, :existence => {:both => false}
+  validates :publish, :presence => true, :numericality => true
+  
+  def supply_default
+    self.border = 2
+    self.publish = 0
   end
   
-  def self.find_t comic_id, t
-    Panel.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
+  def overwrite operators
+    return false unless operators.author
+    self.author_id = operators.author.id
   end
-
-  #更新する時にPanelIDをチェックしとかないと勝手に所属先を変えられるゾ!?
-
-  def vdt_save
-    f = nil
-    max_t = Panel.max_t self.comic_id
-    f = Panel.find_t(self.comic_id, self.t) if self.t
-    if f
-      Panel.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
+  
+  def visible? operators
+    case super
+    when nil # super return
+      return true
+    when false
+      return false
     else
-      self.t = max_t + 1
+      return true if self.new_record?
+      self.publish?
     end
-    self.save
   end
   
-  def move_to new_t
-    return true if self.t == new_t
-    if self.t > new_t
-      Panel.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, new_t, self.t])
-    else
-      nf = Panel.find_t(self.comic_id, new_t)
-      max_t = Panel.max_t self.comic_id
-      new_t = max_t if new_t > max_t
-      Panel.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, self.t, new_t])
-    end
-    self.t = new_t
-    self.save
+  def usable? operators
+    self.visible? operators
+  end
+  
+  def publish?
+    self.publish > 0
+  end
+  
+  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 destroy_and_shorten
-    Panel.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
-    self.destroy
+  # ground_picture element template 
+  def style_wh
+    {
+      'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px'
+    }
   end
   
-  def own? author
-    return false unless author
-    self.author_id == author.id
+  def self.public_list_where
+    'panels.publish > 0'
   end
   
-  def sort_by_time
-    pe = []
-    self.panel_pictures.each do |picture|
-      pe[picture.t] = picture
+  def self.list_order
+    'panels.updated_at desc'
+  end
+  
+  def self.list_opt
+    r = {
+      :author => {}
+    }
+    self.child_models.each do |child_model|
+      r.merge!(child_model.list_opt_for_panel)
     end
-    self.balloons.each do |balloon|
-      pe[balloon.t] = balloon
+    r
+  end
+  
+  def self.show_opt
+    r = {
+      :author => {}
+    }
+    self.child_models.each do |child_model|
+      r.merge!(child_model.show_opt_for_panel)
     end
-    pe
+    {:include => r}
   end
   
-  def each_element
-    self.sort_by_time.each do |e|
-      yield e
+  def parts_element
+    self.elements_items
+  end
+  
+  def zorderd_elements
+    res = []
+    self.parts_element.each do |e|
+      res[e.z-1] = e
     end
+    res
   end
   
   def panel_elements
     res = []
-    self.each_element do |elm|
-      if elm.kind_of?(PanelPicture)
-        res[elm.t] = elm.to_json({:include => :resource_picture})
+    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 => e.class.json_opt_for_panel})
+    }
+  end
+  
+  def panel_elements_as_json
+    self.to_json({:include => {:author => {}}, :methods => :elements})
+  end
+  
+  def self.list_as_json_text ary
+    '[' + 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
+    }.join
+  end
+  
+  def plain_scenario
+    panel_elements.map { |e|
+      e.plain_scenario
+    }.join
+  end
+  
+  def licensed_pictures
+    r = {}
+    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
+  end
+  
+  def self.visible_count
+    Panel.count
+  end
+  
+  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
+  end
+  
+  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 - offset
+  end
+  
+  def self.validate_element_serial elements, name, offset = 0
+    Panel.validate_serial(Panel.collect_element_value(elements, name), offset)
+  end
+  
+  def self.validate_elements_serial c
+    c.map {|conf|
+      Panel.validate_element_serial(conf[:elements], conf[:name], conf[:offset]) ? nil : false
+    }.compact.empty?
+  end
+  
+  def validate_serial_list
+    [
+      {: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_serial validate_serial_list
+  end
+  
+  def store attr, operators
+    if attr == false
+      self.errors.add :base, I18n.t('errors.invalid_json')
+      return false
+    end
+    self.attributes = attr
+    self.overwrite operators
+    res = false
+    Panel.transaction do
+      self.panel_elements.each do |elm|
+        elm.boost
+      end
+#self.publish = nil
+      res = self.save
+      unless validate_child
+        res = false
+        self.errors.add :base, I18n.t('errors.invalid_t')
+        raise ActiveRecord::Rollback
+      end
+    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
-      if elm.kind_of?(Balloon)
-        res[elm.t] = elm.to_json({:include => :speaches})
+      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.parts_element.each do |element|
+        raise ActiveRecord::Rollback unless element.destroy
       end
+      raise ActiveRecord::Rollback unless self.destroy
+      res = true
     end
     res
   end
   
-  def to_json_play
-    self.to_json :methods => :panel_elements
+  def self.panelize panel
+    attr = panel.attributes
+    attr.delete 'id'
+    attr
   end
+  
+  
+=begin
+  def self.validate_id ary, pid
+    ary.map {|v|
+      if pid
+        (v == pid or v == nil) ? nil : false
+      else
+        v ? false : nil
+      end
+    }.compact.empty?
+  end
+  
+  def self.validate_element_id elements, name, pid
+    Panel.validate_id(Panel.collect_element_value(elements, name), pid)
+  end
+  
+  def self.validate_elements_id c
+    c.map {|conf|
+      Panel.validate_element_id(conf[:elements], conf[:name], conf[:parent_id]) ? nil : false
+    }.compact.empty?
+  end
+  
+  def validate_id_list
+    r = self.speech_balloons.map {|sb|
+      {:elements => [sb.speeches, sb.balloons], :name => :speech_balloon_id, :parent_id => sb.id}
+    }
+    r.unshift({:elements => [self.panel_pictures, self.speech_balloons], :name => :panel_id, :parent_id => self.id})
+    r
+  end
+=end
+  
 end