OSDN Git Service

t#32313:add sheet copy
[pettanr/pettanr.git] / app / models / sheet.rb
index afe4b8d..15991b1 100644 (file)
@@ -312,4 +312,27 @@ class Sheet < ActiveRecord::Base
     r
   end
   
+  def copy
+    attr = self.copy_attributes
+    Sheet.each_element_class_names do |n|
+      attr.merge! Sheet.class_name_to_class(n).panelize(self.elements_by_class_name(n).map {|elm|  elm.copy_attributes})
+    end
+    attr
+  end
+  
+  def copy_attributes
+    r = self.attributes
+    r.delete 'id'
+    r.delete 'author_id'
+    r.delete 'created_at'
+    r.delete 'updated_at'
+    r
+  end
+  
+  def self.panelize sheet
+    attr = sheet.attributes
+    attr.delete 'id'
+    attr
+  end
+  
 end