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| %> - <%= link_to_if review.product, review.product.name, product_path(review.product) %> + <%= link_to_if review.product, review&.product&.name, products_path(review.product) %> <%= txt_stars(review.rating) %> <%= link_to "(#{review.feedback_stars}/#{review.feedback_reviews.size})", admin_review_feedback_reviews_path(review) %> <%= review.user_id ? link_to(review.user.try(:email), admin_user_path(review.user)) : Spree.t(:anonymous) %> diff --git a/app/views/spree/reviews/_form.html.erb b/app/views/spree/reviews/_form.html.erb index a84e152e..3cbe7a67 100644 --- a/app/views/spree/reviews/_form.html.erb +++ b/app/views/spree/reviews/_form.html.erb @@ -1,38 +1,55 @@ <%= form_for review, url: product_reviews_path(product), html: { method: :post } do |f| %> - <%= render 'spree/shared/error_messages', target: review %> + <%= render 'spree/shared/error_messages', target: review %> -
- <%= f.label :rating %> - <%= render 'spree/reviews/stars', stars: review.rating, edit_enabled: true %> -
+
+ <%= f.label :rating %> + <%= render 'spree/reviews/stars', stars: review.rating, edit_enabled: true %> +
-
- <%= f.label :name %> - <%= f.text_field :name, maxlength: 255, size: 50, class: 'form-control', required: true %> +
+ <%= f.label :title %> + <%= f.text_field :title, maxlength: 20, size: 50, class: 'form-control' %> +
+ +
+ <%= f.label :review %> + Max limit 40 characters + <%= f.text_area :review, maxlength: 40, wrap: 'virtual', rows: 4, cols: 50, class: 'form-control', required: true %> +
+ +
+

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 know
-
- <%= f.label :title %> - <%= f.text_field :title, maxlength: 255, size: 50, class: 'form-control' %> +
+

Do 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 know
-
- <%= f.label :review %> - <%= f.text_area :review, wrap: 'virtual', rows: 10, cols: 50, class: 'form-control', required: true %> +
+

Was 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
- <% if Spree::Reviews::Config[:show_identifier] %> -
- <%= f.label :show_identifier do %> - <%= f.check_box :show_identifier, checked: true %> - <%= Spree::Review.human_attribute_name(:show_identifier) %> - <% end %> -
- <% end %> + <% if Spree::Reviews::Config[:show_identifier] %> +
+ <%= f.label :show_identifier do %> + <%= f.check_box :show_identifier, checked: true %> + <%= Spree::Review.human_attribute_name(:show_identifier) %> + <% end %> +
+ <% end %> -
- <%= f.submit Spree.t(:submit_your_review), class: 'btn btn-primary' %> - <%= Spree.t(:or) %> - <%= link_to Spree.t(:cancel), product_path(@product), class: 'btn btn-outline-primary', rel: 'nofollow' %> -
-<% end %> +
+ <%= f.submit Spree.t(:submit_your_review), class: 'btn btn-primary' %> + <%= Spree.t(:or) %> + <%= link_to Spree.t(:cancel), product_path(@product), class: 'btn btn-outline-primary', rel: 'nofollow' %> +
+ <% end %> \ No newline at end of file diff --git a/app/views/spree/reviews/index.html.erb b/app/views/spree/reviews/index.html.erb index ebc10a60..df2abfc1 100644 --- a/app/views/spree/reviews/index.html.erb +++ b/app/views/spree/reviews/index.html.erb @@ -1,7 +1,7 @@
-

<%= Spree.t(:reviews) %>

+

<%= Spree.t(:reviews) %>

<% if Spree::Reviews::Config[:include_unapproved_reviews] == false and @product.reviews.approved.count == 0 %>

<%= 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) %>
@@ -40,4 +38,4 @@ <% end %>
-
+
\ No newline at end of file diff --git a/app/views/spree/shared/_product_added_modal.html.erb b/app/views/spree/shared/_product_added_modal.html.erb new file mode 100644 index 00000000..fa0eca02 --- /dev/null +++ b/app/views/spree/shared/_product_added_modal.html.erb @@ -0,0 +1,25 @@ +<% @review = Spree::Review.new(product: @product) %> + diff --git a/app/views/spree/shared/_rating.html.erb b/app/views/spree/shared/_rating.html.erb index 619d1158..4e31a9bb 100644 --- a/app/views/spree/shared/_rating.html.erb +++ b/app/views/spree/shared/_rating.html.erb @@ -1,15 +1,72 @@ -<% stars = product.stars %> -<% reviews_count = product.reviews_count %> +<% @average_rating = product.reviews.count > 0 ? product.reviews.sum(:rating).to_f / product.reviews.count : 0 %> + +<% reviews_count = product.reviews.count %> +<% rating_with_five = reviews_count != 0 ? product.reviews.rating_with_five * 100 / reviews_count : 0 %> +<% rating_with_four = reviews_count != 0 ? product.reviews.rating_with_four * 100 / reviews_count : 0 %> +<% rating_with_three = reviews_count != 0 ? product.reviews.rating_with_three * 100 / reviews_count : 0 %> +<% rating_with_two = reviews_count != 0 ? product.reviews.rating_with_two * 100 / reviews_count : 0%> +<% rating_with_one = reviews_count != 0 ? product.reviews.rating_with_one * 100 / reviews_count : 0 %> +
-

