Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. # Script to create and add categories for all headquarters and offices
  2. locations = Office.joins(:country).joins(:region).select('btrim(offices.city) as city, btrim(regions.name) as state_name, btrim(countries.name) as country_name, offices.region_id, offices.country_id').group('city, state_name, country_name, offices.region_id, offices.country_id')
  3. locations.each{|loc|
  4. puts "'#{loc.city}', '#{loc.state_name}', '#{loc.country_name}''"
  5. city_cat = Category.find_by(:tag => loc.city.downcase.strip, :tag_type => "LocationTag") unless loc.city.blank?
  6. state_cat = Category.find_by(:tag => loc.state_name.downcase.strip, :tag_type => "LocationTag") unless loc.state_name.blank?
  7. country_cat = Category.find_by(:tag => loc.country_name.downcase.strip, :tag_type => "LocationTag") unless loc.country_name.blank?
  8.  
  9. # Add all companies based on matching offices
  10. offices = Office.where(city: loc.city, region_id: loc.region_id, country_id: loc.country_id).select(:company_id).distinct
  11. offices.each{|off|
  12. next if off.company_id.blank?
  13. company = Company.find_by(id: off.company_id)
  14. next if company.blank? || company.id.blank?
  15.  
  16. unless city_cat.blank? || loc.city.blank?
  17. if CategoryCompany.where("category_id = #{city_cat.id} AND company_id = #{company.id}").blank?
  18. CategoryCompany.create({:category_id => city_cat.id, :company_id => company.id})
  19. end
  20. end
  21. unless state_cat.id.blank? || loc.state_name.blank? || (!city_cat.blank? && !state_cat.blank? && city_cat.id == state_cat.id)
  22. if CategoryCompany.where("category_id = #{state_cat.id} AND company_id = #{company.id}").blank?
  23. CategoryCompany.create({:category_id => state_cat.id, :company_id => company.id})
  24. end
  25. end
  26. unless country_cat.blank? || loc.country_name.blank?
  27. if CategoryCompany.where("category_id = #{country_cat.id} AND company_id = #{company.id}").blank?
  28. CategoryCompany.create({:category_id => country_cat.id, :company_id => company.id})
  29. end
  30. end
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement