Skip to content

Instantly share code, notes, and snippets.

@hayduke19us
Last active July 6, 2016 23:00
Show Gist options
  • Select an option

  • Save hayduke19us/ecdfa817e1632dd1dd38920b8f7ca53e to your computer and use it in GitHub Desktop.

Select an option

Save hayduke19us/ecdfa817e1632dd1dd38920b8f7ca53e to your computer and use it in GitHub Desktop.

Special Alex Script

This is used to add supplier translated room type text translations for testing

Install

Clones the directory and changes the name to a more readable format.·

git clone https://gist.github.com/hayduke19us/73c83e90478727fadc0853c3d5516bc7.git &&
mv 73c83e90478727fadc0853c3d5516bc7 special_alex_script

Usage

rails r [path_to_script] [property uuids]

Single property
rails r special_alex_script/create_translations.rb "8bdffc79-edd6-5c72-a0bc-88fbe2807d2c"

Multiple properties

rails r special_alex_script/create_translations "3208d3c5-95b0-5fb4-a882-093d23de475a d491b724-a7f5-52d2-850c-9af1c8e2ac0d"

Change Language

To change the language or add more languags just add the key and value to the hash as seen on line 7 above.

Output

  • You are about to add French and Spanish translations to Properties ["73c83e90478727fadc0853c3d5516bc7"]
Success
French translation added to Monumental Hotel Orlando·
Errors
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Monumental Hotel Orlando -- 504ff0e1-8e4f-5c87-8d95-c04d862ad950 is invalid

 -- Name is invalid and Description is invalid

 -- language must be unique, unable to create Spanish description translation
module SpecialAlexScript # aka SAS
class Translator
attr_reader :uuids, :languages
def initialize(uuids)
@uuids = uuids.flatten
@languages = { 'French' => 'fr', 'Spanish' =>'es' }
end
def properties
raise ArgumentError, 'Missing uuids' if uuids.empty?
@properties ||= uuids.map do |uuid|
if property = ::Property.find_by_uuid(uuid)
ShortProperty.new(property)
else
puts "No Property found for #{uuid}"
nil
end
end
@properties.compact
end
def create
if properties.uniq.any?
properties.each do |property|
unless property.room_category
raise ArgumentError, "\n\nNo room categories for #{property.name}, #{property.uuid}, create a room category.\n\n"
end
add_translations property, :name
add_translations property, :description
end
else
puts "\nNo Properties were found\n\n"
end
end
def add_translations(property, type)
languages.each do |text, language|
translation = property.room_category.send(type).build({ text: text, language: language }, SupplierTranslatedRoomTypeText)
translation.supplier_property_id = property.supplier_property_id
translation.room_type_id = ShortProperty.room_type_id
if property.room_category.valid? && translation.valid?
puts "\n#{text} translation added to #{property.name}'s #{property.room_category.en_name_text} room_category"
property.room_category.save!
else
puts "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
puts "#{property.name} -- #{property.uuid} is invalid\n"
puts "\n -- #{property.room_category.errors.full_messages.to_sentence} \n"
puts "\n -- #{translation.errors.full_messages.to_sentence}, unable to create #{text} #{type} translation \n\n"
end
end
end
class ShortProperty
attr_reader :room_category, :supplier_property_id, :name, :uuid, :room_type_id
def initialize(property)
@room_category = property.room_categories.first
@supplier_property_id = property.supplier_properties.first.id
@name = property.name
@uuid = property.uuid
@@room_type_id = set_room_type_id
end
def self.room_type_id
@@room_type_id
end
def set_room_type_id
if @room_category && @room_category.room_type_mappings.any?
@@room_type_id = room_category.room_type_mappings.first.room_type_id
end
end
end
end
end
translator = SpecialAlexScript::Translator.new ARGV.shift.split(" ")
puts "\nYou are about to add French and Spanish translations to Properties #{translator.uuids}\n\n"
translator.create
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment