-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
ar_models.rb
87 lines (68 loc) · 1.69 KB
/
ar_models.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
# frozen_string_literal: true
require "active_record"
require "fabrication"
require "test_prof"
require "test_prof/factory_bot"
require 'activerecord-jdbc-adapter' if defined? JRUBY_VERSION
require 'activerecord-jdbcsqlite3-adapter' if defined? JRUBY_VERSION
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.define do
create_table :users do |t|
t.string :name
t.string :tag
end
create_table :posts do |t|
t.text :text
t.integer :user_id
t.foreign_key :users if ActiveRecord::VERSION::MAJOR >= 4
end
end
ActiveRecord::Base.logger = Logger.new(STDOUT) if ENV['LOG']
class User < ActiveRecord::Base
validates :name, presence: true
has_many :posts, dependent: :destroy
def clone
copy = dup
copy.name = "#{name} (cloned)"
copy
end
end
class Post < ActiveRecord::Base
belongs_to :user
end
TestProf::FactoryBot.define do
factory :user do
sequence(:name) { |n| "John #{n}" }
trait :with_posts do
after(:create) do
TestProf::FactoryBot.create_pair(:post)
end
end
trait :traited do
tag 'traited'
end
trait :other_trait do
tag 'other_trait'
end
end
factory :post do
sequence(:text) { |n| "Post ##{n}}" }
user
trait :with_bad_user do
user { create(:user) }
end
trait :with_traited_user do
association :user, factory: %i[user traited]
end
trait :with_other_traited_user do
association :user, factory: %i[user other_trait]
end
end
end
Fabricator(:user) do
name Fabricate.sequence(:name) { |n| "John #{n}" }
end
Fabricator(:post) do
text Fabricate.sequence(:text) { |n| "Post ##{n}}" }
user
end