Skip to content

Instantly share code, notes, and snippets.

@hayduke19us
Created July 14, 2016 21:21
Show Gist options
  • Save hayduke19us/5b556218c07cb77c997e72ddac5131a6 to your computer and use it in GitHub Desktop.
Save hayduke19us/5b556218c07cb77c997e72ddac5131a6 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
module TranslationTypeChange
class << self
def properties
Mongoid.database[:properties]
end
def find_properties
properties.find(
{
'$or' => [
{ 'room_categories.name._type' => 'TranslatedText' },
{ 'room_categories.description._type' => 'TranslatedText'}
]
},
fields: { "room_categories" => 1 }
)
end
def update
find_properties.each do |property|
update_room_category property
end
end
def update_room_category(property)
id = property["_id"]
property["room_categories"].each do |room_category|
update_translations room_category, id, 'name'
update_translations room_category, id, 'description'
end
end
def update_translations(room_category, id, type)
room_category[type] && room_category[type].each_with_index do |translation, index|
if translated_text?(translation["_type"])
query = { "_id" => id, "room_categories.#{type}._id" => translation["_id"] }
set = { "$set" => { "room_categories.$.#{type}.#{index}._type" => 'UserTranslatedText' } }
update_property query, set
end
end
end
def translated_text?(type)
type.starts_with?('TranslatedText')
end
def update_property(query, set)
properties.update(
query,
set,
{ w: 1, multi: 1 }
)
end
end
end
puts "\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
puts "Found #{TranslationTypeChange.find_properties.count} room_categories with TranslatedText _type translations\n"
TranslationTypeChange.update
puts "\nFinished\n"
puts "\nFound #{TranslationTypeChange.find_properties.count} room_categories with TranslatedText _type translations\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment