-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathreview.rb
More file actions
44 lines (37 loc) · 1.73 KB
/
Copy pathreview.rb
File metadata and controls
44 lines (37 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Spree::Review < ActiveRecord::Base
belongs_to :product, touch: true
belongs_to :user, class_name: Spree.user_class.to_s
has_many :feedback_reviews
after_save :recalculate_product_rating
after_destroy :recalculate_product_rating
validates :name, :review, presence: true
validates :rating, numericality: {
only_integer: true,
greater_than_or_equal_to: 1,
less_than_or_equal_to: 5,
message: Spree.t(:you_must_enter_value_for_rating)
}
default_scope { order('spree_reviews.created_at DESC') }
scope :localized, ->(lc) { where('spree_reviews.locale = ?', lc) }
scope :most_recent_first, -> { order('spree_reviews.created_at DESC') }
scope :oldest_first, -> { reorder('spree_reviews.created_at ASC') }
scope :preview, -> { limit(Spree::Reviews::Config[:preview_size]).oldest_first }
scope :approved, -> { where(approved: true) }
scope :not_approved, -> { where(approved: false) }
scope :default_approval_filter, -> { Spree::Reviews::Config[:include_unapproved_reviews] ? all : approved }
scope :raiting_five, -> { where(rating: '5').count }
scope :raiting_four, -> { where(rating: '4').count }
scope :raiting_three, -> { where(rating: '3').count }
scope :raiting_two, -> { where(rating: '2').count }
scope :raiting_one, -> { where(rating: '1').count }
enum product_worth: %i[yes no dont_know], _prefix: :product_worth
enum product_recommend: %i[yes no dont_know], _prefix: :product_recommend
enum vendor_recommend: %i[yes no dont_know], _prefix: :vendor_recommend
def feedback_stars
return 0 if feedback_reviews.size <= 0
((feedback_reviews.sum(:rating) / feedback_reviews.size) + 0.5).floor
end
def recalculate_product_rating
product.recalculate_rating if product.present?
end
end