mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 15:22:58 +00:00
change ORM attribute generation and extension mechanism
now using shim since the attribute matcher is not required for AM
This commit is contained in:
@@ -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
|
||||
|
||||
56
spec/test_model.rb
Normal file
56
spec/test_model.rb
Normal file
@@ -0,0 +1,56 @@
|
||||
module TestModel
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
extend ActiveModel::Translation
|
||||
include ActiveModel::Validations
|
||||
include ActiveModel::AttributeMethods
|
||||
include DynamicMethods
|
||||
|
||||
attribute_method_suffix ""
|
||||
attribute_method_suffix "="
|
||||
cattr_accessor :model_attributes
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def define_method_attribute=(attr_name)
|
||||
generated_attribute_methods.module_eval("def #{attr_name}=(new_value); @attributes['#{attr_name}']=new_value ; end", __FILE__, __LINE__)
|
||||
end
|
||||
|
||||
def define_method_attribute(attr_name)
|
||||
generated_attribute_methods.module_eval("def #{attr_name}; @attributes['#{attr_name}']; end", __FILE__, __LINE__)
|
||||
end
|
||||
end
|
||||
|
||||
module DynamicMethods
|
||||
def method_missing(method_id, *args, &block)
|
||||
if !self.class.attribute_methods_generated?
|
||||
self.class.define_attribute_methods self.class.model_attributes.map(&:to_s)
|
||||
method_name = method_id.to_s
|
||||
send(method_id, *args, &block)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(attributes = nil)
|
||||
@attributes = self.class.model_attributes.inject({}) do |hash, column|
|
||||
hash[column.to_s] = nil
|
||||
hash
|
||||
end
|
||||
self.attributes = attributes unless attributes.nil?
|
||||
end
|
||||
|
||||
def attributes
|
||||
@attributes.keys
|
||||
end
|
||||
|
||||
def attributes=(new_attributes={})
|
||||
new_attributes.each do |key, value|
|
||||
send "#{key}=", value
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -1,20 +1,18 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe ValidatesTimeliness::AttributeMethods do
|
||||
before do
|
||||
Employee.validates_datetime :birth_datetime
|
||||
Employee.define_attribute_methods
|
||||
Person.validates_datetime :birth_datetime
|
||||
Person.define_attribute_methods [:birth_datetime]
|
||||
end
|
||||
|
||||
it 'should define _timeliness_raw_value_for instance method' do
|
||||
Person.instance_methods.should include('_timeliness_raw_value_for')
|
||||
end
|
||||
|
||||
context "attribute write method" do
|
||||
class EmployeeCopy < ActiveRecord::Base
|
||||
set_table_name 'employees'
|
||||
validates_datetime :birth_datetime
|
||||
end
|
||||
|
||||
it 'should cache attribute raw value' do
|
||||
r = Employee.new
|
||||
r = EmployeeCopy.new
|
||||
r.birth_datetime = date_string = '2010-01-01'
|
||||
r._timeliness_raw_value_for(:birth_datetime).should == date_string
|
||||
end
|
||||
|
||||
@@ -22,7 +22,7 @@ describe ValidatesTimeliness::HelperMethods do
|
||||
describe ".timeliness_validated_attributes" do
|
||||
it 'should return attributes validated with plugin validator' do
|
||||
Person.validates_date :birth_date
|
||||
Person.timeliness_validated_attributes.should == ["birth_date"]
|
||||
Person.timeliness_validated_attributes.should == {"birth_date" => :date}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user