Class: RoomTypeSlugConstraint

Inherits:
Object
  • Object
show all
Defined in:
lib/room_type_slug_constraint.rb

Overview

Routing constraint that only matches when params[:room_type] is
the slug of a real RoomType. Lets the public site keep nice
/floor-heating/bathroom-style URLs without 404'ing on arbitrary
garbage path segments.

Instance Method Summary collapse

Instance Method Details

#matches?(request) ⇒ Boolean

Matches only when the provided slug corresponds to an existing RoomType

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
# File 'lib/room_type_slug_constraint.rb', line 8

def matches?(request)
  slug = request.params[:room_type].to_s
  return false if slug.blank?

  RoomType.exists?([
                     "seo_key = :slug OR LOWER(friendly_name) = :slug OR LOWER(REPLACE(name, ' ', '-')) = :slug",
                     { slug: slug.downcase }
                   ])
end