Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script: Assign biogeographic regions from localities #1128

Closed
1 task done
jonkerz opened this issue Jul 31, 2020 · 1 comment
Closed
1 task done

Script: Assign biogeographic regions from localities #1128

jonkerz opened this issue Jul 31, 2020 · 1 comment
Labels

Comments

@jonkerz
Copy link
Member

jonkerz commented Jul 31, 2020

See #695

Script

EDIT_SUMMARY = 'Assign biogeographic regions from localities, see %github1128'
ANTCATBOT = User.find_by!(name: 'AntCatBot')

RequestStore.store[:current_request_uuid] = SecureRandom.uuid
PaperTrail.request.whodunnit = ANTCATBOT.id

protonyms = Protonym.joins(:name).
  where(names: { type: Name::SPECIES_GROUP_NAMES }).
  where(biogeographic_region: nil).where.not(locality: nil).
  where(fossil: false); nil

country_mappings = Protonym.where.not(biogeographic_region: nil).
  where.not(locality: nil).pluck(:locality, :biogeographic_region).to_h; nil

protonyms.each do |protonym|
  locality = protonym.locality
  locality_without_parens = locality.gsub(/\(.*\)/, "").gsub(/[".]/, "").squish

  biogeographic_region =
    country_mappings[locality] ||
    country_mappings[locality.downcase.upcase_first] ||
    country_mappings[locality_without_parens] ||
    country_mappings[locality_without_parens.downcase.upcase_first]

  next unless biogeographic_region

  protonym.update!(biogeographic_region: biogeographic_region)
  protonym.create_activity :update, ANTCATBOT,
    edit_summary: "Assign biogeographic region from locality (#{protonym.locality} --> #{biogeographic_region}), see %github1128"
end; nil

Activity.execute_script_activity User.find_by!(name: 'Fredrik Palmkron'), EDIT_SUMMARY

@jonkerz jonkerz added the script label Jul 31, 2020
@bpescador
Copy link

bpescador commented Jul 31, 2020 via email

@jonkerz jonkerz closed this as completed Aug 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants