OSDN Git Service

t#31725:add panel inspire
authoryasushiito <yas@pen-chan.jp>
Sat, 20 Jul 2013 09:06:48 +0000 (18:06 +0900)
committeryasushiito <yas@pen-chan.jp>
Sat, 20 Jul 2013 09:06:48 +0000 (18:06 +0900)
app/models/ground_color.rb
app/models/ground_picture.rb
app/models/panel.rb
app/models/panel_picture.rb
app/models/speech_balloon.rb
app/views/panels/show.html.erb
config/environment.rb
lib/inspire.rb [new file with mode: 0644]
spec/models/panel_spec.rb

index 7ff6a1f..810ffb5 100644 (file)
@@ -1,4 +1,5 @@
 class GroundColor < ActiveRecord::Base
+  include ElementInspire
   belongs_to :panel
   belongs_to :color
   
@@ -167,41 +168,6 @@ class GroundColor < ActiveRecord::Base
     self.panel.remove_element(self, au)
   end
   
-  def copy_attributes
-    r = self.attributes
-    r.delete 'id'
-    r.delete 'panel_id'
-    r.delete 'created_at'
-    r.delete 'updated_at'
-    r
-  end
-  
-  def copy to_panel
-    new_ground_color = GroundColor.new self.copy_attributes
-    new_ground_color.t = to_panel.new_t 
-    new_ground_color.z = to_panel.new_z 
-    new_attr = GroundColor.panelize new_ground_color.copy_attributes
-    r = {} #Panel.panelize to_panel
-    r.merge! new_attr
-    r
-  end
-  
-  def self.panelize ground_colors_attributes
-    ground_colors_attributes = [ground_colors_attributes] unless ground_colors_attributes.is_a?(Array)
-    hash = {}
-    index = 0
-    ground_colors_attributes.each do |ground_color_attributes|
-      n = if ground_color_attributes['id']
-        ground_color_attributes['id'].to_s
-      else
-        index += 1
-        'new' + index.to_s 
-      end
-      hash[n] = ground_color_attributes
-    end
-    {self.to_s.tableize + '_attributes' => hash}
-  end
-  
   def scenario
     if caption.blank?
       ''
index 2ce5f4a..e48b5e9 100644 (file)
@@ -1,4 +1,5 @@
 class GroundPicture < ActiveRecord::Base
+  include ElementInspire
   belongs_to :panel
   belongs_to :picture
   
@@ -183,41 +184,6 @@ class GroundPicture < ActiveRecord::Base
     self.panel.remove_element(self, au)
   end
   
-  def copy_attributes
-    r = self.attributes
-    r.delete 'id'
-    r.delete 'panel_id'
-    r.delete 'created_at'
-    r.delete 'updated_at'
-    r
-  end
-  
-  def copy to_panel
-    new_ground_picture = GroundPicture.new self.copy_attributes
-    new_ground_picture.t = to_panel.new_t 
-    new_ground_picture.z = to_panel.new_z 
-    new_attr = GroundPicture.panelize new_ground_picture.copy_attributes
-    r = {} #Panel.panelize to_panel
-    r.merge! new_attr
-    r
-  end
-  
-  def self.panelize ground_pictures_attributes
-    ground_pictures_attributes = [ground_pictures_attributes] unless ground_pictures_attributes.is_a?(Array)
-    hash = {}
-    index = 0
-    ground_pictures_attributes.each do |ground_picture_attributes|
-      n = if ground_picture_attributes['id']
-        ground_picture_attributes['id'].to_s
-      else
-        index += 1
-        'new' + index.to_s 
-      end
-      hash[n] = ground_picture_attributes
-    end
-    {self.to_s.tableize + '_attributes' => hash}
-  end
-  
   def scenario
     if caption.blank?
       ''
index 58a0bcf..dd919d0 100644 (file)
@@ -188,11 +188,6 @@ class Panel < ActiveRecord::Base
   end
   
   def parts
-    res = []
-    self.parts_element.each do |e|
-      res[e.t] = e
-    end
-    res
   end
   
   def zorderd_elements
@@ -204,7 +199,11 @@ class Panel < ActiveRecord::Base
   end
   
   def panel_elements
