OSDN Git Service

t#32313:add sheet copy
[pettanr/pettanr.git] / app / models / sheet_panel.rb
index 2612c56..8d6ea69 100644 (file)
@@ -3,8 +3,8 @@ class SheetPanel < ActiveRecord::Base
   belongs_to :panel
   belongs_to :sheet
   
-  validates :sheet_id, :presence => true, :numericality => true, :existence => {:both => false}
-  validates :panel_id, :presence => true, :numericality => true, :existence => {:both => false}
+  validates :sheet_id, :numericality => {:allow_blank => true}
+  validates :panel_id, :numericality => {:allow_blank => true}
   validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :x, :presence => true, :numericality => true
   validates :y, :presence => true, :numericality => true
@@ -191,10 +191,36 @@ class SheetPanel < ActiveRecord::Base
     if self.panel
       self.panel.licensed_pictures
     else
-      []
+      {}
+    end
+  end
+  
+    def self.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
-  
+    
+    def copy_attributes
+      r = self.attributes
+      r.delete 'id'
+      r.delete 'sheet_id'
+      r.delete 'created_at'
+      r.delete 'updated_at'
+      r
+    end
+    
   def self.new_t sheet_id
     r = SheetPanel.max_t(sheet_id)
     r.blank? ? 0 : r.to_i + 1