Module: Elabs::Concerns::Sluggable

Extended by:
ActiveSupport::Concern
Included in:
ApplicationContentRecord, Language, License, Tag, User
Defined in:
app/models/elabs/concerns/sluggable.rb

Overview

For this concern to work, a SLUGGABLE_FIELD constant should be declared in the model

Instance Method Summary collapse

Instance Method Details

#fill_slugObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/elabs/concerns/sluggable.rb', line 11

def fill_slug
  return unless self.class::SLUGGABLE_FIELD

  slug       = make_slug_from_field
  slug_field = self.class::SLUG_FIELD || :slug

  same_slugs = self.class
                   .where("#{slug_field} LIKE ?", "#{slug}%")
                   .pluck(slug_field)
                   .count { |r| r[/^#{slug}(-\d+)?$/] }

  slug += "-#{same_slugs}" if same_slugs.positive?

  self.slug = slug
end

#to_paramObject



27
28
29
# File 'app/models/elabs/concerns/sluggable.rb', line 27

def to_param
  slug
end