Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. # Script to create and add categories for all headquarters and offices
  2. locations = Office.joins(:iso_country).joins(:iso_region).select('btrim(offices.city) as city, btrim(iso_regions.name) as state, btrim(iso_countries.name) as country, iso_countries.region,iso_countries.sub_region, offices.iso_region_id, offices.iso_country_id').group('city, state, country, region, sub_region, offices.iso_region_id, offices.iso_country_id')
  3. locations.each{|loc|
  4. puts "'#{loc.city}', '#{loc.state}', '#{loc.country}', '#{loc.region}', '#{loc.sub_region}'"
  5. city_cat = Category.find_by(:tag => loc.city.downcase, :tag_type => "LocationTag") unless loc.city.blank?
  6. state_cat = Category.find_by(:tag => loc.state.downcase, :tag_type => "LocationTag") unless loc.state.blank?
  7. country_cat = Category.find_by(:tag => loc.country.downcase, :tag_type => "LocationTag") unless loc.country.blank?
  8. region_cat = Category.find_by(:tag => loc.region.downcase, :tag_type => "LocationTag") unless loc.region.blank?
  9. sub_region_cat = Category.find_by(:tag => loc.sub_region.downcase, :tag_type => "LocationTag") unless loc.sub_region.blank?
  10.  
  11. # Add all companies based on matching offices
  12. offices = Office.where(city: loc.city, iso_region_id: loc.iso_region_id, iso_country_id: loc.iso_country_id).select(:company_id).distinct
  13. offices.each{|off|
  14. company = Company.find(off.company_id)
  15. company.clean_data_bypass = true
  16. company.denormalize_bypass = true
  17. company.reindex_bypass = true
  18.  
  19. unless city_cat.blank? || loc.city.blank?
  20. if CategoryCompany.where("category_id = #{city_cat.id} AND company_id = #{company.id}").blank?
  21. city_cat.companies << company
  22. city_cat.companies_count_bypass = true
  23. city_cat.save
  24. end
  25. end
  26. unless state_cat.id.blank? || loc.state.blank? || (!city_cat.blank? && !state_cat.blank? && city_cat.id == state_cat.id)
  27. if CategoryCompany.where("category_id = #{state_cat.id} AND company_id = #{company.id}").blank?
  28. state_cat.companies << company
  29. state_cat.companies_count_bypass = true
  30. state_cat.save
  31. end
  32. end
  33. unless country_cat.blank? || loc.country.blank?
  34. if CategoryCompany.where("category_id = #{country_cat.id} AND company_id = #{company.id}").blank?
  35. country_cat.companies << company
  36. country_cat.companies_count_bypass = true
  37. country_cat.save
  38. end
  39. end
  40. unless region_cat.blank? || loc.region.blank?
  41. if CategoryCompany.where("category_id = #{region_cat.id} AND company_id = #{company.id}").blank?
  42. region_cat.companies << company
  43. region_cat.companies_count_bypass = true
  44. region_cat.save
  45. end
  46. end
  47. unless sub_region_cat.blank? || loc.sub_region.blank?
  48. if CategoryCompany.where("category_id = #{sub_region_cat.id} AND company_id = #{company.id}").blank?
  49. sub_region_cat.companies << company
  50. sub_region_cat.companies_count_bypass = true
  51. sub_region_cat.save
  52. end
  53. end
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement