Module: Virtuaservices::Concerns::Sluggable::ClassMethods

Defined in:
lib/virtuaservices/concerns/sluggable.rb

Overview

Module holding the class methods for the classes including this concern.

Author:

Instance Method Summary collapse

Instance Method Details

#make_sluggable(entity_type) ⇒ Object

Add the field and its validations in the model including it.

Parameters:

  • entity_type (String, Symbol)

    the name of the model including it, to be included in the error messages.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/virtuaservices/concerns/sluggable.rb', line 13

def make_sluggable(entity_type)
  # @!attribute [rw] slug
  #   @return [String] the slug of the current entity ; it must be snake-cased, longer than four characters, unique for the entity and given.
  field :slug, type: String

  validates :slug,
    length: {minimum: 4, message: 'minlength', if: :slug?},
    format: {with: /\A[a-z]+(_[a-z]+)*\z/, message: 'pattern', if: :slug?},
    uniqueness: {message: 'uniq', if: :slug?},
    presence: {message: 'required'}
end