OSDN Git Service

add natural number validater v03_db_repair
authoryasushiito <yasushiito@git.sourceforge.jp>
Mon, 26 Mar 2012 23:02:43 +0000 (08:02 +0900)
committeryasushiito <yasushiito@git.sourceforge.jp>
Mon, 26 Mar 2012 23:02:43 +0000 (08:02 +0900)
app/models/comic.rb
app/models/panel_picture.rb
lib/validators/natural_number_validator.rb [new file with mode: 0644]
lib/validators/not_zero_validator.rb
spec/models/comic_spec.rb
spec/models/panel_picture_spec.rb [new file with mode: 0644]

index 6524e8e..f4fb836 100644 (file)
@@ -3,8 +3,8 @@ class Comic < ActiveRecord::Base
   belongs_to :author
   
   validates :title, :presence => true, :length => {:maximum => 100}\r
-  validates :width, :presence => true, :numericality => true, :not_zero => true
-  validates :height, :presence => true, :numericality => true, :not_zero => true
+  validates :width, :presence => true, :numericality => true, :natural_number => true
+  validates :height, :presence => true, :numericality => true, :natural_number => true
   validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..3}
   validates :editable, :presence => true, :numericality => true, :inclusion => {:in => 0..3}
   
index eaff130..13e117c 100644 (file)
@@ -2,6 +2,9 @@ class PanelPicture < ActiveRecord::Base
   belongs_to :panel
   belongs_to :resource_picture
   
+  validates :width, :presence => true, :numericality => true, :not_zero => true
+  validates :height, :presence => true, :numericality => true, :not_zero => true
+  
   def flip
     res = (self.height < 0 ? '' : 'v') + (self.width == 0 ? '' : 'h')
     res += '/' unless res.empty?
diff --git a/lib/validators/natural_number_validator.rb b/lib/validators/natural_number_validator.rb
new file mode 100644 (file)
index 0000000..cd918c2
--- /dev/null
@@ -0,0 +1,6 @@
+class NaturalNumberValidator < ActiveModel::EachValidator\r
+  def validate_each(record, attribute, value)\r
+    record.errors[attribute] << (options[:message] || "is not natural number") if value.to_i < 1\r
+  end\r
+end\r
+
index 6b34247..8d4389b 100644 (file)
@@ -1,6 +1,6 @@
 class NotZeroValidator < ActiveModel::EachValidator\r
   def validate_each(record, attribute, value)\r
-    record.errors[attribute] << (options[:message] || "is zero") if value == 0\r
+    record.errors[attribute] << (options[:message] || "is zero") if value.to_i == 0\r
   end\r
 end\r
 
index f1c9744..88d164b 100644 (file)
@@ -37,9 +37,9 @@ describe Comic do
         @comic = Factory.build :normal_comic, :author_id => @author.id, :width => 0
         @comic.should_not be_valid
       end
-      it 'è² ã\81§ã\81¯é\80\9aる' do
+      it 'è² ã\81§ã\82\82失æ\95\97ã\81\99る' do
         @comic = Factory.build :normal_comic, :author_id => @author.id, :width => -1
-        @comic.should be_valid
+        @comic.should_not be_valid
       end
       it '正なら通る' do
         @comic = Factory.build :normal_comic, :author_id => @author.id, :width => 1
@@ -55,9 +55,9 @@ describe Comic do
         @comic = Factory.build :normal_comic, :author_id => @author.id, :height => 0
         @comic.should_not be_valid
       end
-      it 'è² ã\81§ã\81¯é\80\9aる' do
+      it 'è² ã\81§ã\82\82失æ\95\97ã\81\99る' do
         @comic = Factory.build :normal_comic, :author_id => @author.id, :height => -1
-        @comic.should be_valid
+        @comic.should_not be_valid
       end
       it '正なら通る' do
         @comic = Factory.build :normal_comic, :author_id => @author.id, :height => 1
diff --git a/spec/models/panel_picture_spec.rb b/spec/models/panel_picture_spec.rb
new file mode 100644 (file)
index 0000000..fbe7cd7
--- /dev/null
@@ -0,0 +1,51 @@
+# -*- encoding: utf-8 -*-
+require 'spec_helper'
+
+describe PanelPicture do
+  describe '検証に於いて' do
+    before do
+    end
+    
+    it 'オーソドックスなデータなら通る' do
+      @comic = Factory.build :normal_comic, :author_id => @author.id
+      @comic.should be_valid
+    end
+    
+    context 'widthを検証するとき' do
+      it 'nullなら失敗する' do
+        @comic = Factory.build :normal_comic, :author_id => @author.id, :width => nil
+        @comic.should_not be_valid
+      end
+      it '0なら失敗する' do
+        @comic = Factory.build :normal_comic, :author_id => @author.id, :width => 0
+        @comic.should_not be_valid
+      end
+      it '負では通る' do
+        @comic = Factory.build :normal_comic, :author_id => @author.id, :width => -1
+        @comic.should be_valid
+      end
+      it '正なら通る' do
+        @comic = Factory.build :normal_comic, :author_id => @author.id, :width => 1
+        @comic.should be_valid
+      end
+    end
+    context 'heightを検証するとき' do
+      it 'nullなら失敗する' do
+        @comic = Factory.build :normal_comic, :author_id => @author.id, :height => nil
+        @comic.should_not be_valid
+      end
+      it '0なら失敗する' do
+        @comic = Factory.build :normal_comic, :author_id => @author.id, :height => 0
+        @comic.should_not be_valid
+      end
+      it '負では通る' do
+        @comic = Factory.build :normal_comic, :author_id => @author.id, :height => -1
+        @comic.should be_valid
+      end
+      it '正なら通る' do
+        @comic = Factory.build :normal_comic, :author_id => @author.id, :height => 1
+        @comic.should be_valid
+      end
+    end
+  end
+end