Sha256: bf45c941e0690408a40e2698e146a77ba0a7ac22d19efe8d1bf5414e901333d9

Contents?: true

Size: 1.98 KB

Versions: 2

Compression:

Stored size: 1.98 KB

Contents

require 'whyvalidationssuckin96/rails/active_record'

ActiveRecord::Base.establish_connection(:adapter  => "sqlite3", :database => ":memory:")
ActiveRecord::Schema.define(:version => 1) do
  create_table :musical_works do |t|
    t.string :name
  end
  
  create_table :visual_works do |t|
    t.string :name
    t.string :author
    t.string :type
  end
end 

class MusicalWork < ActiveRecord::Base
  attr_accessor :state, :callbacks_run, :validations_run 

  def after_initialize
    @callbacks_run = []
    @validations_run = []
  end
  
  setup_validations do
    validate :something_that_passes do
      pass if validatable.state == :pass
    end
    
    validate :something_that_fails do
      fail if validatable.state == :fail
    end
    
    validate :validation_that_runs_on_update, :on => :update do
      validatable.validations_run << :validation_that_runs_on_update
    end
    
    validate :validation_that_runs_on_create, :on => :create do
      validatable.validations_run << :validation_that_runs_on_create
    end
  end
  
  def before_validation_on_update
    callbacks_run << :before_validation_on_update
  end

  def before_validation
    callbacks_run << :before_validation
  end
  
  def before_validation_on_create
    callbacks_run << :before_validation_on_create
  end
  
  def after_validation_on_update
    callbacks_run << :after_validation_on_update
  end
  
  def after_validation
    callbacks_run << :after_validation
  end
  
  def after_validation_on_create
    callbacks_run << :after_validation_on_create
  end
  
end

class VisualWork < ActiveRecord::Base
  setup_validations do
    validates_presence_of :name
  end
end

class Photograph < VisualWork
  setup_validations do
    validates_uniqueness_of :author, :base_class_scope => false
  end
end

class Painting < VisualWork
  setup_validations do
    validates_uniqueness_of :author, :base_class_scope => true
  end
end

class Collage < VisualWork
  setup_validations do
    validates_uniqueness_of :name, :scope => :author
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
whyvalidationssuckin96-1.1.0 test/rails/active_record_test_helper.rb
whyvalidationssuckin96-1.0.0 test/rails/active_record_test_helper.rb