forked from softace/validates_timeliness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_helper.rb
99 lines (82 loc) · 2.48 KB
/
spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require 'rspec'
require 'active_model'
require 'active_model/validations'
require 'active_record'
require 'action_view'
require 'timecop'
require 'rspec_tag_matchers'
require 'validates_timeliness'
require 'support/test_model'
require 'support/model_helpers'
require 'support/config_helper'
ValidatesTimeliness.setup do |c|
c.extend_orms = [ :active_record ]
c.enable_date_time_select_extension!
c.enable_multiparameter_extension!
c.default_timezone = :utc
end
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)
# Extend TestModel as you would another ORM/ODM module
module TestModelShim
extend ActiveSupport::Concern
include ValidatesTimeliness::AttributeMethods
module ClassMethods
# 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
attribute :birth_date, :date
attribute :birth_time, :time
attribute :birth_datetime, :datetime
define_attribute_methods model_attributes.keys
end
class PersonWithShim < Person
include TestModelShim
end
ActiveRecord::Base.default_timezone = :utc
ActiveRecord::Base.time_zone_aware_attributes = true
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define(:version => 1) do
create_table :employees, :force => true do |t|
t.string :first_name
t.string :last_name
t.date :birth_date
t.time :birth_time
t.datetime :birth_datetime
end
end
class Employee < ActiveRecord::Base
attr_accessor :redefined_birth_date_called
validates_date :birth_date, :allow_nil => true
validates_date :birth_time, :allow_nil => true
validates_date :birth_datetime, :allow_nil => true
def birth_date=(value)
self.redefined_birth_date_called = true
super
end
end
RSpec.configure do |c|
c.mock_with :rspec
c.include(RspecTagMatchers)
c.include(ModelHelpers)
c.include(ConfigHelper)
c.before do
reset_validation_setup_for(Person)
reset_validation_setup_for(PersonWithShim)
end
c.filter_run_excluding :active_record => lambda {|version|
!(::ActiveRecord::VERSION::STRING.to_s =~ /^#{version.to_s}/)
}
end