Note that, currently, we take these settings from the database but we
don't provide a way to edit them through the admin interface, so the
locales must be manually introduced through a Rails console.
While we did consider using a comma-separated list, we're using spaces
in order to be consistent with the way we store the allowed content
types settings.
The `enabled_locales` nomenclature, which contrasts with
`available_locales`, is probably subconsciously based on similar
patterns like the one Nginx uses to enable sites.
Note that we aren't using `Setting.enabled_locales` in the globalize
initializer when setting the fallbacks. This means the following test
(which we could add to the shared globalizable examples) would fail:
```
it "Falls back to an enabled locale if the fallback is not enabled" do
Setting["locales.default"] = "en"
Setting["locales.enabled"] = "fr en"
allow(I18n.fallbacks).to receive(:[]).and_return([:fr, :es])
Globalize.set_fallbacks_to_all_available_locales
I18n.with_locale(:fr) do
expect(record.send(attribute)).to eq "In English"
end
end
```
The reason is that the code making this test pass could be:
```
def Globalize.set_fallbacks_to_all_available_locales
Globalize.fallbacks = I18n.available_locales.index_with do |locale|
((I18n.fallbacks[locale] & Setting.enabled_locales) + Setting.enabled_locales).uniq
end
end
```
However, this would make it impossible to run `rake db:migrate` on new
applications because the initializer would try to load the `Setting`
model but the `settings` table wouldn't exist at that point.
Besides, this is a really rare case that IMHO we don't need to support.
For this scenario, an installation would have to enable a locale, create
records with contents in that locale, then disable that locale and have
that locale as a fallback for a language where content for that record
wasn't created. If that happened, it would be solved by creating content
for that record in every enabled language.
290 lines
10 KiB
Ruby
290 lines
10 KiB
Ruby
require_dependency "poll/answer"
|
|
require_dependency "poll/question/answer"
|
|
|
|
section "Creating polls" do
|
|
def create_poll!(attributes)
|
|
poll = Poll.create!(attributes.merge(starts_at: 1.day.from_now, ends_at: 2.days.from_now))
|
|
poll.update_columns(
|
|
starts_at: attributes[:starts_at].beginning_of_minute,
|
|
ends_at: attributes[:ends_at].beginning_of_minute
|
|
)
|
|
end
|
|
|
|
create_poll!(name: I18n.t("seeds.polls.current_poll"),
|
|
slug: I18n.t("seeds.polls.current_poll").parameterize,
|
|
starts_at: 7.days.ago,
|
|
ends_at: 7.days.from_now,
|
|
geozone_restricted: false)
|
|
|
|
create_poll!(name: I18n.t("seeds.polls.current_poll_geozone_restricted"),
|
|
slug: I18n.t("seeds.polls.current_poll_geozone_restricted").parameterize,
|
|
starts_at: 5.days.ago,
|
|
ends_at: 5.days.from_now,
|
|
geozone_restricted_to: Geozone.sample(3))
|
|
|
|
create_poll!(name: I18n.t("seeds.polls.recounting_poll"),
|
|
slug: I18n.t("seeds.polls.recounting_poll").parameterize,
|
|
starts_at: 15.days.ago,
|
|
ends_at: 2.days.ago)
|
|
|
|
create_poll!(name: I18n.t("seeds.polls.expired_poll_without_stats"),
|
|
slug: I18n.t("seeds.polls.expired_poll_without_stats").parameterize,
|
|
starts_at: 2.months.ago,
|
|
ends_at: 1.month.ago)
|
|
|
|
create_poll!(name: I18n.t("seeds.polls.expired_poll_with_stats"),
|
|
slug: I18n.t("seeds.polls.expired_poll_with_stats").parameterize,
|
|
starts_at: 2.months.ago,
|
|
ends_at: 1.month.ago,
|
|
results_enabled: true,
|
|
stats_enabled: true)
|
|
|
|
Poll.find_each do |poll|
|
|
name = poll.name
|
|
Setting.enabled_locales.map do |locale|
|
|
Globalize.with_locale(locale) do
|
|
poll.name = "#{name} (#{locale})"
|
|
poll.summary = "Summary for locale #{locale}"
|
|
poll.description = "Description for locale #{locale}"
|
|
end
|
|
end
|
|
poll.save!
|
|
end
|
|
end
|
|
|
|
section "Creating Poll Questions & Answers" do
|
|
Poll.find_each do |poll|
|
|
(3..5).to_a.sample.times do
|
|
question_title = Faker::Lorem.sentence(word_count: 3).truncate(60) + "?"
|
|
question = Poll::Question.new(author: User.sample,
|
|
title: question_title,
|
|
poll: poll)
|
|
Setting.enabled_locales.map do |locale|
|
|
Globalize.with_locale(locale) do
|
|
question.title = "#{question_title} (#{locale})"
|
|
end
|
|
end
|
|
question.save!
|
|
Faker::Lorem.words(number: (2..4).to_a.sample).each_with_index do |title, index|
|
|
description = "<p>#{Faker::Lorem.paragraphs.join("</p><p>")}</p>"
|
|
answer = Poll::Question::Answer.new(question: question,
|
|
title: title.capitalize,
|
|
description: description,
|
|
given_order: index + 1)
|
|
Setting.enabled_locales.map do |locale|
|
|
Globalize.with_locale(locale) do
|
|
answer.title = "#{title} (#{locale})"
|
|
answer.description = "#{description} (#{locale})"
|
|
end
|
|
end
|
|
answer.save!
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
section "Creating Poll Votation types" do
|
|
poll = Poll.first
|
|
|
|
poll.questions.each do |question|
|
|
vote_type = VotationType.vote_types.keys.sample
|
|
question.create_votation_type!(vote_type: vote_type, max_votes: (3 unless vote_type == "unique"))
|
|
end
|
|
end
|
|
|
|
section "Creating Poll Booths & BoothAssignments" do
|
|
20.times do |i|
|
|
Poll::Booth.create(name: "Booth #{i}",
|
|
location: Faker::Address.street_address,
|
|
polls: [Poll.sample])
|
|
end
|
|
end
|
|
|
|
section "Creating Poll Shifts for Poll Officers" do
|
|
Poll.find_each do |poll|
|
|
Poll::BoothAssignment.where(poll: poll).find_each do |booth_assignment|
|
|
scrutiny = (poll.ends_at.to_datetime..poll.ends_at.to_datetime + Poll::RECOUNT_DURATION)
|
|
Poll::Officer.find_each do |poll_officer|
|
|
{
|
|
vote_collection: (poll.starts_at.to_datetime..poll.ends_at.to_datetime),
|
|
recount_scrutiny: scrutiny
|
|
}.each do |task_name, task_dates|
|
|
task_dates.each do |shift_date|
|
|
Poll::Shift.create(booth: booth_assignment.booth,
|
|
officer: poll_officer,
|
|
date: shift_date,
|
|
officer_name: poll_officer.name,
|
|
officer_email: poll_officer.email,
|
|
task: task_name)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
section "Commenting Polls" do
|
|
30.times do
|
|
author = User.sample
|
|
poll = Poll.sample
|
|
Comment.create!(user: author,
|
|
created_at: rand(poll.created_at..Time.current),
|
|
commentable: poll,
|
|
body: Faker::Lorem.sentence)
|
|
end
|
|
end
|
|
|
|
section "Creating Poll Voters" do
|
|
def vote_poll_on_booth(user, poll)
|
|
officer = Poll::Officer.sample
|
|
|
|
Poll::Voter.create!(document_type: user.document_type,
|
|
document_number: user.document_number,
|
|
user: user,
|
|
poll: poll,
|
|
origin: "booth",
|
|
officer: officer,
|
|
officer_assignment: officer.officer_assignments.sample,
|
|
booth_assignment: poll.booth_assignments.sample)
|
|
end
|
|
|
|
def vote_poll_on_web(user, poll)
|
|
randomly_answer_questions(poll, user)
|
|
Poll::Voter.create!(document_type: user.document_type,
|
|
document_number: user.document_number,
|
|
user: user,
|
|
poll: poll,
|
|
origin: "web")
|
|
end
|
|
|
|
def randomly_answer_questions(poll, user)
|
|
poll.questions.each do |question|
|
|
next unless [true, false].sample
|
|
|
|
Poll::Answer.create!(question_id: question.id,
|
|
author: user,
|
|
answer: question.question_answers.sample.title)
|
|
end
|
|
end
|
|
|
|
(Poll.expired + Poll.current + Poll.recounting).uniq.each do |poll|
|
|
verified_users = User.level_two_or_three_verified
|
|
if poll.geozone_restricted?
|
|
verified_users = verified_users.where(geozone_id: poll.geozone_ids)
|
|
end
|
|
user_groups = verified_users.in_groups(2)
|
|
user_groups.first.each { |user| vote_poll_on_booth(user, poll) }
|
|
user_groups.second.compact.each { |user| vote_poll_on_web(user, poll) }
|
|
end
|
|
end
|
|
|
|
section "Creating Poll Recounts" do
|
|
Poll.find_each do |poll|
|
|
poll.booth_assignments.each do |booth_assignment|
|
|
officer_assignment = poll.officer_assignments.first
|
|
author = Poll::Officer.first.user
|
|
|
|
total_amount = white_amount = null_amount = 0
|
|
|
|
booth_assignment.voters.count.times do
|
|
case rand
|
|
when 0...0.1 then null_amount += 1
|
|
when 0.1...0.2 then white_amount += 1
|
|
else total_amount += 1
|
|
end
|
|
end
|
|
|
|
Poll::Recount.create!(officer_assignment: officer_assignment,
|
|
booth_assignment: booth_assignment,
|
|
author: author,
|
|
date: poll.ends_at,
|
|
white_amount: white_amount,
|
|
null_amount: null_amount,
|
|
total_amount: total_amount,
|
|
origin: "booth")
|
|
end
|
|
end
|
|
end
|
|
|
|
section "Creating Poll Results" do
|
|
Poll.find_each do |poll|
|
|
poll.booth_assignments.each do |booth_assignment|
|
|
officer_assignment = poll.officer_assignments.first
|
|
author = Poll::Officer.first.user
|
|
|
|
poll.questions.each do |question|
|
|
question.question_answers.each do |answer|
|
|
Poll::PartialResult.create!(officer_assignment: officer_assignment,
|
|
booth_assignment: booth_assignment,
|
|
date: Date.current,
|
|
question: question,
|
|
answer: answer.title,
|
|
author: author,
|
|
amount: rand(999),
|
|
origin: "booth")
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
section "Creating Poll Questions from Proposals" do
|
|
3.times do
|
|
proposal = Proposal.sample
|
|
poll = Poll.current.first
|
|
question = Poll::Question.new(poll: poll)
|
|
question.copy_attributes_from_proposal(proposal)
|
|
question_title = question.title
|
|
Setting.enabled_locales.map do |locale|
|
|
Globalize.with_locale(locale) do
|
|
question.title = "#{question_title} (#{locale})"
|
|
end
|
|
end
|
|
question.save!
|
|
Faker::Lorem.words(number: (2..4).to_a.sample).each_with_index do |title, index|
|
|
description = "<p>#{Faker::ChuckNorris.fact}</p>"
|
|
answer = Poll::Question::Answer.new(question: question,
|
|
title: title.capitalize,
|
|
description: description,
|
|
given_order: index + 1)
|
|
Setting.enabled_locales.map do |locale|
|
|
Globalize.with_locale(locale) do
|
|
answer.title = "#{title} (#{locale})"
|
|
answer.description = "#{description} (#{locale})"
|
|
end
|
|
end
|
|
answer.save!
|
|
end
|
|
end
|
|
end
|
|
|
|
section "Creating Successful Proposals" do
|
|
10.times do
|
|
proposal = Proposal.sample
|
|
poll = Poll.current.first
|
|
question = Poll::Question.new(poll: poll)
|
|
question.copy_attributes_from_proposal(proposal)
|
|
question_title = question.title
|
|
Setting.enabled_locales.map do |locale|
|
|
Globalize.with_locale(locale) do
|
|
question.title = "#{question_title} (#{locale})"
|
|
end
|
|
end
|
|
question.save!
|
|
Faker::Lorem.words(number: (2..4).to_a.sample).each_with_index do |title, index|
|
|
description = "<p>#{Faker::ChuckNorris.fact}</p>"
|
|
answer = Poll::Question::Answer.new(question: question,
|
|
title: title.capitalize,
|
|
description: description,
|
|
given_order: index + 1)
|
|
Setting.enabled_locales.map do |locale|
|
|
Globalize.with_locale(locale) do
|
|
answer.title = "#{title} (#{locale})"
|
|
answer.description = "#{description} (#{locale})"
|
|
end
|
|
end
|
|
answer.save!
|
|
end
|
|
end
|
|
end
|