From a779f3627540f162b83b64484345c5ff16bb871b Mon Sep 17 00:00:00 2001 From: yasushiito Date: Tue, 27 Mar 2012 08:02:43 +0900 Subject: [PATCH] add natural number validater --- app/models/comic.rb | 4 +-- app/models/panel_picture.rb | 3 ++ lib/validators/natural_number_validator.rb | 6 ++++ lib/validators/not_zero_validator.rb | 2 +- spec/models/comic_spec.rb | 8 ++--- spec/models/panel_picture_spec.rb | 51 ++++++++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 lib/validators/natural_number_validator.rb create mode 100644 spec/models/panel_picture_spec.rb diff --git a/app/models/comic.rb b/app/models/comic.rb index 6524e8e1..f4fb836a 100644 --- a/app/models/comic.rb +++ b/app/models/comic.rb @@ -3,8 +3,8 @@ class Comic < ActiveRecord::Base belongs_to :author validates :title, :presence => true, :length => {:maximum => 100} - 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} diff --git a/app/models/panel_picture.rb b/app/models/panel_picture.rb index eaff1304..13e117c1 100644 --- a/app/models/panel_picture.rb +++ b/app/models/panel_picture.rb @@ -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 index 00000000..cd918c2c --- /dev/null +++ b/lib/validators/natural_number_validator.rb @@ -0,0 +1,6 @@ +class NaturalNumberValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + record.errors[attribute] << (options[:message] || "is not natural number") if value.to_i < 1 + end +end + diff --git a/lib/validators/not_zero_validator.rb b/lib/validators/not_zero_validator.rb index 6b34247f..8d4389ba 100644 --- a/lib/validators/not_zero_validator.rb +++ b/lib/validators/not_zero_validator.rb @@ -1,6 +1,6 @@ class NotZeroValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) - record.errors[attribute] << (options[:message] || "is zero") if value == 0 + record.errors[attribute] << (options[:message] || "is zero") if value.to_i == 0 end end diff --git a/spec/models/comic_spec.rb b/spec/models/comic_spec.rb index f1c9744b..88d164b3 100644 --- a/spec/models/comic_spec.rb +++ b/spec/models/comic_spec.rb @@ -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 '負では通る' do + it '負でも失敗する' 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 '負では通る' do + it '負でも失敗する' 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 index 00000000..fbe7cd71 --- /dev/null +++ b/spec/models/panel_picture_spec.rb @@ -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 -- 2.11.0