Sha256: cbe7c61135f0a4a62ca46d7271bcafb9ce2068576afbaa2bfe0b15fc1329a3c5

Contents?: true

Size: 1.47 KB

Versions: 62

Compression:

Stored size: 1.47 KB

Contents

module Workarea
  class RedirectNavigableSlugs
    include Sidekiq::Worker
    include Sidekiq::CallbacksWorker
    include I18n::DefaultUrlOptions

    sidekiq_options(
      enqueue_on: {
        Navigable => :update,
        with: -> { [self.class.name, id, changes] },
        ignore_if: -> { changes['slug'].blank? }
      },
      queue: 'low'
    )

    # 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 {Workarea::Admin::ApplicationController} and can be
    # enabled on demand through the {Sidekiq::Callbacks} interface.
    def self.enabled?
      !!enabled
    end

    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

    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

    def storefront_routes
      Storefront::Engine.routes.url_helpers
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.5.26 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.4.45 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.5.25 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.5.23 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.4.44 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.5.22 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.4.43 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.5.21 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.4.42 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.5.20 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.4.41 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.5.19 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.4.40 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.5.18 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.4.39 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.5.17 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.4.38 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.5.16 app/workers/workarea/redirect_navigable_slugs.rb
workarea-core-3.4.37 app/workers/workarea/redirect_navigable_slugs.rb