• R/O
  • HTTP
  • SSH
  • HTTPS

pettanr: コミット

サーバ側、Railsシステム本体


コミットメタ情報

リビジョンa779f3627540f162b83b64484345c5ff16bb871b (tree)
日時2012-03-27 08:02:43
作者yasushiito <yasushiito@git....>
コミッターyasushiito

ログメッセージ

add natural number validater

変更サマリ

差分

--- a/app/models/comic.rb
+++ b/app/models/comic.rb
@@ -3,8 +3,8 @@ class Comic < ActiveRecord::Base
33 belongs_to :author
44
55 validates :title, :presence => true, :length => {:maximum => 100}
6- validates :width, :presence => true, :numericality => true, :not_zero => true
7- validates :height, :presence => true, :numericality => true, :not_zero => true
6+ validates :width, :presence => true, :numericality => true, :natural_number => true
7+ validates :height, :presence => true, :numericality => true, :natural_number => true
88 validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..3}
99 validates :editable, :presence => true, :numericality => true, :inclusion => {:in => 0..3}
1010
--- a/app/models/panel_picture.rb
+++ b/app/models/panel_picture.rb
@@ -2,6 +2,9 @@ class PanelPicture < ActiveRecord::Base
22 belongs_to :panel
33 belongs_to :resource_picture
44
5+ validates :width, :presence => true, :numericality => true, :not_zero => true
6+ validates :height, :presence => true, :numericality => true, :not_zero => true
7+
58 def flip
69 res = (self.height < 0 ? '' : 'v') + (self.width == 0 ? '' : 'h')
710 res += '/' unless res.empty?
--- /dev/null
+++ b/lib/validators/natural_number_validator.rb
@@ -0,0 +1,6 @@
1+class NaturalNumberValidator < ActiveModel::EachValidator
2+ def validate_each(record, attribute, value)
3+ record.errors[attribute] << (options[:message] || "is not natural number") if value.to_i < 1
4+ end
5+end
6+
--- a/lib/validators/not_zero_validator.rb
+++ b/lib/validators/not_zero_validator.rb
@@ -1,6 +1,6 @@
11 class NotZeroValidator < ActiveModel::EachValidator
22 def validate_each(record, attribute, value)
3- record.errors[attribute] << (options[:message] || "is zero") if value == 0
3+ record.errors[attribute] << (options[:message] || "is zero") if value.to_i == 0
44 end
55 end
66
--- a/spec/models/comic_spec.rb
+++ b/spec/models/comic_spec.rb
@@ -37,9 +37,9 @@ describe Comic do
3737 @comic = Factory.build :normal_comic, :author_id => @author.id, :width => 0
3838 @comic.should_not be_valid
3939 end
40- it '負では通る' do
40+ it '負でも失敗する' do
4141 @comic = Factory.build :normal_comic, :author_id => @author.id, :width => -1
42- @comic.should be_valid
42+ @comic.should_not be_valid
4343 end
4444 it '正なら通る' do
4545 @comic = Factory.build :normal_comic, :author_id => @author.id, :width => 1
@@ -55,9 +55,9 @@ describe Comic do
5555 @comic = Factory.build :normal_comic, :author_id => @author.id, :height => 0
5656 @comic.should_not be_valid
5757 end
58- it '負では通る' do
58+ it '負でも失敗する' do
5959 @comic = Factory.build :normal_comic, :author_id => @author.id, :height => -1
60- @comic.should be_valid
60+ @comic.should_not be_valid
6161 end
6262 it '正なら通る' do
6363 @comic = Factory.build :normal_comic, :author_id => @author.id, :height => 1
--- /dev/null
+++ b/spec/models/panel_picture_spec.rb
@@ -0,0 +1,51 @@
1+# -*- encoding: utf-8 -*-
2+require 'spec_helper'
3+
4+describe PanelPicture do
5+ describe '検証に於いて' do
6+ before do
7+ end
8+
9+ it 'オーソドックスなデータなら通る' do
10+ @comic = Factory.build :normal_comic, :author_id => @author.id
11+ @comic.should be_valid
12+ end
13+
14+ context 'widthを検証するとき' do
15+ it 'nullなら失敗する' do
16+ @comic = Factory.build :normal_comic, :author_id => @author.id, :width => nil
17+ @comic.should_not be_valid
18+ end
19+ it '0なら失敗する' do
20+ @comic = Factory.build :normal_comic, :author_id => @author.id, :width => 0
21+ @comic.should_not be_valid
22+ end
23+ it '負では通る' do
24+ @comic = Factory.build :normal_comic, :author_id => @author.id, :width => -1
25+ @comic.should be_valid
26+ end
27+ it '正なら通る' do
28+ @comic = Factory.build :normal_comic, :author_id => @author.id, :width => 1
29+ @comic.should be_valid
30+ end
31+ end
32+ context 'heightを検証するとき' do
33+ it 'nullなら失敗する' do
34+ @comic = Factory.build :normal_comic, :author_id => @author.id, :height => nil
35+ @comic.should_not be_valid
36+ end
37+ it '0なら失敗する' do
38+ @comic = Factory.build :normal_comic, :author_id => @author.id, :height => 0
39+ @comic.should_not be_valid
40+ end
41+ it '負では通る' do
42+ @comic = Factory.build :normal_comic, :author_id => @author.id, :height => -1
43+ @comic.should be_valid
44+ end
45+ it '正なら通る' do
46+ @comic = Factory.build :normal_comic, :author_id => @author.id, :height => 1
47+ @comic.should be_valid
48+ end
49+ end
50+ end
51+end
旧リポジトリブラウザで表示