Sha256: bfa4eff4a4106b3c1f4c2029ba0a86658c3d49c5d7fa8e0bc025f6ce081190d4

Contents?: true

Size: 1.74 KB

Versions: 16

Compression:

Stored size: 1.74 KB

Contents

# FriendlyID support methods
# In general, 
# - extends FriendlyID
# - requires slug to be present
# - normalizes slug using Babosa gem.  Ensures normalized even if user inputed
# - indicates to generate a new slug if it's blank
#
# To use:
#   extend FriendlyId
#   include DmCore::Concerns::FriendlyId
#
#   def model_slug
#     send("title_#{Account.current.preferred_default_locale}")
#   end

#------------------------------------------------------------------------------
module DmCore
  module Concerns
    module FriendlyId
      extend ActiveSupport::Concern

      included do

        friendly_id               :model_slug, use: :scoped, scope: :account_id
        validates_presence_of     :slug
        before_save               :normalize_slug

        # Override this to provide what the slug value should be based on
        #------------------------------------------------------------------------------
        def model_slug
          slug
        end

        # regenerate slug if it's blank
        #------------------------------------------------------------------------------
        def should_generate_new_friendly_id?
          self.slug.blank?
        end

        # If user set slug sepcifically, we need to make sure it's been normalized
        #------------------------------------------------------------------------------
        def normalize_slug
          self.slug = normalize_friendly_id(self.slug)
        end
  
        # use babosa gem (to_slug) to allow better handling of multi-language slugs
        #------------------------------------------------------------------------------
        def normalize_friendly_id(text)
          text.to_s.to_slug.normalize.to_s
        end
      end

      module ClassMethods
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
dm_core-4.2.3.10 app/models/dm_core/concerns/friendly_id.rb
dm_core-4.2.3.9 app/models/dm_core/concerns/friendly_id.rb
dm_core-4.2.3.8 app/models/dm_core/concerns/friendly_id.rb
dm_core-4.2.3.7 app/models/dm_core/concerns/friendly_id.rb
dm_core-4.2.3.6 app/models/dm_core/concerns/friendly_id.rb
dm_core-4.2.3.5 app/models/dm_core/concerns/friendly_id.rb
dm_core-4.2.3.4 app/models/dm_core/concerns/friendly_id.rb
dm_core-4.2.3.3 app/models/dm_core/concerns/friendly_id.rb
dm_core-4.2.3.2 app/models/dm_core/concerns/friendly_id.rb
dm_core-4.2.3.1 app/models/dm_core/concerns/friendly_id.rb
dm_core-4.2.3 app/models/dm_core/concerns/friendly_id.rb
dm_core-4.2.2.3 app/models/dm_core/concerns/friendly_id.rb
dm_core-4.2.2.2 app/models/dm_core/concerns/friendly_id.rb
dm_core-4.2.2.1 app/models/dm_core/concerns/friendly_id.rb
dm_core-4.2.2 app/models/dm_core/concerns/friendly_id.rb
dm_core-4.2.1.5 app/models/dm_core/concerns/friendly_id.rb