diff --git a/app/controllers/spree/reviews_controller.rb b/app/controllers/spree/reviews_controller.rb index 1ee8e2e3..e51b09ab 100644 --- a/app/controllers/spree/reviews_controller.rb +++ b/app/controllers/spree/reviews_controller.rb @@ -39,7 +39,7 @@ def load_product end def permitted_review_attributes - [:rating, :title, :review, :name, :show_identifier] + [:rating, :title, :review, :name, :show_identifier, :product_worth, :product_recommend, :vendor_recommend] end def review_params diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 0fb8a6ab..297fa4b8 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -9,10 +9,12 @@ def stars avg_rating.try(:round) || 0 end + + def recalculate_rating - self[:reviews_count] = reviews.reload.approved.count + self[:reviews_count] = reviews.count self[:avg_rating] = if reviews_count > 0 - reviews.approved.sum(:rating).to_f / reviews_count + reviews.sum(:rating).to_f / reviews.count else 0 end diff --git a/app/models/spree/review.rb b/app/models/spree/review.rb index 98398992..58f12861 100644 --- a/app/models/spree/review.rb +++ b/app/models/spree/review.rb @@ -3,17 +3,15 @@ class Spree::Review < ActiveRecord::Base belongs_to :user, class_name: Spree.user_class.to_s has_many :feedback_reviews - after_save :recalculate_product_rating, if: :approved? + 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) } @@ -23,6 +21,15 @@ class Spree::Review < ActiveRecord::Base scope :approved, -> { where(approved: true) } scope :not_approved, -> { where(approved: false) } scope :default_approval_filter, -> { Spree::Reviews::Config[:include_unapproved_reviews] ? all : approved } + scope :rating_with_five, -> { where(rating: '5').count } + scope :rating_with_four, -> { where(rating: '4').count } + scope :rating_with_three, -> { where(rating: '3').count } + scope :rating_with_two, -> { where(rating: '2').count } + scope :rating_with_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 diff --git a/app/overrides/add_reviews_to_admin_configuration_sidebar.rb b/app/overrides/add_reviews_to_admin_configuration_sidebar.rb index e9032cd0..29e512c4 100644 --- a/app/overrides/add_reviews_to_admin_configuration_sidebar.rb +++ b/app/overrides/add_reviews_to_admin_configuration_sidebar.rb @@ -2,5 +2,5 @@ virtual_path: 'spree/admin/shared/sub_menu/_configuration', name: 'converted_admin_configurations_menu', insert_bottom: '[data-hook="admin_configurations_sidebar_menu"]', - text: '<%= configurations_sidebar_menu_item Spree.t(:review_settings, scope: :spree_reviews), edit_admin_review_settings_path %>' + text: '<%= configurations_sidebar_menu_item(Spree.t(:review_settings, scope: :spree_reviews), edit_admin_review_settings_path) if can? :manage, Spree::Review %>' ) diff --git a/app/views/spree/admin/reviews/index.html.erb b/app/views/spree/admin/reviews/index.html.erb index 6ff82328..7eeff657 100644 --- a/app/views/spree/admin/reviews/index.html.erb +++ b/app/views/spree/admin/reviews/index.html.erb @@ -67,7 +67,7 @@
<% @reviews.each do |review| %>Is this product worth?
+ <%= f.radio_button :product_worth, "yes", style: "display:none" %> YES + <%= f.radio_button :product_worth, "no", style: "display:none" %> NO + <%= f.radio_button :product_worth, "dont_know", style: "display:none" %> Don't knowDo you recommend this product?
+ <%= f.radio_button :product_recommend, "yes", style: "display:none" %> YES + <%= f.radio_button :product_recommend, "no", style: "display:none" %> NO + <%= f.radio_button :product_recommend, "dont_know", style: "display:none" %> Don't knowWas the experience with the vendor good?
+ <%= f.radio_button :vendor_recommend, "yes", style: "display:none" %> YES + <%= f.radio_button :vendor_recommend, "no", style: "display:none" %> NO + <%= f.radio_button :vendor_recommend, "dont_know", style: "display:none" %> Don't know<%= Spree.t(:no_reviews_available) %>
<% else %> @@ -15,11 +15,9 @@ <%= render 'spree/shared/review', review: review %> <% end %> <% end %> - <%= link_to Spree.t(:write_your_own_review), new_product_review_path(@product), class: 'btn btn-primary', rel: 'nofollow' %> <%= Spree.t(:or) %> <%= link_to Spree.t(:back_to) + " " +@product.name, product_path(@product), class: 'btn btn-outline-primary', rel: 'nofollow' %> - <% if Spree.version.to_f < 4.0 %> <% if @approved_reviews.respond_to?(:total_pages) %><%= review.review %>
+<%= Spree.t(:no_reviews_available) %>
+<%= Spree.t(:no_reviews_available) %>
+"> <%= Spree.t(:write_your_own_review) %>
+You need to login to Post a review
+ <% end %> + <% else %> <%= render 'spree/shared/rating', product: @product, review: 0 %> +"> <%= Spree.t(:write_your_own_review) %>
+You need to login to Post a review
+ <% end %> <% for review in (Spree::Reviews::Config[:track_locale] ? @product.reviews.localized(I18n.locale) : @product.reviews).default_approval_filter.preview %> <%= render 'spree/shared/review', review: review %> <% end %> <% end %> - <%= link_to Spree.t(:write_your_own_review), new_product_review_path(@product), class: 'btn btn-primary', rel: 'nofollow' %> - - <% unless Spree::Reviews::Config[:include_unapproved_reviews] == false and @product.reviews.approved.count == 0 %> - <%= Spree.t(:or) %> - <%= link_to Spree.t(:read_all_reviews), product_reviews_path(@product), class: 'btn btn-primary', rel: 'nofollow' %> - <% end %> -