Sha256: 964d0a36f7f2a18d8f75c8d519c7a9d1fda52e3f540f2572871f80ccf06dd121

Contents?: true

Size: 1.31 KB

Versions: 43

Compression:

Stored size: 1.31 KB

Contents

# Make redirects for SEO needs
module Spree
  module Frontend
    module Middleware
      class SeoAssist
        def initialize(app)
          @app = app
        end

        def call(env)
          request = Rack::Request.new(env)
          params = request.params

          taxon_id = params['taxon']

          # redirect requests using taxon id's to their permalinks
          if !taxon_id.blank? && !taxon_id.is_a?(Hash) && taxon = Taxon.find(taxon_id)
            params.delete('taxon')

            return build_response(params, "#{request.script_name}t/#{taxon.permalink}" )
          elsif env["PATH_INFO"] =~ /^\/(t|products)(\/\S+)?\/$/
            # ensures no trailing / for taxon and product urls

            return build_response(params, env["PATH_INFO"][0...-1])
          end

          @app.call(env)
        end

        private

        def build_response(params, location)
          query = build_query(params)
          location += '?' + query unless query.blank?
          [301, { 'Location' => location }, []]
        end

        def build_query(params)
          params.map { |k, v|
            if v.class == Array
              build_query(v.map { |x| ["#{k}[]", x] })
            else
              k + "=" + Rack::Utils.escape(v)
            end
          }.join("&")
        end
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
solidus_frontend-2.5.2 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.5.1 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.5.0 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.5.0.rc1 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.5.0.beta2 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.5.0.beta1 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.4.2 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.2.2 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.4.1 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.3.1 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.1.1 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.0.3 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-1.4.2 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-1.3.2 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.4.0 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.4.0.rc1 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.4.0.beta1 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.3.0 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.3.0.rc3 lib/spree/frontend/middleware/seo_assist.rb
solidus_frontend-2.3.0.rc2 lib/spree/frontend/middleware/seo_assist.rb