Created
July 6, 2016 21:15
-
-
Save hayduke19us/73c83e90478727fadc0853c3d5516bc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| | |
translations = property.room_category.send(type) | |
translation = translations.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
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.·
Usage
rails r [path_to_script] [property uuids]
Single property
Multiple properties
On remote server
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
Success
Errors