Class: Workarea::RedirectNavigableSlugs

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::CallbacksWorker, Sidekiq::Worker, I18n::DefaultUrlOptions
Defined in:
app/workers/workarea/redirect_navigable_slugs.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from I18n::DefaultUrlOptions

#default_url_options

Class Method Details

.enabled?Boolean

Do not enable by default. We do not want this to run in bulk tasks such as importing or bulk actions. However, this is enabled for all admin activity from the Admin::ApplicationController and can be enabled on demand through the Sidekiq::Callbacks interface.

Returns:

  • (Boolean)


20
21
22
# File 'app/workers/workarea/redirect_navigable_slugs.rb', line 20

def self.enabled?
  !!enabled
end

Instance Method Details



41
42
43
44
45
# File 'app/workers/workarea/redirect_navigable_slugs.rb', line 41

def navigable_path(model, url_params = nil)
  params = default_url_options.merge(id: url_params || model)
  resource_name = model.class.model_name.element
  storefront_routes.send("#{resource_name}_path", params)
end

#perform(class_name, id, changes) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/workers/workarea/redirect_navigable_slugs.rb', line 24

def perform(class_name, id, changes)
  old_slug, _new_slug = changes['slug']

  model = class_name.constantize.find_or_initialize_by(id: id)
  return unless model.persisted?

  I18n.for_each_locale do
    old_path = navigable_path(model, old_slug)
    next if Navigation::Redirect.find_by_path(old_path).present?

    Navigation::Redirect.create(
      path: old_path,
      destination: navigable_path(model)
    )
  end
end

#storefront_routesObject



47
48
49
# File 'app/workers/workarea/redirect_navigable_slugs.rb', line 47

def storefront_routes
  Storefront::Engine.routes.url_helpers
end