OSDN Git Service

merge v04
[pettanr/pettanr.git] / app / models / original_picture.rb
index d178db2..19c993d 100644 (file)
@@ -1,16 +1,16 @@
 class OriginalPicture < ActiveRecord::Base
   belongs_to :artist
-  belongs_to :license
   has_one :resource_picture
   
-  before_destroy :destroy_with_file
+  validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}\r
+  validates :width, :presence => true, :numericality => true, :natural_number => true
+  validates :height, :presence => true, :numericality => true, :natural_number => true
+  validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
+  validates :artist_id, :presence => true, :numericality => true, :existence => true
   
-  def validate
-    errors.add(:filesize, 'size over(1MB)') if self.filesize > 1000000
-  end
+  before_destroy :destroy_with_file
   
   def supply_default art
-    self.license_id = art.default_license_id if self.license_id.blank?
   end
   
   def self.default_page_size
@@ -43,27 +43,33 @@ class OriginalPicture < ActiveRecord::Base
   end
   
   def self.list_opt
-    {:include => [:artist, :license, :resource_picture]}
+    {:include => {:resource_picture => {}}}
   end
   
   def self.list_json_opt
-    {:include => [:resource_picture, :artist, :license]}
+    {:include => {:resource_picture => {}}}
   end
   
-  def self.show cid, author, opt = {}
+  def self.show cid, artist, opt = {}
     pic = OriginalPicture.find(cid, :include => self.show_include_opt(opt))
-    raise ActiveRecord::Forbidden unless pic.own?(author)
+    raise ActiveRecord::Forbidden unless pic.own?(artist)
+    pic
+  end
+  
+  def self.edit cid, artist, opt = {}
+    pic = OriginalPicture.find(cid, :include => self.show_include_opt(opt))
+    raise ActiveRecord::Forbidden unless pic.own?(artist)
     pic
   end
   
   def self.show_include_opt opt = {}
-    res = [:license]
-    res.push(opt[:include]) if opt[:include]
+    res = {:resource_picture => {}}
+    res.merge!(opt[:include]) if opt[:include]
     res
   end
   
   def self.show_json_include_opt
-    {:include => :license}
+    {:include => {:resource_picture => {}}}
   end
   
   def destroy_with_file
@@ -87,27 +93,27 @@ class OriginalPicture < ActiveRecord::Base
     '/original_pictures/' + filename
   end
   
-  def store(picture_data, art, lid = nil)
-    res = false
+  def data_to_mgk picture_data
     begin
       mgk = Magick::Image.from_blob(picture_data).shift
     rescue 
       self.errors.add :base, 'magick failed'
       return false
     end
+    mgk
+  end
+  
+  def store(picture_data, art)
+    mgk = data_to_mgk picture_data
+    return false unless mgk
+    res = false
     self.attributes = {:ext => mgk.format.downcase, :width => mgk.columns, :height => mgk.rows, 
-      :filesize => mgk.filesize, :artist_id => art.id, 
-      :license_id => lid.blank? ? art.default_license_id : lid.to_i
+      :filesize => mgk.filesize, :artist_id => art.id
     }
     OriginalPicture.transaction do
       if res = self.save
         if res = PictureIO.original_picture_io.put(picture_data, self.filename)
-          rp = ResourcePicture.update_picture(self)
-          unless rp.store(mgk)
-            PictureIO.original_picture_io.delete(self.filename)
-            self.errors.add :base, 'resource picture copying error'
-            raise ActiveRecord::Rollback
-          end
+          res = true
         else
           self.errors.add :base, 'original picture io does not work'
           raise ActiveRecord::Rollback
@@ -121,10 +127,9 @@ class OriginalPicture < ActiveRecord::Base
     PictureIO.original_picture_io.get self.filename, subdir
   end
   
-  def own? author
-    return false unless author
-    return false unless author.artist?
-    self.artist_id == author.artist.id
+  def own? art
+    return false unless art
+    self.artist_id == art.id
   end
   
 end