Class: Spree::ReviewsController

Inherits:
StoreController
  • Object
show all
Defined in:
app/controllers/spree/reviews_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

save if all ok



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/spree/reviews_controller.rb', line 25

def create
  review_params[:rating].sub!(/\s*[^0-9]*\z/, '') if review_params[:rating].present?

  @review = Spree::Review.new(review_params)
  @review.product = @product
  @review.user = spree_current_user if spree_user_signed_in?
  @review.ip_address = request.remote_ip
  @review.locale = I18n.locale.to_s if Spree::Reviews::Config[:track_locale]
  # Handle images
  params[:review][:images]&.each do |image|
    @review.images.new(attachment: image) if image.present?
  end

  authorize! :create, @review
  if @review.save
    flash[:notice] = I18n.t('spree.review_successfully_submitted')
    redirect_to spree.product_path(@product)
  else
    render :new
  end
end

#editObject



16
17
18
19
20
21
22
# File 'app/controllers/spree/reviews_controller.rb', line 16

def edit
  @review = Spree::Review.find(params[:id])
  if @review.product.nil?
    flash[:error] = I18n.t('spree.error_no_product')
  end
  authorize! :update, @review
end

#indexObject



7
8
9
# File 'app/controllers/spree/reviews_controller.rb', line 7

def index
  @approved_reviews = Spree::Review.approved.where(product: @product)
end

#newObject



11
12
13
14
# File 'app/controllers/spree/reviews_controller.rb', line 11

def new
  @review = Spree::Review.new(product: @product)
  authorize! :create, @review
end

#updateObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/spree/reviews_controller.rb', line 47

def update
  review_params[:rating].sub!(/\s*[^0-9]*\z/, '') if params[:review][:rating].present?

  @review = Spree::Review.find(params[:id])

  # Handle images
  params[:review][:images]&.each do |image|
    @review.images.new(attachment: image) if image.present?
  end

  authorize! :update, @review
  if @review.update(review_params)
    flash[:notice] = I18n.t('spree.review_successfully_submitted')
    redirect_to spree.product_path(@product)
  else
    render :edit
  end
end