OSDN Git Service

t#31687:fiix remove_element
authoryasushiito <yas@pen-chan.jp>
Mon, 8 Jul 2013 23:31:34 +0000 (08:31 +0900)
committeryasushiito <yas@pen-chan.jp>
Mon, 8 Jul 2013 23:31:34 +0000 (08:31 +0900)
Gemfile
app/models/ground_color.rb
app/models/ground_picture.rb
app/models/panel.rb
app/models/panel_picture.rb
app/models/speech_balloon.rb

diff --git a/Gemfile b/Gemfile
index 6288973..f654fe9 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -33,7 +33,7 @@ gem 'jquery-rails'
 gem 'jquery-ui-rails'
 
 # To use ActiveModel has_secure_password
-gem 'bcrypt-ruby', '~> 3.0.0'
+gem 'bcrypt-ruby', '~> 3.0.0'
 
 # Use unicorn as the web server
 # gem 'unicorn'
index 3f5be72..5d50f96 100644 (file)
@@ -164,22 +164,7 @@ class GroundColor < ActiveRecord::Base
   end
   
   def remove au
-    d = false
-    ground_colors_attributes = {}
-    self.panel.ground_colors.each do |ground_color|
-      attr = ground_color.attributes
-      if ground_color == self
-        attr['_destroy'] = true
-        d = true
-      else
-        if d
-          attr['t']  -= 1 
-        end
-      end
-      ground_colors_attributes[ground_color.id] = attr
-    end
-    self.panel.attributes = {:ground_colors_attributes => ground_colors_attributes}
-    self.panel.store({}, au)
+    self.panel.remove_element(self, au)
   end
   
   def scenario
index 9f36b75..4bcd683 100644 (file)
@@ -180,22 +180,7 @@ class GroundPicture < ActiveRecord::Base
   end
   
   def remove au
-    d = false
-    ground_pictures_attributes = {}
-    self.panel.ground_pictures.each do |ground_picture|
-      attr = ground_picture.attributes
-      if ground_picture == self
-        attr['_destroy'] = true
-        d = true
-      else
-        if d
- #         attr['t']  -= 1 
-        end
-      end
-      ground_pictures_attributes[ground_picture.id] = attr
-    end
-    self.panel.attributes = {:ground_pictures_attributes => ground_pictures_attributes}
-    self.panel.store({}, au)
+    self.panel.remove_element(self, au)
   end
   
   def scenario
index 8970687..108681a 100644 (file)
@@ -331,6 +331,27 @@ class Panel < ActiveRecord::Base
     res
   end
   
+  def remove_element target, au
+    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, au)
+  end
+  
   def destroy_with_elements
     res = false
     Panel.transaction do
index 57a3a4b..cef80fe 100644 (file)
@@ -200,22 +200,7 @@ class PanelPicture < ActiveRecord::Base
   end
   
   def remove au
-    d = false
-    panel_pictures_attributes = {}
-    self.panel.panel_pictures.each do |panel_picture|
-      attr = panel_picture.attributes
-      if panel_picture == self
-        attr['_destroy'] = true
-        d = true
-      else
-        if d
-          attr['t']  -= 1 
-        end
-      end
-      panel_pictures_attributes[panel_picture.id] = attr
-    end
-    self.panel.attributes = {:panel_pictures_attributes => panel_pictures_attributes}
-    self.panel.store({}, au)
+    self.panel.remove_element(self, au)
   end
   
   def scenario
index ff6716d..d8e6e31 100644 (file)
@@ -165,22 +165,7 @@ class SpeechBalloon < ActiveRecord::Base
   end
   
   def remove au
-    d = false
-    speech_balloons_attributes = {}
-    self.panel.speech_balloons.each do |speech_balloon|
-      attr = speech_balloon.attributes
-      if speech_balloon == self
-        attr['_destroy'] = true
-        d = true
-      else
-        if d
-          attr['t']  -= 1 
-        end
-      end
-      speech_balloons_attributes[speech_balloon.id] = attr
-    end
-    self.panel.attributes = {:speech_balloons_attributes => speech_balloons_attributes}
-    self.panel.store({}, au)
+    self.panel.remove_element(self, au)
   end
   
   def scenario