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}
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?
--- /dev/null
+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
+
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
@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
@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
--- /dev/null
+# -*- 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