change ORM attribute generation and extension mechanism

now using shim since the attribute matcher is not required for AM
This commit is contained in:
Adam Meehan
2010-09-16 22:33:22 +10:00
parent d0080ebac4
commit 9ddd150b2f
11 changed files with 179 additions and 81 deletions

View File

@@ -13,9 +13,10 @@ require 'rspec_tag_matchers'
require 'model_helpers'
require 'validates_timeliness'
require 'test_model'
ValidatesTimeliness.setup do |c|
c.extend_classes = [ ActiveModel::Validations, ActiveRecord::Base ]
c.extend_orms = [ :active_record ]
c.enable_date_time_select_extension!
c.enable_multiparameter_extension!
end
@@ -25,22 +26,38 @@ Time.zone = 'Australia/Melbourne'
LOCALE_PATH = File.expand_path(File.dirname(__FILE__) + '/../lib/generators/validates_timeliness/templates/en.yml')
I18n.load_path.unshift(LOCALE_PATH)
class Person
include ActiveModel::AttributeMethods
include ActiveModel::Validations
extend ActiveModel::Translation
# Extend TestModel as you would another ORM/ODM module
module TestModel
include ValidatesTimeliness::HelperMethods
include ValidatesTimeliness::AttributeMethods
attr_accessor :birth_date, :birth_time, :birth_datetime
attr_accessor :attributes
def self.included(base)
base.extend HookMethods
end
def initialize(attributes = {})
@attributes = {}
attributes.each do |key, value|
send "#{key}=", value
module HookMethods
# Hook method for attribute method generation
def define_attribute_methods(attr_names)
super
define_timeliness_methods
end
# Hook into native time zone handling check, if any
def timeliness_attribute_timezone_aware?(attr_name)
false
end
end
end
class Person
include TestModel
self.model_attributes = :birth_date, :birth_time, :birth_datetime
validates_date :birth_date
validates_time :birth_time
validates_datetime :birth_datetime
define_attribute_methods model_attributes
end
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define(:version => 1) do
@@ -54,6 +71,10 @@ ActiveRecord::Schema.define(:version => 1) do
end
class Employee < ActiveRecord::Base
validates_date :birth_date
validates_time :birth_time
validates_datetime :birth_datetime
define_attribute_methods
end
Rspec.configure do |c|
@@ -61,8 +82,10 @@ Rspec.configure do |c|
c.include(RspecTagMatchers)
c.before do
Person.reset_callbacks(:validate)
Person.timeliness_validated_attributes = {}
Person._validators.clear
Employee.reset_callbacks(:validate)
Employee.timeliness_validated_attributes = {}
Employee._validators.clear
end
end