Module: ModelExt::Restaurants::FriendlyIdSetup

Extended by:
ActiveSupport::Concern
Included in:
Restaurant
Defined in:
lib/model_ext/restaurants/friendly_id_setup.rb

Instance Method Summary collapse

Instance Method Details

#city_country_nameObject

Fetch the city and country name (if present)



62
63
64
65
66
# File 'lib/model_ext/restaurants/friendly_id_setup.rb', line 62

def city_country_name
  return nil unless city_id.present?

  fetch_city.country_name
end

#city_nameObject

Fetch the city name (if present)



55
56
57
58
59
# File 'lib/model_ext/restaurants/friendly_id_setup.rb', line 55

def city_name
  return nil unless city_id.present?

  fetch_city.name
end

#city_name_changed?Boolean

Check if the city name has changed or is missing in the current slug

Returns:

  • (Boolean)


45
46
47
# File 'lib/model_ext/restaurants/friendly_id_setup.rb', line 45

def city_name_changed?
  city_id_changed? && !slug_includes_city_name?
end

#name_en_with_cityObject

Fetch the name combined with city



33
34
35
36
37
38
39
40
41
42
# File 'lib/model_ext/restaurants/friendly_id_setup.rb', line 33

def name_en_with_city
  return name_en if city_name.blank?

  # Check if the city name is already part of name_en
  if name_en.downcase.include?(city_name.downcase)
    name_en.parameterize
  else
    "#{name_en}-#{city_name.parameterize}"
  end
end

#slug_candidatesObject

Generate slug candidates

  • name_en_with_city: This is the base candidate, which combines the restaurant's English name (name_en) with the city name (if available).

  • [:name_en_with_city, city_country_name]: If the first candidate is not unique, this candidate adds the country name of the city to the slug for additional specificity.

  • [:name_en_with_city, city_country_name, id]: If the second candidate is still not unique, this candidate adds the restaurant's unique id to ensure a completely unique slug.



24
25
26
27
28
29
30
# File 'lib/model_ext/restaurants/friendly_id_setup.rb', line 24

def slug_candidates
  [
    name_en_with_city,
    [:name_en_with_city, city_country_name],
    [:name_en_with_city, city_country_name, id],
  ]
end

#slug_includes_city_name?Boolean

Check if the slug already includes the city name

Returns:

  • (Boolean)


50
51
52
# File 'lib/model_ext/restaurants/friendly_id_setup.rb', line 50

def slug_includes_city_name?
  city_name.present? && slug.include?(city_name.parameterize)
end