Skip to content

Instantly share code, notes, and snippets.

@professor
Created January 7, 2022 23:15
Show Gist options
  • Save professor/6bfdf3597a80b81bf2599ba3c277d999 to your computer and use it in GitHub Desktop.
Save professor/6bfdf3597a80b81bf2599ba3c277d999 to your computer and use it in GitHub Desktop.
require_relative '../app/utilities/external_apis/phrase_api_client.rb'
require 'active_support'
require 'active_support/core_ext/string'
def process_file(phrase, english_locale_id, branch, key_prefix, existing_file)
menu_with_label_priority_regex = /menu parent: (.+), label: (.+), priority: (.+)/
menu_with_label_regex = /menu parent: (.+), label: (.+)/
active_admin_register_regex = /ActiveAdmin.register (.+) do/
puts "Processing #{File.basename(existing_file)}"
new_file = existing_file.delete_suffix(".rb") + "_new.rb"
File.open(new_file, "w") do |f|
File.open(existing_file).each do |line|
# register_matches = line.match(active_admin_register_regex)
# if register_matches
# class_name = register_matches[1] # "FulfillmentOrderItem::Version"
# class_underscore = class_name.underscore # "fulfillment_order_item/version"
# end
priority = nil
if line.include?("menu parent:") && line.include?("priority:")
puts "priority match"
menu_matches = line.match(menu_with_label_priority_regex)
priority = menu_matches[3] # 3
puts "priority #{priority}"
else
menu_matches = line.match(menu_with_label_regex)
end
if menu_matches
menu_parents = eval(menu_matches[1]) # ["Inventory", "History"]
menu_name = menu_matches[2] # "Fulfillment Order Item History"
menu_name = menu_name.delete_prefix('"').delete_suffix('"')
menu_name = menu_name.delete_prefix("'").delete_suffix("'")
menu_name_underscore = menu_name.gsub(" ", "_").underscore
new_menu_parents_string = menu_parents.map do |menu_parent|
menu_parent_underscore = menu_parent.gsub(" ", "_").underscore
menu_parent_key = key_prefix + ".menu.parent.#{menu_parent_underscore}"
puts "find_or_create_key_with_value: I18n.t('#{menu_parent_key}' #{menu_parent})"
# phrase.find_key_or_create_key_with_value(branch, english_locale_id, menu_parent_key, menu_parent)
"I18n.t('#{menu_parent_key}')"
end
puts new_menu_parents_string.join(",")
# key = "activerecord.models.inventory/#{class_underscore}"
menu_key = key_prefix + ".menu.page.#{menu_name_underscore}"
puts "find_or_create_key_with_value: I18n.t('#{menu_key}' #{menu_name})"
# phrase.find_key_or_create_key_with_value(branch, english_locale_id, menu_key, menu_name)
new_menu_name = "I18n.t('#{menu_key}')"
if priority
# replaced_line = " menu parent: [\"#{new_menu_parents_string.join("\", \"")}\"],\n label: \"#{new_menu_name}\",\n priority: #{priority}\n"
replaced_line = " menu parent: [\n \"#{new_menu_parents_string.join("\",\n \"")}\",\n ],\n label: \"#{new_menu_name}\",\n priority: #{priority}\n"
else
# replaced_line = " menu parent: [\"#{new_menu_parents_string.join("\", \"")}\"],\n label: \"#{new_menu_name}\"\n"
replaced_line = " menu parent: [\n \"#{new_menu_parents_string.join("\",\n \"")}\",\n ],\n label: \"#{new_menu_name}\"\n"
end
f.write replaced_line
else
f.write line
end
end
end
`mv #{new_file} #{existing_file}`
end
branch = "ts/i18n-active-admin-menu"
phrase = ExternalApis::PhraseApiClient.new
english_locale_id = phrase.get_english_locale_id(branch)
directory = "/Users/tsedano/workspace/super_samurai/app/admin/"
page = "activity"
# file = "fulfillment_order_item_versions.rb"
# file = "bin_level.rb"
# file = "bin_review.rb"
# directory = "/Users/tsedano/workspace/super_samurai/app/admin/"
# key_prefix = "active_admin_enjoy"
#
# Dir.glob(directory + '*.rb').each do |f|
# process_file phrase, english_locale_id, branch, key_prefix, f
# end
directory = "/Users/tsedano/workspace/super_samurai/apps/inventory/app/admin/inventory/"
key_prefix = "active_admin_enjoy.inventory"
# process_file phrase, english_locale_id, branch, directory + "bin_review.rb"
# process_file phrase, english_locale_id, branch, directory + "fulfillment_order_item_versions.rb"
# process_file phrase, english_locale_id, branch, directory + "erp_sync.rb"
Dir.glob(directory + '*.rb').each do |f|
process_file phrase, english_locale_id, branch, key_prefix, f
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment