Use variables instead of constants in dev seeds

While running the `dev_seed` twice, as we do in the tests, we were
getting the following warnings:

```
db/dev_seeds/proposals.rb:1: warning: already initialized constant
IMAGE_FILES

db/dev_seeds/budgets.rb:1: warning: already initialized constant
INVESTMENT_IMAGE_FILES
```

So we're extracting a method which allows us to use local variables
while removing duplication.

We had this warning with every version of Ruby, not just Ruby 2.7, but
since we're getting rid of all the warnings, we're taking care of this
one as well.
This commit is contained in:
Javi Martín
2023-01-26 17:42:38 +01:00
parent 5e7b3f72a2
commit e19205cfe5
3 changed files with 46 additions and 49 deletions

View File

@@ -36,6 +36,17 @@ def random_locales_attributes(**attribute_names_with_values)
end end
end end
def add_image_to(imageable, sample_image_files)
# imageable should respond to #title & #author
imageable.image = Image.create!({
imageable: imageable,
title: imageable.title,
attachment: Rack::Test::UploadedFile.new(sample_image_files.sample),
user: imageable.author
})
imageable.save!
end
log "Creating dev seeds for tenant #{Tenant.current_schema}" unless Tenant.default? log "Creating dev seeds for tenant #{Tenant.current_schema}" unless Tenant.default?
load_dev_seeds "settings" load_dev_seeds "settings"

View File

@@ -1,27 +1,20 @@
INVESTMENT_IMAGE_FILES = %w[ def add_image_to_investment(investment)
image_files = %w[
brennan-ehrhardt-25066-unsplash_713x513.jpg brennan-ehrhardt-25066-unsplash_713x513.jpg
carl-nenzen-loven-381554-unsplash_713x475.jpg carl-nenzen-loven-381554-unsplash_713x475.jpg
carlos-zurita-215387-unsplash_713x475.jpg carlos-zurita-215387-unsplash_713x475.jpg
hector-arguello-canals-79584-unsplash_713x475.jpg hector-arguello-canals-79584-unsplash_713x475.jpg
olesya-grichina-218176-unsplash_713x475.jpg olesya-grichina-218176-unsplash_713x475.jpg
sole-d-alessandro-340443-unsplash_713x475.jpg sole-d-alessandro-340443-unsplash_713x475.jpg
].map do |filename| ].map do |filename|
Rails.root.join("db", Rails.root.join("db",
"dev_seeds", "dev_seeds",
"images", "images",
"budget", "budget",
"investments", filename) "investments", filename)
end end
def add_image_to(imageable) add_image_to(investment, image_files)
# imageable should respond to #title & #author
imageable.image = Image.create!({
imageable: imageable,
title: imageable.title,
attachment: Rack::Test::UploadedFile.new(INVESTMENT_IMAGE_FILES.sample),
user: imageable.author
})
imageable.save!
end end
section "Creating Budgets" do section "Creating Budgets" do
@@ -122,7 +115,7 @@ section "Creating Investments" do
terms_of_service: "1" terms_of_service: "1"
}.merge(translation_attributes)) }.merge(translation_attributes))
add_image_to(investment) if Random.rand > 0.5 add_image_to_investment(investment) if Random.rand > 0.5
end end
end end
@@ -167,7 +160,7 @@ section "Winner Investments" do
price: rand(10000..heading.price), price: rand(10000..heading.price),
terms_of_service: "1" terms_of_service: "1"
) )
add_image_to(investment) if Random.rand > 0.3 add_image_to_investment(investment) if Random.rand > 0.3
end end
budget.headings.each do |heading| budget.headings.each do |heading|
Budget::Result.new(budget, heading).calculate_winners Budget::Result.new(budget, heading).calculate_winners

View File

@@ -1,24 +1,17 @@
IMAGE_FILES = %w[ def add_image_to_proposal(proposal)
image_files = %w[
firdouss-ross-414668-unsplash_846x475.jpg firdouss-ross-414668-unsplash_846x475.jpg
nathan-dumlao-496190-unsplash_713x475.jpg nathan-dumlao-496190-unsplash_713x475.jpg
steve-harvey-597760-unsplash_713x475.jpg steve-harvey-597760-unsplash_713x475.jpg
tim-mossholder-302931-unsplash_713x475.jpg tim-mossholder-302931-unsplash_713x475.jpg
].map do |filename| ].map do |filename|
Rails.root.join("db", Rails.root.join("db",
"dev_seeds", "dev_seeds",
"images", "images",
"proposals", filename) "proposals", filename)
end end
def add_image_to(imageable) add_image_to(proposal, image_files)
# imageable should respond to #title & #author
imageable.image = Image.create!({
imageable: imageable,
title: imageable.title,
attachment: Rack::Test::UploadedFile.new(IMAGE_FILES.sample),
user: imageable.author
})
imageable.save!
end end
section "Creating Proposals" do section "Creating Proposals" do
@@ -47,7 +40,7 @@ section "Creating Proposals" do
proposal.save! proposal.save!
end end
end end
add_image_to proposal add_image_to_proposal proposal
end end
end end
@@ -75,7 +68,7 @@ section "Creating Archived Proposals" do
proposal.save! proposal.save!
end end
end end
add_image_to proposal add_image_to_proposal proposal
end end
end end
@@ -103,7 +96,7 @@ section "Creating Successful Proposals" do
proposal.save! proposal.save!
end end
end end
add_image_to proposal add_image_to_proposal proposal
end end
tags = Tag.where(kind: "category") tags = Tag.where(kind: "category")
@@ -128,7 +121,7 @@ section "Creating Successful Proposals" do
proposal.save! proposal.save!
end end
end end
add_image_to proposal add_image_to_proposal proposal
end end
end end