Module: ModelExt::Restaurants::PrimaryTags

Included in:
Restaurant
Defined in:
lib/model_ext/restaurants/primary_tags.rb

Overview

instance methods regarding primary tags relation for restaurant

Instance Method Summary collapse

Instance Method Details

#primary_tag_by_category(category) ⇒ PrimaryTag?

Finds the primary tag for a given category.

If the category is a location category, it tries to find a matching tag. If not found, it falls back to the popular zone category. For other categories, it finds the first matching tag.

Parameters:

  • category (String)

    the category to search for

Returns:

  • (PrimaryTag, nil)

    the found primary tag or nil if not found



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/model_ext/restaurants/primary_tags.rb', line 13

def primary_tag_by_category(category)
  return nil if category.blank? || primary_tags.blank?

  if RestaurantTag::LOCATION_CATEGORIES.include?(category)
    primary_tag_location = primary_tags.joins(:restaurant_tag).
      where('restaurant_tags.title_en LIKE ?', "#{category}%")&.first

    # if primary_tag_location is nil, try to find popularzone by default
    primary_tag_location.presence || primary_tags.joins(:restaurant_tag).
      where('restaurant_tags.title_en LIKE ?', "#{RestaurantTag::POPULAR_ZONE}%")&.first
  else
    primary_tags.joins(:restaurant_tag).
      where('restaurant_tags.title_en LIKE ?', "#{category}%")&.first
  end
end