Sha256: f32c59fa981aa532c3a13fdf2969feaaa6ba5bbe59c74f44ca69d9c749beaf16

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

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, :if => :approved?
  after_destroy :recalculate_product_rating

  validates :name, presence: true
  validates :review, presence: true

  validates :rating, numericality: { only_integer: false,
                                     greater_than_or_equal_to: 0.5,
                                     less_than_or_equal_to: 5.0,
                                     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 }

  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
    self.product.recalculate_rating if product.present?
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jiffyshirts_spree_reviews-2.3.1.2 app/models/spree/review.rb
jiffyshirts_spree_reviews-2.3.1.1 app/models/spree/review.rb