Sha256: 94eb0beacb3f01c4d90d5b5ef7ea0d35a28930865c1400e9be2b1d8373627fa0

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

module Workarea
  module Storefront
    class ReviewRequestsController < Storefront::ApplicationController
      before_action :validate_request
      before_action :find_product, :set_review

      def show; end

      def complete
        if verify_recaptcha(model: @review, env: Rails.env) && @review.save
          @request.complete!
          Review::Request.cancel_for_orders!(@request.order_id)

          flash[:success] = t('workarea.storefront.reviews.flash_messages.created')
          redirect_to product_path(@product)
        else
          flash[:error] = t('workarea.storefront.reviews.flash_messages.failure')
          render :show
        end
      end

      private

      def validate_request
        @request = Review::Request.find_by_token(params[:id])

        if @request.nil? || @request.completed?
          flash[:error] = t('workarea.storefront.review_requests.flash_messages.already_submitted')
          redirect_to(root_path) && (return)
        end
      end

      def find_product
        model = Catalog::Product.find(@request.product_id)
        @product = ProductViewModel.wrap(model, view_model_options)
      end

      def set_review
        @review = ReviewViewModel.wrap(Review.new(review_params))
      end

      def review_params
        ReviewRequestParams.new(
          @request,
          params[:review]&.permit(:rating, :title, :body)&.to_h || {}
        ).to_h
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
workarea-reviews-3.1.2 app/controllers/workarea/storefront/review_requests_controller.rb
workarea-reviews-3.1.1 app/controllers/workarea/storefront/review_requests_controller.rb
workarea-reviews-3.1.0 app/controllers/workarea/storefront/review_requests_controller.rb
workarea-reviews-3.0.10 app/controllers/workarea/storefront/review_requests_controller.rb
workarea-reviews-3.0.9 app/controllers/workarea/storefront/review_requests_controller.rb
workarea-reviews-3.0.8 app/controllers/workarea/storefront/review_requests_controller.rb