Files
grecia/db/dev_seeds/proposals.rb
Javi Martín 7ca55c44e0 Apply Rails/SaveBang rubocop rule
Having exceptions is better than having silent bugs.

There are a few methods I've kept the same way they were.

The `RelatedContentScore#score_with_opposite` method is a bit peculiar:
it creates scores for both itself and the opposite related content,
which means the opposite related content will try to create the same
scores as well.

We've already got a test to check `Budget::Ballot#add_investment` when
creating a line fails ("Edge case voting a non-elegible investment").

Finally, the method `User#send_oauth_confirmation_instructions` doesn't
update the record when the email address isn't already present, leading
to the test "Try to register with the email of an already existing user,
when an unconfirmed email was provided by oauth" fo fail if we raise an
exception for an invalid user. That's because updating a user's email
doesn't update the database automatically, but instead a confirmation
email is sent.

There are also a few false positives for classes which don't have bang
methods (like the GraphQL classes) or destroying attachments.

For these reasons, I'm adding the rule with a "Refactor" severity,
meaning it's a rule we can break if necessary.
2019-10-23 14:39:31 +02:00

147 lines
6.1 KiB
Ruby

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|
File.new(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: IMAGE_FILES.sample,
user: imageable.author
})
imageable.save!
end
section "Creating Proposals" do
tags = Faker::Lorem.words(25)
30.times do
title = Faker::Lorem.sentence(3).truncate(60)
summary = Faker::Lorem.sentence(3)
author = User.all.sample
description = "<p>#{Faker::Lorem.paragraphs.join("</p><p>")}</p>"
proposal = Proposal.create!(author: author,
title: title,
summary: summary,
responsible_name: Faker::Name.name,
description: description,
created_at: rand((Time.current - 1.week)..Time.current),
tag_list: tags.sample(3).join(","),
geozone: Geozone.all.sample,
skip_map: "1",
terms_of_service: "1",
published_at: Time.current)
random_locales.map do |locale|
Globalize.with_locale(locale) do
proposal.title = "Title for locale #{locale}"
proposal.summary = "Summary for locale #{locale}"
proposal.description = "<p>Description for locale #{locale}</p>"
proposal.save!
end
end
add_image_to proposal
end
end
section "Creating Archived Proposals" do
tags = Faker::Lorem.words(25)
5.times do
author = User.all.sample
description = "<p>#{Faker::Lorem.paragraphs.join("</p><p>")}</p>"
months_to_archive_proposals = Setting["months_to_archive_proposals"]
proposal = Proposal.create!(author: author,
title: Faker::Lorem.sentence(3).truncate(60),
summary: Faker::Lorem.sentence(3),
responsible_name: Faker::Name.name,
description: description,
tag_list: tags.sample(3).join(","),
geozone: Geozone.all.sample,
skip_map: "1",
terms_of_service: "1",
created_at: months_to_archive_proposals.to_i.months.ago,
published_at: months_to_archive_proposals.to_i.months.ago)
random_locales.map do |locale|
Globalize.with_locale(locale) do
proposal.title = "Archived proposal title for locale #{locale}"
proposal.summary = "Archived proposal title summary for locale #{locale}"
proposal.description = "<p>Archived proposal description for locale #{locale}</p>"
proposal.save!
end
end
add_image_to proposal
end
end
section "Creating Successful Proposals" do
tags = Faker::Lorem.words(25)
10.times do
author = User.all.sample
description = "<p>#{Faker::Lorem.paragraphs.join("</p><p>")}</p>"
proposal = Proposal.create!(author: author,
title: Faker::Lorem.sentence(3).truncate(60),
summary: Faker::Lorem.sentence(3),
responsible_name: Faker::Name.name,
description: description,
created_at: rand((Time.current - 1.week)..Time.current),
tag_list: tags.sample(3).join(","),
geozone: Geozone.all.sample,
skip_map: "1",
terms_of_service: "1",
cached_votes_up: Setting["votes_for_proposal_success"],
published_at: Time.current)
random_locales.map do |locale|
Globalize.with_locale(locale) do
proposal.title = "Successful proposal title for locale #{locale}"
proposal.summary = "Successful proposal title summary for locale #{locale}"
proposal.description = "<p>Successful proposal description for locale #{locale}</p>"
proposal.save!
end
end
add_image_to proposal
end
tags = Tag.where(kind: "category")
30.times do
author = User.all.sample
description = "<p>#{Faker::Lorem.paragraphs.join("</p><p>")}</p>"
proposal = Proposal.create!(author: author,
title: Faker::Lorem.sentence(4).truncate(60),
summary: Faker::Lorem.sentence(3),
responsible_name: Faker::Name.name,
description: description,
created_at: rand((Time.current - 1.week)..Time.current),
tag_list: tags.sample(3).join(","),
geozone: Geozone.all.sample,
skip_map: "1",
terms_of_service: "1",
published_at: Time.current)
random_locales.map do |locale|
Globalize.with_locale(locale) do
proposal.title = "Tagged proposal title for locale #{locale}"
proposal.summary = "Tagged proposal title summary for locale #{locale}"
proposal.description = "<p>Tagged proposal description for locale #{locale}</p>"
proposal.save!
end
end
add_image_to proposal
end
end
section "Creating proposal notifications" do
100.times do |i|
ProposalNotification.create!(title: "Proposal notification title #{i}",
body: "Proposal notification body #{i}",
author: User.all.sample,
proposal: Proposal.all.sample)
end
end