Sha256: 7c551729a3abd395e3dd21c943791a2130ec052861dbda7500923a5fb3a48f20

Contents?: true

Size: 1.3 KB

Versions: 34

Compression:

Stored size: 1.3 KB

Contents

# Make redirects for SEO needs
module Spree
  module Core
    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

34 entries across 34 versions & 2 rubygems

Version Path
spree_core-1.3.5 lib/spree/core/middleware/seo_assist.rb
spree_core-1.3.4 lib/spree/core/middleware/seo_assist.rb
spree_core-1.3.3 lib/spree/core/middleware/seo_assist.rb
spree_core-1.2.5 lib/spree/core/middleware/seo_assist.rb
spree_core-1.1.6 lib/spree/core/middleware/seo_assist.rb
spree_core-1.1.5 lib/spree/core/middleware/seo_assist.rb
spree_core-1.2.4 lib/spree/core/middleware/seo_assist.rb
spree_core-1.3.2 lib/spree/core/middleware/seo_assist.rb
spree_core-1.0.7 lib/spree/core/middleware/seo_assist.rb
spree_core-1.2.3 lib/spree/core/middleware/seo_assist.rb
spree_core-1.3.1 lib/spree/core/middleware/seo_assist.rb
spree_core-1.3.0 lib/spree/core/middleware/seo_assist.rb
spree_core-1.3.0.rc2 lib/spree/core/middleware/seo_assist.rb
dup_spree_core-1.3.0.rc1 lib/spree/core/middleware/seo_assist.rb
spree_core-1.3.0.rc1 lib/spree/core/middleware/seo_assist.rb
spree_core-1.2.2 lib/spree/core/middleware/seo_assist.rb
spree_core-1.1.4 lib/spree/core/middleware/seo_assist.rb
spree_core-1.2.0 lib/spree/core/middleware/seo_assist.rb
spree_core-1.2.0.rc2 lib/spree/core/middleware/seo_assist.rb
spree_core-1.2.0.rc1 lib/spree/core/middleware/seo_assist.rb