Skip to content
This repository
Browse code

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

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

0 comments on commit 366b8d6

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