-    parts
+    res = []
+    self.parts_element.each do |e|
+      res[e.t] = e
+    end
+    res
   end
   
   @@elm_json_opt = {
@@ -239,11 +238,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
@@ -364,32 +363,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
index 89452df..9d11682 100644 (file)
@@ -1,4 +1,5 @@
 class PanelPicture < ActiveRecord::Base
+  include ElementInspire
   belongs_to :panel
   belongs_to :picture
   
@@ -203,41 +204,6 @@ class PanelPicture < ActiveRecord::Base
     self.panel.remove_element(self, au)
   end
   
-  def copy_attributes
-    r = self.attributes
-    r.delete 'id'
-    r.delete 'panel_id'
-    r.delete 'created_at'
-    r.delete 'updated_at'
-    r
-  end
-  
-  def copy to_panel
-    new_panel_picture = PanelPicture.new self.copy_attributes
-    new_panel_picture.t = to_panel.new_t 
-    new_panel_picture.z = to_panel.new_z 
-    new_attr = PanelPicture.panelize new_panel_picture.copy_attributes
-    r = {} #Panel.panelize to_panel
-    r.merge! new_attr
-    r
-  end
-  
-  def self.panelize panel_pictures_attributes
-    panel_pictures_attributes = [panel_pictures_attributes] unless panel_pictures_attributes.is_a?(Array)
-    hash = {}
-    index = 0
-    panel_pictures_attributes.each do |panel_picture_attributes|
-      n = if panel_picture_attributes['id']
-        panel_picture_attributes['id'].to_s
-      else
-        index += 1
-        'new' + index.to_s 
-      end
-      hash[n] = panel_picture_attributes
-    end
-    {self.to_s.tableize + '_attributes' => hash}
-  end
-  
   def scenario
     if caption.blank?
       ''
index 3b7b01e..08f50b8 100644 (file)
@@ -1,4 +1,5 @@
 class SpeechBalloon < ActiveRecord::Base
+  include ElementInspire
   has_one :balloon, :dependent => :destroy
   has_one :speech, :dependent => :destroy
   belongs_to :speech_balloon_template
@@ -168,43 +169,8 @@ class SpeechBalloon < ActiveRecord::Base
     self.panel.remove_element(self, au)
   end
   
-  def copy_attributes
-    r = self.attributes
-    r.delete 'id'
-    r.delete 'panel_id'
-    r.delete 'created_at'
-    r.delete 'updated_at'
-    new_balloon_attr = self.balloon ? Balloon.panelize(self.balloon.copy_attributes) : {}
-    new_speech_attr = self.speech ? Speech.panelize(self.speech.copy_attributes) : {}
-    r.merge! new_balloon_attr
-    r.merge! new_speech_attr
-    r
-  end
-  
-  def copy to_panel
-    new_speech_balloon = SpeechBalloon.new self.copy_attributes
-    new_speech_balloon.t = to_panel.new_t 
-    new_speech_balloon.z = to_panel.new_z 
-    new_attr = SpeechBalloon.panelize new_speech_balloon.copy_attributes
-    r = {} #Panel.panelize to_panel
-    r.merge! new_attr
-    r
-  end
-  
-  def self.panelize speech_balloons_attributes
-    speech_balloons_attributes = [speech_balloons_attributes] unless speech_balloons_attributes.is_a?(Array)
-    hash = {}
-    index = 0
-    speech_balloons_attributes.each do |speech_balloon_attributes|
-      n = if speech_balloon_attributes['id']
-        speech_balloon_attributes['id'].to_s
-      else
-        index += 1
-        'new' + index.to_s 
-      end
-      hash[n] = speech_balloon_attributes
-    end
-    {self.to_s.tableize + '_attributes' => hash}
+  def parts
+    @parts ||= [self.balloon, self.speech]
   end
   
   def scenario
index efd8ba4..f88b6a7 100644 (file)
@@ -21,7 +21,7 @@
 <h2><%= t('panels.show.copy') -%></h2>
 <%= form_for(Panel.new, :html => {:jqform => 'pettanr-panel-form'}) do |f| %>
 
-  <%= hidden_field_tag "json", @panel.copy(@author).to_json %>
+  <%= hidden_field_tag "json", @panel.copy().to_json %>
   <div>
     <%= submit_tag t('panels.show.inspire') -%>
   </div>
index 7ee3cfd..64c5306 100644 (file)
@@ -10,4 +10,5 @@ require 'pettan_imager'
 require 'ar_helper'
 require 'import_result'
 require 'common'
+require 'inspire'
 
diff --git a/lib/inspire.rb b/lib/inspire.rb
new file mode 100644 (file)
index 0000000..ca844ed
--- /dev/null
@@ -0,0 +1,69 @@
+module ElementInspire
+  def self.included(base)
+    base.extend(ClassMethods)
+    base.__send__ :include, InstanceMethods
+  end
+  
+  module ClassMethods
+    def panelize elements_attributes
+      elements_attributes = [elements_attributes] unless elements_attributes.is_a?(Array)
+      hash = {}
+      index = 0
+      elements_attributes.each do |element_attributes|
+        hash[self.to_s.tableize + '_attributes'] ||= {}
+        n = if element_attributes['id']
+          element_attributes['id'].to_s
+        else
+          index += 1
+          'new' + index.to_s 
+        end
+        hash[self.to_s.tableize + '_attributes'][n] = element_attributes
+      end
+      hash
+    end
+  end
+  
+  module InstanceMethods
+    private
+    
+    public
+    
+    def parts
+      @parts ||= []
+    end
+    
+    def copy_attributes
+      r = self.attributes
+      r.delete 'id'
+      r.delete 'panel_id'
+      r.delete 'created_at'
+      r.delete 'updated_at'
+      self.parts.each do |part|
+        new_part_attr = part ? part.class.panelize(part.copy_attributes) : {}
+        r.merge! new_part_attr
+      end
+      r
+    end
+    
+    def copy to_panel
+      new_element = self.class.new self.copy_attributes
+      new_element.t = to_panel.new_t
+      new_element.z = to_panel.new_z
+      new_attr = new_element.class.panelize new_element.copy_attributes
+      r = {} #Panel.panelize to_panel
+      r.merge! new_attr
+      r
+    end
+    
+    def copy_all index
+      new_element = self.class.new self.copy_attributes
+      new_attr = new_element.class.panelize(new_element.copy_attributes, index)
+      r = {} #Panel.panelize to_panel
+      r.merge! new_attr
+      r
+    end
+    
+  end
+  
+end
+
index 7542b20..ec82c05 100644 (file)
@@ -786,54 +786,14 @@ describe Panel do
   end\r
   describe 'コマ部品に於いて' do\r
     before do\r
-      #コマを作成しておく。\r
-      @panel = FactoryGirl.create :panel, :author_id => @author.id\r
-      @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height\r
-      @sb = @panel.speech_balloons.create(\r
-        FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 0)\r
-      )\r
-      @gc = @panel.ground_colors.create(\r
-        FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id, :t => 2)\r
-      )\r
-      @gp = @panel.ground_pictures.create(\r
-        FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 3)\r
-      )\r
-      @panel.reload\r
     end\r
     context 'つつがなく終わるとき' do\r
       it 'コマ部品集合を利用している' do\r
