Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

git-svn-id: http://svn.viney.net.nz/things/rails/plugins/validates_da…

…te_time@36 20afb1e0-9c0e-0410-9884-91ed27886737
  • Loading branch information...
commit a3ac46dccd19112939e2cbe46af6517218e7a2df 1 parent 99a7a9b
jonathan authored
2  init.rb
View
@@ -1,3 +1 @@
require File.dirname(__FILE__) + '/lib/validates_date_time'
-
-ActiveRecord::Base.send(:include, ActiveRecord::Validations::DateTime)
2  lib/validates_date_time.rb
View
@@ -126,3 +126,5 @@ def unambiguous_year(year)
end
end
end
+
+ActiveRecord::Base.send(:include, ActiveRecord::Validations::DateTime)
42 test/abstract_unit.rb
View
@@ -1,18 +1,40 @@
require 'test/unit'
-require 'rubygems'
-require_gem 'activerecord'
+
+begin
+ require 'active_record/fixtures'
+rescue LoadError
+ require 'rubygems'
+ require_gem 'activerecord'
+ require 'active_record/fixtures'
+end
require File.dirname(__FILE__) + '/../lib/validates_date_time'
-ActiveRecord::Base.send(:include, ActiveRecord::Validations::DateTime)
+config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
+ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
+ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'mysql'])
+
+load(File.dirname(__FILE__) + '/schema.rb')
-require File.dirname(__FILE__) + '/person'
+Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + '/fixtures/'
+$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
+
+class Test::Unit::TestCase #:nodoc:
+ def create_fixtures(*table_names)
+ if block_given?
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
+ else
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
+ end
+ end
-class Test::Unit::TestCase
- attr_reader :p
-
- def setup
- @p = Person.new(:name => 'Jonathan')
- assert p.valid?
+ # Turn off transactional fixtures if you're working with MyISAM tables in MySQL
+ self.use_transactional_fixtures = true
+
+ # Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david)
+ self.use_instantiated_fixtures = false
+
+ def p
+ people(:jonathan)
end
end
6 test/database.yml
View
@@ -0,0 +1,6 @@
+mysql:
+ :adapter: mysql
+ :host: localhost
+ :username: rails
+ :password:
+ :database: validates_date_time_plugin_test
2  test/date_test.rb
View
@@ -1,6 +1,8 @@
require File.dirname(__FILE__) + '/abstract_unit'
class DateTest < Test::Unit::TestCase
+ fixtures :people
+
def test_no_date_checking
assert p.update_attributes(:date_of_birth => nil, :date_of_death => nil)
assert p.update_attributes(:date_of_visit => 'All Blacks')
34 test/person.rb
View
@@ -1,34 +0,0 @@
-class Person < ActiveRecord::Base
- def self.columns()
- @columns ||= []
- end
-
- def self.column(name, sql_type = nil, default = nil, null = true)
- columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
- end
-
- column :name
- column :date_of_birth, :date
- column :date_of_death, :date
- column :date_of_visit, :date
-
- column :date_of_arrival, :date
- column :date_of_departure, :date
-
- column :time_of_birth, :time
- column :time_of_death, :time
-
- validates_presence_of :name
-
- validates_date :date_of_birth, :if => Proc.new { |p| p.date_of_birth? }, :before => nil
- validates_date :date_of_death, :if => Proc.new { |p| p.date_of_death? }, :before => Proc.new { 1.day.from_now.to_date }, :after => Proc.new { |p| p.date_of_birth }
-
- validates_date :date_of_arrival, :if => Proc.new { |p| p.date_of_arrival? }, :before => Proc.new { |p| p.date_of_departure }, :before_message => "avant %s", :after_message => "apres %s"
-
- validates_time :time_of_birth, :if => Proc.new { |p| p.time_of_birth? }
-
- # Want to be able to use update_attributes
- def save
- valid?
- end
-end
13 test/schema.rb
View
@@ -0,0 +1,13 @@
+ActiveRecord::Schema.define :version => 0 do
+ create_table :people, :force => true do |t|
+ t.column :date_of_birth, :date
+ t.column :date_of_death, :date
+ t.column :date_of_visit, :date
+
+ t.column :date_of_arrival, :date
+ t.column :date_of_departure, :date
+
+ t.column :time_of_birth, :time
+ t.column :time_of_death, :time
+ end
+end
2  test/time_test.rb
View
@@ -1,6 +1,8 @@
require File.dirname(__FILE__) + '/abstract_unit'
class TimeTest < Test::Unit::TestCase
+ fixtures :people
+
def test_no_time_checking
assert p.update_attributes(:time_of_birth => nil, :time_of_death => nil)
assert p.update_attributes(:time_of_death => 'Silver Ferns')
Please sign in to comment.
Something went wrong with that request. Please try again.