Files
grecia/db/dev_seeds.rb
Javi Martín 490fe5fd11 Make dev seeds independent on available locales
Some developers work on CONSUL installations where Spanish and/or
English aren't part of the available locales. In those cases, the
`dev_seed` task was crashing because we were using attributes like
`name_en` and `name_es`.

So we're using attributes for random locales instead.

We're using a proc so we don't have code like this all over the place:

random_locales.map do |locale|
  I18n.with_locale(locale) do
    phase.name = I18n.t("budgets.phase.#{phase.kind}")
    phase.save!
  end
end

This would make the code harder to read and would execute a `save!` once
per locale, which would make the task much slower.

We could also avoid the procs writing something like:

def random_locales_attributes(**attribute_names_with_values)
  random_locales.each_with_object({}) do |locale, attributes|
    I18n.with_locale(locale) do
      attribute_names_with_values.each do |attribute_name, (i18n_key, i18n_args)|
        value = I18n.t(i18n_key, (i18n_args || {}).merge(language: I18n.t("i18n.language.name")))
        attributes["#{attribute_name}_#{locale.to_s.underscore}"] = value
      end
    end
  end
end

And calling the method with with:

random_locales_attributes(name: ["seeds.budgets.name", year: Date.current.year - 1])

However, this code would also be different that what we usually do, we'd
have to apply some magic to pass the `language:` parameter, and the
strings wouldn't be recognized by i18n-tasks, so we aren't sure we're
really gaining anything.
2021-12-14 20:05:07 +01:00

59 lines
1.8 KiB
Ruby

unless Rails.env.test?
require "database_cleaner"
DatabaseCleaner.clean_with :truncation
end
@logger = Logger.new(STDOUT)
@logger.formatter = proc do |_severity, _datetime, _progname, msg|
msg unless @avoid_log
end
def section(section_title)
@logger.info section_title
yield
log("")
end
def log(msg)
@logger.info "#{msg}\n"
end
def random_locales
[I18n.default_locale, *(I18n.available_locales & %i[en es]), *I18n.available_locales.sample(4)].uniq.take(5)
end
def random_locales_attributes(**attribute_names_with_values)
random_locales.each_with_object({}) do |locale, attributes|
I18n.with_locale(locale) do
attribute_names_with_values.each do |attribute_name, value_proc|
attributes["#{attribute_name}_#{locale.to_s.underscore}"] = value_proc.call
end
end
end
end
require_relative "dev_seeds/settings"
require_relative "dev_seeds/geozones"
require_relative "dev_seeds/users"
require_relative "dev_seeds/tags_categories"
require_relative "dev_seeds/debates"
require_relative "dev_seeds/proposals"
require_relative "dev_seeds/budgets"
require_relative "dev_seeds/comments"
require_relative "dev_seeds/votes"
require_relative "dev_seeds/flags"
require_relative "dev_seeds/hiddings"
require_relative "dev_seeds/banners"
require_relative "dev_seeds/polls"
require_relative "dev_seeds/communities"
require_relative "dev_seeds/legislation_processes"
require_relative "dev_seeds/newsletters"
require_relative "dev_seeds/notifications"
require_relative "dev_seeds/widgets"
require_relative "dev_seeds/admin_notifications"
require_relative "dev_seeds/legislation_proposals"
require_relative "dev_seeds/milestones"
require_relative "dev_seeds/pages"
require_relative "dev_seeds/sdg"
log "All dev seeds created successfuly 👍"