Skip to content

Commit

Permalink
product.rb and taxon.rb models updated to enable saving slugs and per…
Browse files Browse the repository at this point in the history
…malinks
  • Loading branch information
kpokorski committed Aug 28, 2023
1 parent 03c6b60 commit 86f8a42
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 13 additions & 0 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,24 @@ class Product < Spree::Base
translates(*TRANSLATABLE_FIELDS)

self::Translation.class_eval do
before_update :set_slug
acts_as_paranoid
# deleted translation values also need to be accessible for index views listing deleted resources
default_scope { unscope(where: :deleted_at) }
def set_slug
if self.name.blank? & self.slug.blank?
self.slug = translated_model.translations.find_by(locale: translated_model.stores.first.default_locale).name.to_url
else
if self.slug.blank?
self.slug = self.name.to_url
else
self.slug = self.slug.to_url
end
end
end
end


friendly_id :slug_candidates, use: [:history, :mobility]
acts_as_paranoid
auto_strip_attributes :name
Expand Down
6 changes: 4 additions & 2 deletions core/app/models/spree/taxon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ class Taxon < Spree::Base

self::Translation.class_eval do
alias_attribute :slug, :permalink

before_create :set_permalink
before_update :set_permalink

def set_permalink
parent = translated_model.parent
name_with_fallback = name || translated_model.name

if self.name.blank? & self.permalink.blank?
self.permalink = translated_model.name.to_url
end
if parent.present?
self.permalink = [parent.permalink, (permalink.blank? ? name_with_fallback.to_url : permalink.split('/').last)].join('/')
else
Expand Down

0 comments on commit 86f8a42

Please sign in to comment.