module SpreeMultiDomain
  module ActionView
    module TemplateRendererDecorator
      def find_layout(layout, *args)
        unless api_request?
          if @view.respond_to?(:current_store) && @view.current_store && !@view.controller.is_a?(::Spree::Admin::BaseController)
            store_layout = if layout.is_a?(String)
              layout.gsub("layouts/", "layouts/#{@view.current_store.code}/")
            else
              layout.call.try(:gsub, "layouts/", "layouts/#{@view.current_store.code}/")
            end

            begin
              super(store_layout, *args)
            rescue ::ActionView::MissingTemplate
              super(layout, *args)
            end
          else
            super(layout, *args)
          end
        end
      end

      def controller_name
        # check if it's not dangerous to call the method that way
        @view.controller.class.to_s
      end

      def api_request?
        return true if controller_name.include?('::Api::')
      end

      ::ActionView::TemplateRenderer.prepend self
    end
  end
end

module SpreeMultiDomain
  module ActionView
    module TemplateRendererDecorator
      if Rails.gem_version >= Gem::Version.new('6.0.0')
        def render_template(view, template, layout_name, locals)
          @view = view
          super
        end
      end

      def find_layout(layout, *args)
        unless api_request?
          if @view.respond_to?(:current_store) && @view.current_store && !@view.controller.is_a?(::Spree::Admin::BaseController)
            store_layout = if layout.is_a?(String)
              layout.gsub("layouts/", "layouts/#{@view.current_store.code}/")
            else
              layout.call.try(:gsub, "layouts/", "layouts/#{@view.current_store.code}/")
            end

            begin
              super(store_layout, *args)
            rescue ::ActionView::MissingTemplate
              super(layout, *args)
            end
          else
            super(layout, *args)
          end
        end
      end

      def controller_name
        # check if it's not dangerous to call the method that way
        @view.controller.class.to_s
      end

      def api_request?
        return true if controller_name.include?('::Api::')
      end

      ::ActionView::TemplateRenderer.prepend self
    end
  end
end

Fix ActionView::TemplateRendererDecorator not supporting Rails 6
Fix ActionView::TemplateRendererDecorator not supporting Rails 6

Starting with Rails 6, ActionView::TemplateRenderer passes the
`view` object around rather than storing it in an ivar[1]. Since
we need this object in `find_layout`, we need to use the previous
behavior in our decorator.

[1]: https://github.com/rails/rails/commit/1853b0d0abf87dfdd4c3a277c3badb17ca19652e
