Module: Spree::PrefixedId
- Extended by:
- ActiveSupport::Concern
- Included in:
- AdminUserMethods, Base, UserMethods
- Defined in:
- app/models/concerns/spree/prefixed_id.rb
Overview
Adds Stripe-style prefixed IDs to Spree models using Sqids encoding. IDs are computed on the fly from integer primary keys – no database column needed.
e.g., Product with id=12345 -> “prod_86Rf07xd4z”
class Product < Spree.base_class
has_prefix_id :prod
end
Constant Summary collapse
- SQIDS =
Sqids.new(min_length: 10)
Instance Method Summary collapse
-
#prefixed_id ⇒ Object
Returns the Stripe-style prefixed ID, or nil for unsaved records.
-
#to_param ⇒ Object
Use prefixed_id for URL params when available.
Instance Method Details
#prefixed_id ⇒ Object
Returns the Stripe-style prefixed ID, or nil for unsaved records.
24 25 26 27 28 |
# File 'app/models/concerns/spree/prefixed_id.rb', line 24 def prefixed_id return nil unless id.present? "#{self.class._prefix_id_prefix}_#{Spree::PrefixedId::SQIDS.encode([id])}" end |
#to_param ⇒ Object
Use prefixed_id for URL params when available. Skip if FriendlyId is used (it has its own to_param using slug).
32 33 34 35 36 37 |
# File 'app/models/concerns/spree/prefixed_id.rb', line 32 def to_param return super if self.class.respond_to?(:friendly_id_config) return super unless self.class._prefix_id_prefix.present? prefixed_id.presence || super end |