Skip to content

Commit 455b04b

Browse files
committed
Allow models to override slug parameterization default
We are presently using slugs for Pages with forward slashes (to show nesting), and parameterize replaces forward slashes with dashes
1 parent 5942630 commit 455b04b

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

app/models/better_together/page.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class Page < ApplicationRecord
3434

3535
slugged :title, min_length: 1
3636

37+
self.parameterize_slug = false # Allows us to keep forward slashes in the slug (for now)
38+
3739
# Validations
3840
validates :title, presence: true
3941
validates :layout, inclusion: { in: PAGE_LAYOUTS }, allow_blank: true

app/models/concerns/better_together/friendly_slug.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module FriendlySlug
99
include Translatable
1010
extend ::FriendlyId
1111

12+
class_attribute :parameterize_slug, default: true
13+
1214
# This method must be called or the class will have validation issues
1315
def self.slugged(attribute, **options)
1416
translates :slug, type: :string
@@ -29,7 +31,8 @@ def self.slugged(attribute, **options)
2931
end
3032

3133
def slug= arg, locale: nil, **options
32-
super(arg&.parameterize, locale:, **options)
34+
arg = arg&.parameterize if self.class.parameterize_slug
35+
super(arg&.strip, locale:, **options)
3336
end
3437
end
3538
end

app/models/concerns/better_together/identifier.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ module Identifier
2020
before_validation :generate_identifier
2121

2222
def identifier=(arg)
23-
self.slug = super(arg.parameterize)
23+
arg = arg&.parameterize if self.class.parameterize_slug
24+
self.slug = super(arg&.strip)
2425
end
2526

2627
def to_param

0 commit comments

Comments
 (0)