Skip to content

Commit fef089f

Browse files
committed
Seeds
1 parent 2070324 commit fef089f

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

app/lib/seeds.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Seeds
2+
def perform; end
3+
4+
def flushdb
5+
raise 'Not in production!' if Rails.env.production?
6+
7+
load_all_models!
8+
9+
ActiveRecord::Base.connection.transaction do
10+
ApplicationRecord.descendants.each(&:delete_all)
11+
end
12+
end
13+
14+
private
15+
16+
def user
17+
@user ||= User.create!(
18+
email: 'user@yopmail.com',
19+
external_id: '1',
20+
current_organization: organization
21+
)
22+
end
23+
24+
def organization
25+
@organization ||= Organization.create!(
26+
siret: '21920023500014',
27+
mon_compte_pro_payload: {
28+
label: 'Commune de clamart - Mairie'
29+
},
30+
)
31+
end
32+
33+
def load_all_models!
34+
Dir[Rails.root.join('app/models/**/*.rb')].each { |f| require f }
35+
end
36+
end

db/seeds.rb

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
# This file should ensure the existence of records required to run the application in every environment (production,
2-
# development, test). The code here should be idempotent so that it can be executed at any point in every environment.
3-
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
4-
#
5-
# Example:
6-
#
7-
# ["Action", "Comedy", "Drama", "Horror"].each do |genre_name|
8-
# MovieGenre.find_or_create_by!(name: genre_name)
9-
# end
1+
return unless Rails.env.development?
2+
3+
Seeds.new.perform

spec/lib/seeds_spec.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
RSpec.describe Seeds do
2+
describe '#perform' do
3+
subject { described_class.new.perform }
4+
5+
it 'does not raise error' do
6+
expect {
7+
subject
8+
}.not_to raise_error
9+
end
10+
end
11+
12+
describe '#flushdb' do
13+
subject { described_class.new.flushdb }
14+
15+
it 'does not raise error' do
16+
expect {
17+
subject
18+
}.not_to raise_error
19+
end
20+
21+
context 'when in production' do
22+
before do
23+
allow(Rails).to receive(:env).and_return('production')
24+
end
25+
26+
it 'raises error' do
27+
expect {
28+
subject
29+
}.to raise_error(StandardError)
30+
end
31+
end
32+
end
33+
end

0 commit comments

Comments
 (0)