Sha256: 6fb45a5e8900b2b1a36ee88d19789486acf600dfd71e1f5d3afa2d17b6708008

Contents?: true

Size: 759 Bytes

Versions: 5

Compression:

Stored size: 759 Bytes

Contents

# frozen_string_literal: true

require "cgi"

module EncodedId
  module Rails
    class SluggedId
      def initialize(from_object, slug_method: :name_for_encoded_id_slug, id_method: :id, separator: "--")
        @from_object = from_object
        @slug_method = slug_method
        @id_method = id_method
        @separator = separator
      end

      def slugged_id
        slug_part = @from_object.send(@slug_method)
        id_part = @from_object.send(@id_method)
        unless id_part.present? && slug_part.present?
          raise ::StandardError, "The model does not return a valid ID (:#{@id_method}) and/or slug (:#{@slug_method})"
        end
        "#{slug_part.to_s.parameterize}#{CGI.escape(@separator)}#{id_part}"
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
encoded_id-rails-0.6.2 lib/encoded_id/rails/slugged_id.rb
encoded_id-rails-0.6.1 lib/encoded_id/rails/slugged_id.rb
encoded_id-rails-0.6.0 lib/encoded_id/rails/slugged_id.rb
encoded_id-rails-0.5.0 lib/encoded_id/rails/slugged_id.rb
encoded_id-rails-0.4.0 lib/encoded_id/rails/slugged_id.rb