OSDN Git Service

t#31779:element lib
[pettanr/pettanr.git] / app / models / panel.rb
index 58a0bcf..6751428 100644 (file)
@@ -30,6 +30,27 @@ class Panel < ActiveRecord::Base
     end
   end
   
+  def self.each_element_class_names
+    Pettanr::Application.elements.each do |k, n|
+      yield k
+    end
+  end
+  
+  def self.class_name_to_class k
+    Object.const_get k
+  end
+  
+  def self.each_element_classes
+    self.each_element_class_names do |k|
+      e = self.class_name_to_class k
+      yield e
+    end
+  end
+  
+  def elements_by_class_name class_name
+    self.__send__ class_name.tableize
+  end
+  
   def supply_default
     self.border = 2
     self.publish = 0
@@ -138,18 +159,13 @@ class Panel < ActiveRecord::Base
   end
   
   def self.list_opt
-    {
-      :panel_pictures => {
-        :picture => {:artist => {}, :license => {}}
-      }, 
-      :speech_balloons => {:balloon => {}, :speech => {}},
-      :ground_pictures => {
-        :picture => {:artist => {}, :license => {}}
-      }, 
-      :ground_colors => {
-      }, 
+    r = {
       :author => {}
     }
+    self.each_element_classes do |e|
+      r.merge!(e.list_opt_for_panel)
+    end
+    r
   end
   
   def self.show rid, roles
@@ -169,30 +185,21 @@ class Panel < ActiveRecord::Base
   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.each_element_classes do |e|
+      r.merge!(e.show_opt_for_panel)
+    end
+    {:include => r}
   end
   
   def parts_element
-    ((self.panel_pictures || []) + (self.speech_balloons || []) + (self.ground_colors || []) + (self.ground_pictures || [])).compact
-  end
-  
-  def parts
-    res = []
-    self.parts_element.each do |e|
-      res[e.t] = e
+    r = []
+    self.class.each_element_class_names do |k|
+      r += (self.elements_by_class_name(k) || [])
     end
-    res
+    r
   end
   
   def zorderd_elements
@@ -204,29 +211,17 @@ class Panel < ActiveRecord::Base
   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]
+    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
   
@@ -239,11 +234,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 +255,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
@@ -302,9 +298,13 @@ class Panel < ActiveRecord::Base
   end
   
   def validate_serial_list
+    l = []
+    self.class.each_element_class_names do |k|
+      l << self.elements_by_class_name(k)
+    end
     [
-      {: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 => l, :name => :t, :offset => 0}, 
+      {:elements => l, :name => :z, :offset => 1}
     ]
   end
   def validate_child
@@ -364,32 +364,14 @@ class Panel < ActiveRecord::Base
     res
   end
   
-  def copy au
-    attr = self.attributes
-    attr.delete 'id'
-    self.panel_elements.each_with_index do |elm, index|
-      elm_attr = elm.attributes
-      elm_attr.delete 'id'
-      elm_attr.delete 'panel_id'
-      elm_attr.delete 'created_at'
-      elm_attr.delete 'updated_at'
-      case elm.class.to_s
-      when 'PanelPicture'
-      when 'SpeechBalloon'
-        balloon_attr = elm.balloon.attributes
-        balloon_attr.delete 'id'
-        balloon_attr.delete 'speech_balloon_id'
-        elm_attr['balloon_attributes'] = balloon_attr
-        speech_attr = elm.speech.attributes
-        speech_attr.delete 'id'
-        speech_attr.delete 'speech_balloon_id'
-        elm_attr['speech_attributes'] = speech_attr
-      when 'GroundPicture'
-      when 'GroundColor'
-      end
-      attr[elm.class.to_s.tableize + '_attributes'] ||= {}
-      attr[elm.class.to_s.tableize + '_attributes'][index] = elm_attr
-    end
+  def copy
+    attr = self.copy_attributes
+    attr.merge! PanelPicture.panelize(self.panel_pictures.map {|elm|  elm.copy_attributes})
+    attr.merge! SpeechBalloon.panelize(self.speech_balloons.map {|elm|  elm.copy_attributes})
+    attr.merge! GroundPicture.panelize(self.ground_pictures.map {|elm|  elm.copy_attributes})
+    attr.merge! GroundColor.panelize(self.ground_colors.map {|elm|  elm.copy_attributes})
+    p attr
+    attr
   end
   
   def copy_attributes