Class: Spree::Review

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ransackable_associationsObject



39
40
41
42
43
44
45
# File 'app/models/spree/review.rb', line 39

def self.ransackable_associations(*)
  [
    "feedback_reviews",
    "product",
    "user"
  ]
end

.ransackable_attributesObject



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

def self.ransackable_attributes(*)
  [
    "approved",
    "name",
    "review",
    "title"
  ]
end

Instance Method Details

#approve_reviewObject



78
79
80
81
82
83
84
85
# File 'app/models/spree/review.rb', line 78

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



57
58
59
# File 'app/models/spree/review.rb', line 57

def email
  user&.email
end

#feedback_starsObject



47
48
49
50
51
# File 'app/models/spree/review.rb', line 47

def feedback_stars
  return 0 if feedback_reviews.size <= 0

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

#recalculate_product_ratingObject



53
54
55
# File 'app/models/spree/review.rb', line 53

def recalculate_product_rating
  product.recalculate_rating if product.present?
end

#star_only?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/models/spree/review.rb', line 74

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

#verify_purchaserObject



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

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