Module: Tagging::DistanceHelper

Included in:
PrimaryTagCpt::Operations::UpdateRelation, RestaurantBasedOnGeoWorker, RestaurantTagToRestaurantsWorker
Defined in:
app/workers/tagging/distance_helper.rb

Instance Method Summary collapse

Instance Method Details

#decide_primary_tag(restaurant_id) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'app/workers/tagging/distance_helper.rb', line 13

def decide_primary_tag(restaurant_id)
  @location_ids ||= RestaurantTag.primary_locations_scope.pluck(:id)

  restaurant = Restaurant.find restaurant_id
  primary_tag = restaurant.restaurant_tags_restaurants.joins(:restaurant_tag).where(restaurant_tags: { id: @location_ids }).where.not(distance_to_restaurant: nil).sort_by(&:distance_to_restaurant).first

  if primary_tag
    operation = PrimaryTagCpt::Operations::UpdateRelation.call(id: primary_tag.restaurant_tag_id, restaurant_id: restaurant.id)
    raise ActiveRecord::Rollback, "Failed to tag restaurant id #{restaurant.id}" unless operation.success?
  end
end

#find_distance_between(restaurant:, restaurant_tag:) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'app/workers/tagging/distance_helper.rb', line 3

def find_distance_between(restaurant:, restaurant_tag:)
  # Return nil if either restaurant or restaurant_tag is nil
  return nil if restaurant.nil? || restaurant_tag.nil?

  return nil if [restaurant&.lat, restaurant&.lng, restaurant_tag&.lat, restaurant_tag&.lng].compact.empty?

  distance = Geocoder::Calculations.distance_between([restaurant.lat, restaurant.lng], [restaurant_tag.lat, restaurant_tag.lng])
  distance.nan? ? nil : distance
end