Skip to content

Commit e6eb62a

Browse files
bivanalharcysjonathan
authored andcommitted
refactor(spec): remove dep for rails < 6
- load models in spec/models instead of spec/dummy - restructure database_helpers
1 parent 9a14d5a commit e6eb62a

File tree

5 files changed

+82
-77
lines changed

5 files changed

+82
-77
lines changed

lib/active_record/userstamp/stampable.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ def add_userstamp_associations(options)
8585
belongs_to :updater, **relation_options.reverse_merge(foreign_key: config.updater_attribute) if
8686
associations.second
8787
if associations.third
88-
relation_options.reverse_merge!(required: false) if ActiveRecord::VERSION::MAJOR >= 5 ||
89-
(ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR >= 2)
88+
relation_options.reverse_merge!(required: false)
9089
belongs_to :deleter, **relation_options.reverse_merge(foreign_key: config.deleter_attribute)
9190
end
9291
end

spec/dummy/config/application.rb

-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ class Application < Rails::Application
1818
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
1919
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
2020
# config.i18n.default_locale = :de
21-
22-
config.after_initialize do
23-
require_relative '../db/schema'
24-
end
2521
end
2622
end
2723

spec/dummy/db/schema.rb

-54
This file was deleted.

spec/models.rb

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require_relative './support/database_helpers.rb'
2+
3+
def initialize_schema
4+
initialize_database do
5+
create_table :users, :force => true do |t|
6+
t.column :name, :string
7+
t.column :creator_id, :integer
8+
t.column :created_on, :datetime
9+
t.column :updater_id, :integer
10+
t.column :updated_at, :datetime
11+
end
12+
13+
# People are created and updated by Users
14+
create_table :people, :force => true do |t|
15+
t.column :name, :string
16+
t.column :creator_id, :integer
17+
t.column :created_on, :datetime
18+
t.column :updater_id, :integer
19+
t.column :updated_at, :datetime
20+
end
21+
22+
# Posts are created and updated by People
23+
create_table :posts, :force => true do |t|
24+
t.column :title, :string
25+
t.column :creator_id, :integer
26+
t.column :created_on, :datetime
27+
t.column :updater_id, :integer
28+
t.column :updated_at, :datetime
29+
t.column :deleter_id, :integer
30+
t.column :deleted_at, :datetime
31+
end
32+
33+
# Comments are created and updated by People
34+
# and also use non-standard foreign keys.
35+
create_table :comments, :force => true do |t|
36+
t.column :post_id, :integer
37+
t.column :comment, :string
38+
t.column :created_by, :integer
39+
t.column :created_at, :datetime
40+
t.column :updated_by, :integer
41+
t.column :updated_at, :datetime
42+
t.column :deleted_by, :integer
43+
t.column :deleted_at, :datetime
44+
end
45+
46+
create_table :tags, force: true do |t|
47+
t.column :title, :string
48+
end
49+
50+
create_table :post_tags, force: true do |t|
51+
t.column :post_id, :integer
52+
t.column :tag_id, :integer
53+
end
54+
end
55+
end
56+
57+
initialize_schema

spec/support/database_helpers.rb

+24-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1-
def define_first_post
2-
@first_post = Post.create!(title: 'a title')
1+
require 'active_record'
2+
3+
def connect_to_database
4+
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
5+
end
6+
7+
def clear_database
8+
ActiveRecord::Base.descendants.each do |model|
9+
model.delete_all if model.table_exists?
10+
end
311
end
412

5-
RSpec.configure do |config|
6-
config.before(:each) do
7-
User.delete_all
8-
Person.delete_all
9-
Post.delete_all
10-
Comment.delete_all
11-
User.reset_stamper
12-
Person.reset_stamper
13+
def reset_database
14+
ActiveRecord::Base.descendants.map(&:reset_column_information)
15+
ActiveRecord::Base.connection.disconnect!
16+
connect_to_database
17+
end
1318

14-
@zeus = User.create!(name: 'Zeus')
15-
@hera = User.create!(name: 'Hera')
16-
User.stamper = @zeus.id
19+
def initialize_database(&block)
20+
reset_database
21+
ActiveRecord::Schema.define(&block)
22+
end
1723

18-
@delynn = Person.create!(name: 'Delynn')
19-
@nicole = Person.create!(name: 'Nicole')
20-
Person.stamper = @delynn.id
21-
end
24+
def define_first_post
25+
@first_post = Post.create!(title: 'a title')
2226
end
27+
28+
ActiveRecord::Migration.verbose = false
29+
connect_to_database

0 commit comments

Comments
 (0)