Skip to content
Browse files

add first pass at boolean and datetime columns - just simple presence…

… check for datetime, true/false for booleans
  • Loading branch information...
1 parent 6b864be commit 366b8d684ee5013e603a8459c4d1c2b702a2bf41 @mjankowski mjankowski committed Oct 5, 2008
Showing with 174 additions and 0 deletions.
  1. +13 −0 Rakefile
  2. +1 −0 init.rb
  3. +21 −0 lib/pacecar.rb
  4. +32 −0 lib/pacecar/boolean.rb
  5. +32 −0 lib/pacecar/datetime.rb
  6. +25 −0 test/boolean_test.rb
  7. +25 −0 test/datetime_test.rb
  8. +16 −0 test/models.rb
  9. +9 −0 test/test_helper.rb
View
13 Rakefile
@@ -0,0 +1,13 @@
+require 'rubygems'
+require 'rake'
+require 'rake/testtask'
+
+desc 'Default: run unit tests'
+task :default => :test
+
+desc 'Test the pacecar plugin'
+Rake::TestTask.new :test do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
View
1 init.rb
@@ -0,0 +1 @@
+require 'pacecar'
View
21 lib/pacecar.rb
@@ -0,0 +1,21 @@
+require 'pacecar/boolean'
+require 'pacecar/datetime'
+
+module Pacecar
+
+ def self.included(base)
+ base.class_eval do
+ extend ClassMethods
+ include Pacecar::Boolean
+ include Pacecar::Datetime
+ end
+ end
+
+ module ClassMethods
+ protected
+ def columns_of_type(*types)
+ columns.select { |column| types.include? column.type }.collect(&:name)
+ end
+ end
+
+end
View
32 lib/pacecar/boolean.rb
@@ -0,0 +1,32 @@
+module Pacecar
+
+ module Boolean
+
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+
+ def self.extended(base)
+ base.define_boolean_scopes
+ end
+
+ def define_boolean_scopes
+ boolean_columns.each do |column|
+ named_scope column.to_sym, :conditions => { column.to_sym => true }
+ named_scope "not_#{column}".to_sym, :conditions => { column.to_sym => false }
+ end
+ end
+
+ protected
+
+ def boolean_columns
+ columns_of_type :boolean
+ end
+
+ end
+
+ end
+
+end
View
32 lib/pacecar/datetime.rb
@@ -0,0 +1,32 @@
+module Pacecar
+
+ module Datetime
+
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+
+ def self.extended(base)
+ base.define_datetime_scopes
+ end
+
+ def define_datetime_scopes
+ datetime_columns.each do |column|
+ named_scope column.to_sym, :conditions => "#{column} is not null"
+ named_scope "not_#{column}".to_sym, :conditions => "#{column} is null"
+ end
+ end
+
+ protected
+
+ def datetime_columns
+ columns_of_type :datetime
+ end
+
+ end
+
+ end
+
+end
View
25 test/boolean_test.rb
@@ -0,0 +1,25 @@
+require File.join(File.dirname(__FILE__), 'test_helper')
+
+class BooleanTest < Test::Unit::TestCase
+
+ context "A class which has included Pacecar" do
+ setup do
+ @class = User
+ end
+ should "respond to boolean column method" do
+ assert @class.respond_to? :admin
+ end
+ should "set correct proxy options for boolean column method" do
+ proxy_options = { :conditions => { :admin => true } }
+ assert_equal proxy_options, @class.admin.proxy_options
+ end
+ should "respond to not_ boolean column method" do
+ assert @class.respond_to? :not_admin
+ end
+ should "set correct proxy options for not_ boolean column method" do
+ proxy_options = { :conditions => { :admin => false } }
+ assert_equal proxy_options, @class.not_admin.proxy_options
+ end
+ end
+
+end
View
25 test/datetime_test.rb
@@ -0,0 +1,25 @@
+require File.join(File.dirname(__FILE__), 'test_helper')
+
+class DatetimeTest < Test::Unit::TestCase
+
+ context "A class which has included Pacecar" do
+ setup do
+ @class = User
+ end
+ should "respond to datetime column method" do
+ assert @class.respond_to? :created_at
+ end
+ should "set correct proxy options for datetime column method" do
+ proxy_options = { :conditions => 'created_at is not null' }
+ assert_equal proxy_options, @class.created_at.proxy_options
+ end
+ should "respond to datetime not_ column method" do
+ assert @class.respond_to? :not_created_at
+ end
+ should "set correct proxy options for datetime not_ column method" do
+ proxy_options = { :conditions => 'created_at is null' }
+ assert_equal proxy_options, @class.not_created_at.proxy_options
+ end
+ end
+
+end
View
16 test/models.rb
@@ -0,0 +1,16 @@
+ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => File.join(File.dirname(__FILE__), 'test.db')
+
+class CreateSchema < ActiveRecord::Migration
+ def self.up
+ create_table :users, :force => true do |t|
+ t.boolean :admin, :default => false
+ t.datetime :created_at
+ end
+ end
+end
+
+CreateSchema.suppress_messages { CreateSchema.migrate(:up) }
+
+class User < ActiveRecord::Base
+ include Pacecar
+end
View
9 test/test_helper.rb
@@ -0,0 +1,9 @@
+$: << File.join(File.dirname(__FILE__), '..', 'lib')
+$: << File.join(File.dirname(__FILE__))
+
+require 'rubygems'
+require 'test/unit'
+require 'activerecord'
+require 'shoulda'
+require 'pacecar'
+require 'models'

0 comments on commit 366b8d6

Please sign in to comment.
Something went wrong with that request. Please try again.