Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hayduke19us/1c0642db4c61be4f3d667ff2caacecc4 to your computer and use it in GitHub Desktop.
Save hayduke19us/1c0642db4c61be4f3d667ff2caacecc4 to your computer and use it in GitHub Desktop.
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