This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.
| Validating Data with ActiveRecord | ||
|---|---|---|
| Code | Expected | Actual |
require 'cookbook_dbconnect'
activerecord_connect
class Comment < ActiveRecord::Base
@@profanity = %w{trot krip}
@@no_profanity_re = Regexp.new('^(?!.*(' + @@profanity.join('|') + '))')
validates_presence_of %w{author}
validates_length_of :content, :in => 1..200
validates_format_of :content, :with => @@no_profanity_re,
:message => 'contains profanity'
end
comment = Comment.create
comment.errors.on 'author' |
"can't be blank" | "can't be blank" |
comment.errors['content'] |
"is too short (minimum is 1 characters)" | "is too short (minimum is 1 characters)" |
comment.save |
false | false |
comment = Comment.create(:content => 'x' * 1000) comment.errors['content'] |
"is too long (maximum is 200 characters)" | "is too long (maximum is 200 characters)" |
comment = Comment.create(:author => 'Alice', :content => "About what I'd expect from a trotting krip such as yourself!") comment.errors.count |
1 | 1 |
comment.errors.each_full { |msg| puts msg } |
Content contains profanity |
Content contains profanity |
comment = Comment.create(:author => 'Alice', :content => 'I disagree!') comment.save |
true | true |
require 'cookbook_dbconnect'
activerecord_connect
class Comment < ActiveRecord::Base
@@profanity = %w{trot krip}
@@no_profanity_re = Regexp.new('^(?!.*(' + @@profanity.join('|') + '))')
validates_presence_of %w{author}, :message => 'Please enter your name.'
validates_length_of :content, :in => 1..200,
:too_short => 'Please enter a comment.',
:too_long => 'Comments are limited to 200 characters.'
validates_format_of :content, :with => @@no_profanity_re,
:message => 'Try to express yourself without profanity.'
end |
[#<Proc:0xb79a13a8@/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/validations.rb:296>, #<Proc:0xb79a13a8@/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/validations.rb:296>, #<Proc:0xb799f558@/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/validations.rb:391>, #<Proc:0xb79a13a8@/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/validations.rb:296>, #<Proc:0xb79a13a8@/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/validations.rb:296>, #<Proc:0xb799f558@/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.0/lib/active_record/validations.rb:391>] | |