-        Panel.any_instance.stub(:parts_element).with(any_args).and_return([])\r
-        Panel.any_instance.should_receive(:parts_element).with(any_args).exactly(1)\r
         r = @panel.parts\r
       end\r
     end\r
     it 'リストを返している' do\r
       r = @panel.parts\r
-      r.is_a?(Array).should be_true\r
-      r.size.should eq 4\r
-    end\r
-    it 'tでソートしている' do\r
-      r = @panel.parts\r
-      r[0].should eq @sb\r
-      r[1].should eq @pp\r
-      r[2].should eq @gc\r
-      r[3].should eq @gp\r
-    end\r
-    context 'さらに末尾にフキダシを追加したとき' do\r
-      before do\r
-        @sb2 = @panel.speech_balloons.create(\r
-          FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 4)\r
-        )\r
-        @panel.reload\r
-      end\r
-      it 'tでソートしている' do\r
-        r = @panel.parts\r
-        r[0].should eq @sb\r
-        r[1].should eq @pp\r
-        r[2].should eq @gc\r
-        r[3].should eq @gp\r
-        r[4].should eq @sb2\r
-      end\r
     end\r
   end\r
   \r
@@ -923,6 +883,22 @@ describe Panel do
       r[2].should eq @gc\r
       r[3].should eq @gp\r
     end\r
+    context 'さらに末尾にフキダシを追加したとき' do\r
+      before do\r
+        @sb2 = @panel.speech_balloons.create(\r
+          FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 4)\r
+        )\r
+        @panel.reload\r
+      end\r
+      it 'tでソートしている' do\r
+        r = @panel.panel_elements\r
+        r[0].should eq @sb\r
+        r[1].should eq @pp\r
+        r[2].should eq @gc\r
+        r[3].should eq @gp\r
+        r[4].should eq @sb2\r
+      end\r
+    end\r
   end\r
   describe 'コマ要素のjson出力に於いて' do\r
     before do\r