Module: ActsAsSlugged
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/acts_as_slugged.rb
Overview
ActsAsSlugged
This module automatically generates slugs based on the :to_s field using a before_validation filter
Mark your model with ‘acts_as_slugged’ make sure you have a string field :slug
Defined Under Namespace
Modules: Base, ClassMethods, FinderMethods
Instance Method Summary collapse
-
#build_slug ⇒ Object
Instance Methods.
- #reload(options = nil) ⇒ Object
- #to_global_id(**params) ⇒ Object
- #to_param ⇒ Object
Instance Method Details
#build_slug ⇒ Object
Instance Methods
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/models/concerns/acts_as_slugged.rb', line 59 def build_slug slug = to_s.parameterize.downcase[0, 250] if self.class.excluded_slugs.include?(slug) slug = "#{slug}-#{self.class.name.demodulize.parameterize}" end if (count = self.class.where(slug: slug).count) > 0 slug = "#{slug}-#{count+1}" end slug end |
#reload(options = nil) ⇒ Object
82 83 84 85 86 87 |
# File 'app/models/concerns/acts_as_slugged.rb', line 82 def reload( = nil) self.class.instance_variable_set(:@_effective_reloading, true) retval = super self.class.instance_variable_set(:@_effective_reloading, nil) retval end |
#to_global_id(**params) ⇒ Object
77 78 79 80 |
# File 'app/models/concerns/acts_as_slugged.rb', line 77 def to_global_id(**params) params[:tenant] = Tenant.current if defined?(Tenant) GlobalID.new(URI::GID.build(app: Rails.application.config.global_id.app, model_name: model_name, model_id: to_param, params: params)) end |
#to_param ⇒ Object
73 74 75 |
# File 'app/models/concerns/acts_as_slugged.rb', line 73 def to_param slug_was || slug end |