OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / app / models / sheet.rb
index 15991b1..b24e920 100644 (file)
@@ -61,6 +61,7 @@ class Sheet < ActiveRecord::Base
       return false unless reader_role_check(roles)
     end
     return true if self.own?(roles)
+    return true if self.new_record?
     self.visible > 0
   end
   
@@ -261,6 +262,16 @@ class Sheet < ActiveRecord::Base
     Sheet.validate_elements_serial validate_serial_list
   end
   
+  def boost
+    @new_element_index = 0
+    self.panel_elements.each do |elm|
+      if elm.new_record?
+        elm.new_index = @new_element_index
+        @new_element_index += 1
+      end
+    end
+  end 
+  
   def store attr, au
     if attr == false
       self.errors.add :base, I18n.t('errors.invalid_json')
@@ -270,6 +281,10 @@ class Sheet < ActiveRecord::Base
     self.overwrite au
     res = false
     Sheet.transaction do
+      self.sheet_elements.each do |elm|
+        elm.new_sheet = self
+        elm.boost au
+      end
       res = self.save
       unless validate_child
         res = false
@@ -312,10 +327,28 @@ class Sheet < ActiveRecord::Base
     r
   end
   
+  def copyable?
+    r = true
+    Sheet.each_element_class_names do |n|
+      self.elements_by_class_name(n).each do |elm|
+        next if elm.copyable?
+        r = false
+        break
+      end
+      break unless r
+    end
+    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})
+      element_attr = Sheet.class_name_to_class(n).panelize(
+        self.elements_by_class_name(n).map {|elm|
+          elm.copy_attributes.merge elm.panel_attributes
+        }
+      )
+      attr.merge! element_attr
     end
     attr
   end