Class: Spree::Review

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/review.rb

Instance Method Summary collapse

Instance Method Details

#approve_reviewObject



64
65
66
67
68
69
70
71
# File 'app/models/spree/review.rb', line 64

def approve_review
  # Checks if we should auto approve the review.
  if Spree::Reviews::Config[:approve_star_only]
    self.approved = true if star_only?
  elsif Spree::Reviews::Config[:approve_star_only_for_verified_purchaser]
    self.approved = true if star_only? && verified_purchaser?
  end
end

#emailObject



43
44
45
# File 'app/models/spree/review.rb', line 43

def email
  user&.email
end

#feedback_starsObject



33
34
35
36
37
# File 'app/models/spree/review.rb', line 33

def feedback_stars
  return 0 if feedback_reviews.size <= 0

  ((feedback_reviews.sum(:rating) / feedback_reviews.size) + 0.5).floor
end

#recalculate_product_ratingObject



39
40
41
# File 'app/models/spree/review.rb', line 39

def recalculate_product_rating
  product.recalculate_rating if product.present?
end

#star_only?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'app/models/spree/review.rb', line 60

def star_only?
  [title, review].all?(&:blank?) && rating.present?
end

#verify_purchaserObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/spree/review.rb', line 47

def verify_purchaser
  return unless user_id && product_id

  verified_purchase = Spree::LineItem.joins(:order, :variant)
                                     .where.not(spree_orders: { completed_at: nil })
                                     .find_by(
                                       spree_variants: { product_id: product_id },
                                       spree_orders: { user_id: user_id }
                                     ).present?

  self.verified_purchaser = verified_purchase
end