<%= Spree.t(:average_customer_rating) %>:

-

- - <%= render 'spree/reviews/stars', stars: stars %> - +

+ Let us know your thoughts on a product or view reviews from our members, + independent experts and other websites.

-

(<%= Spree.t(:based_upon_review_count, count: reviews_count) %>)

+
+

+ <% if product.reviews.count > 0 %> + <%= @average_rating.try(:round) %> + <% else %> + 0 + <% end %> + + <%= render 'spree/reviews/stars', stars: @average_rating.try(:round) %> + +

+
+

<%= Spree.t(:based_upon_review_count, count: reviews_count) %>

+ +
+
+

5

+
+
+
+

<%= rating_with_five %>%

+
+
+

4

+
+
+
+

<%= rating_with_four %>%

+
+
+

3

+
+
+
+

<%= rating_with_three %>%

+
+
+

2

+
+
+
+

<%= rating_with_two %>%

+
+
+

1

+
+
+
+

<%= rating_with_one %>%

+
+
+ +
- +
-
+
\ No newline at end of file diff --git a/app/views/spree/shared/_review.html.erb b/app/views/spree/shared/_review.html.erb index 7350a620..99390105 100644 --- a/app/views/spree/shared/_review.html.erb +++ b/app/views/spree/shared/_review.html.erb @@ -1,11 +1,21 @@ -
+
- <%= render 'spree/reviews/stars', stars: review.rating %> + <%= render 'spree/reviews/stars', stars: review&.rating %> - <%= review.title %>
- <%= Spree.t(:submitted_on) %> - <%= l review.created_at.to_date %> + <%= review&.title %> +
+
+

<%= review.review %>

+
+ <% if Spree::Reviews::Config[:feedback_rating] && (!Spree::Reviews::Config[:require_login] || spree_current_user) %> + + <% end %> + + <%#= Spree.t(:submitted_on) %> + <%= l review.created_at.to_date %> @@ -13,17 +23,10 @@ <% if Spree::Reviews::Config[:show_email] && review.user %> <% else %> - + <% end %> <% else %> <% end %> -
- <%= simple_format(review.review) %> -
- <% if Spree::Reviews::Config[:feedback_rating] && (!Spree::Reviews::Config[:require_login] || spree_current_user) %> - - <% end %>
+ diff --git a/app/views/spree/shared/_reviews.html.erb b/app/views/spree/shared/_reviews.html.erb index 7b1a22b3..11885597 100644 --- a/app/views/spree/shared/_reviews.html.erb +++ b/app/views/spree/shared/_reviews.html.erb @@ -1,17 +1,24 @@
-

<%= Spree.t(:reviews) %>

- <% if Spree::Reviews::Config[:include_unapproved_reviews] == false and @product.reviews.approved.count == 0 %> -

<%= Spree.t(:no_reviews_available) %>

+

<%= Spree.t(:reviews) %>

+ <% if @product.reviews.count == 0 %> +

<%= Spree.t(:no_reviews_available) %>

+
+

"> <%= Spree.t(:write_your_own_review) %>

+
+ <% unless spree_current_user.present? %> +

You need to login to Post a review

+ <% end %> + <% else %> <%= render 'spree/shared/rating', product: @product, review: 0 %> +
+

"> <%= Spree.t(:write_your_own_review) %>

+
+ <% unless spree_current_user.present? %> +

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 %> -
+
\ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 1b6869ca..d1684ff3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -22,7 +22,7 @@ en: average_customer_rating: Average rating back_reviews: Back to the reviews based_upon_review_count: - one: based upon one review + one: based upon 1 review other: "based upon %{count} reviews" by: by editing_review_for_html: 'Editing review for %{link}' @@ -34,7 +34,7 @@ en: from: from info_approve_review: Review approved leave_us_a_review_for: "Please leave us a review and a rating for our '%{name}'" - no_reviews_available: "No reviews have been written for this product." + no_reviews_available: "Be the first to review this product" out_of_5: "out of 5" rating: rating reviews: Reviews @@ -64,6 +64,6 @@ en: one: "1 voice" other: "%{count} voices" was_this_review_helpful: "Was this review helpful to you?" - write_your_own_review: Write your own review + write_your_own_review: Post a review you_must_enter_value_for_rating: "You must enter a value for rating." anonymous: Anonymous