From e19205cfe513889665d61ddc6241bc064464eb9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 26 Jan 2023 17:42:38 +0100 Subject: [PATCH] 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. --- db/dev_seeds.rb | 11 ++++++++++ db/dev_seeds/budgets.rb | 43 ++++++++++++++++----------------------- db/dev_seeds/proposals.rb | 41 ++++++++++++++++--------------------- 3 files changed, 46 insertions(+), 49 deletions(-) diff --git a/db/dev_seeds.rb b/db/dev_seeds.rb index 73834c32d..05250b78c 100644 --- a/db/dev_seeds.rb +++ b/db/dev_seeds.rb @@ -36,6 +36,17 @@ def random_locales_attributes(**attribute_names_with_values) 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? load_dev_seeds "settings" diff --git a/db/dev_seeds/budgets.rb b/db/dev_seeds/budgets.rb index 9a4bea0d9..bdae27338 100644 --- a/db/dev_seeds/budgets.rb +++ b/db/dev_seeds/budgets.rb @@ -1,27 +1,20 @@ -INVESTMENT_IMAGE_FILES = %w[ - brennan-ehrhardt-25066-unsplash_713x513.jpg - carl-nenzen-loven-381554-unsplash_713x475.jpg - carlos-zurita-215387-unsplash_713x475.jpg - hector-arguello-canals-79584-unsplash_713x475.jpg - olesya-grichina-218176-unsplash_713x475.jpg - sole-d-alessandro-340443-unsplash_713x475.jpg -].map do |filename| - Rails.root.join("db", - "dev_seeds", - "images", - "budget", - "investments", filename) -end +def add_image_to_investment(investment) + image_files = %w[ + brennan-ehrhardt-25066-unsplash_713x513.jpg + carl-nenzen-loven-381554-unsplash_713x475.jpg + carlos-zurita-215387-unsplash_713x475.jpg + hector-arguello-canals-79584-unsplash_713x475.jpg + olesya-grichina-218176-unsplash_713x475.jpg + sole-d-alessandro-340443-unsplash_713x475.jpg + ].map do |filename| + Rails.root.join("db", + "dev_seeds", + "images", + "budget", + "investments", filename) + end -def add_image_to(imageable) - # 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! + add_image_to(investment, image_files) end section "Creating Budgets" do @@ -122,7 +115,7 @@ section "Creating Investments" do terms_of_service: "1" }.merge(translation_attributes)) - add_image_to(investment) if Random.rand > 0.5 + add_image_to_investment(investment) if Random.rand > 0.5 end end @@ -167,7 +160,7 @@ section "Winner Investments" do price: rand(10000..heading.price), terms_of_service: "1" ) - add_image_to(investment) if Random.rand > 0.3 + add_image_to_investment(investment) if Random.rand > 0.3 end budget.headings.each do |heading| Budget::Result.new(budget, heading).calculate_winners diff --git a/db/dev_seeds/proposals.rb b/db/dev_seeds/proposals.rb index 453960ded..61caf0773 100644 --- a/db/dev_seeds/proposals.rb +++ b/db/dev_seeds/proposals.rb @@ -1,24 +1,17 @@ -IMAGE_FILES = %w[ - firdouss-ross-414668-unsplash_846x475.jpg - nathan-dumlao-496190-unsplash_713x475.jpg - steve-harvey-597760-unsplash_713x475.jpg - tim-mossholder-302931-unsplash_713x475.jpg -].map do |filename| - Rails.root.join("db", - "dev_seeds", - "images", - "proposals", filename) -end +def add_image_to_proposal(proposal) + image_files = %w[ + firdouss-ross-414668-unsplash_846x475.jpg + nathan-dumlao-496190-unsplash_713x475.jpg + steve-harvey-597760-unsplash_713x475.jpg + tim-mossholder-302931-unsplash_713x475.jpg + ].map do |filename| + Rails.root.join("db", + "dev_seeds", + "images", + "proposals", filename) + end -def add_image_to(imageable) - # 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! + add_image_to(proposal, image_files) end section "Creating Proposals" do @@ -47,7 +40,7 @@ section "Creating Proposals" do proposal.save! end end - add_image_to proposal + add_image_to_proposal proposal end end @@ -75,7 +68,7 @@ section "Creating Archived Proposals" do proposal.save! end end - add_image_to proposal + add_image_to_proposal proposal end end @@ -103,7 +96,7 @@ section "Creating Successful Proposals" do proposal.save! end end - add_image_to proposal + add_image_to_proposal proposal end tags = Tag.where(kind: "category") @@ -128,7 +121,7 @@ section "Creating Successful Proposals" do proposal.save! end end - add_image_to proposal + add_image_to_proposal proposal end end