-
Notifications
You must be signed in to change notification settings - Fork 336
/
helper.rb
103 lines (83 loc) · 2.69 KB
/
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
100
101
102
103
# frozen_string_literal: true
require "simplecov"
require "coveralls"
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new(
[
SimpleCov::Formatter::HTMLFormatter,
Coveralls::SimpleCov::Formatter
]
)
SimpleCov.start do
add_filter "/spec/"
minimum_coverage(73.33)
end
require "logger"
require "rspec"
begin
require "protected_attributes"
rescue LoadError # rubocop:disable HandleExceptions
end
require "delayed_job_active_record"
require "delayed/backend/shared_spec"
Delayed::Worker.logger = Logger.new("/tmp/dj.log")
ENV["RAILS_ENV"] = "test"
db_adapter = ENV["ADAPTER"]
gemfile = ENV["BUNDLE_GEMFILE"]
db_adapter ||= gemfile && gemfile[%r{gemfiles/(.*?)/}] && $1 # rubocop:disable PerlBackrefs
db_adapter ||= "sqlite3"
config = YAML.load(File.read("spec/database.yml"))
ActiveRecord::Base.establish_connection config[db_adapter]
ActiveRecord::Base.logger = Delayed::Worker.logger
ActiveRecord::Migration.verbose = false
# MySQL 5.7 no longer supports null default values for the primary key
# Override the default primary key type in Rails <= 4.0
# https://stackoverflow.com/a/34555109
if db_adapter == "mysql2"
types = if defined?(ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter)
# ActiveRecord 3.2+
ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::NATIVE_DATABASE_TYPES
else
# ActiveRecord < 3.2
ActiveRecord::ConnectionAdapters::Mysql2Adapter::NATIVE_DATABASE_TYPES
end
types[:primary_key] = types[:primary_key].sub(" DEFAULT NULL", "")
end
migration_template = File.open("lib/generators/delayed_job/templates/migration.rb")
# need to eval the template with the migration_version intact
migration_context =
Class.new do
def my_binding
binding
end
private
def migration_version
"[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]" if ActiveRecord::VERSION::MAJOR >= 5
end
end
migration_ruby = ERB.new(migration_template.read).result(migration_context.new.my_binding)
eval(migration_ruby) # rubocop:disable Security/Eval
ActiveRecord::Schema.define do
CreateDelayedJobs.up
create_table :stories, primary_key: :story_id, force: true do |table|
table.string :text
table.boolean :scoped, default: true
end
end
# Purely useful for test cases...
class Story < ActiveRecord::Base
if ::ActiveRecord::VERSION::MAJOR < 4 && ActiveRecord::VERSION::MINOR < 2
set_primary_key :story_id
else
self.primary_key = :story_id
end
def tell
text
end
def whatever(number)
tell * number
end
default_scope { where(scoped: true) }
handle_asynchronously :whatever
end
# Add this directory so the ActiveSupport autoloading works
ActiveSupport::Dependencies.autoload_paths << File.dirname(__FILE__)