diff --git a/app/controllers/spree/admin/favorite_products_controller.rb b/app/controllers/spree/admin/favorite_products_controller.rb index 82304c9..77becca 100644 --- a/app/controllers/spree/admin/favorite_products_controller.rb +++ b/app/controllers/spree/admin/favorite_products_controller.rb @@ -4,7 +4,7 @@ class FavoriteProductsController < Spree::Admin::BaseController def index @search = Spree::Product.favorite.search(params[:q]) - @favorite_products = @search.result.order_by_favorite_users_count(sort_in_ascending_users_count?).page(params[:page]) + @favorite_products = @search.result.order_by_favorite_users_count(sort_in_ascending_users_count?).group_by_id.page(params[:page]) end def users diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index ab0144d..bf11fc6 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -4,5 +4,6 @@ scope :favorite, -> { joins(:favorites).uniq } scope :order_by_favorite_users_count, ->(asc = false) { order("count(spree_favorites.user_id) #{asc ? 'asc' : 'desc'}") } + scope :group_by_id, -> { group(:id) } end diff --git a/app/overrides/add_link_to_mark_product_as_favorite.rb b/app/overrides/add_link_to_mark_product_as_favorite.rb index e2d9d26..883684c 100644 --- a/app/overrides/add_link_to_mark_product_as_favorite.rb +++ b/app/overrides/add_link_to_mark_product_as_favorite.rb @@ -4,9 +4,9 @@ insert_after: "div[itemprop='description']", text: %Q{ <% if spree_user_signed_in? && spree_current_user.has_favorite_product?(@product.id) %> - <%= link_to Spree.t(:unmark_as_favorite), favorite_product_path(:id => @product.id), :method => :delete, :remote => true, :class => 'favorite_link' %> + <%= link_to Spree.t(:unmark_as_favorite), favorite_product_path(:id => @product.id), :method => :delete, :remote => true, :class => 'favorite_link btn btn-danger' %> <% else %> - <%= link_to Spree.t(:mark_as_favorite), favorite_products_path(:id => @product.id), :method => :post, :remote => spree_user_signed_in?, :class => 'favorite_link' %> + <%= link_to Spree.t(:mark_as_favorite), favorite_products_path(:id => @product.id), :method => :post, :remote => spree_user_signed_in?, :class => 'favorite_link btn btn-primary' %> <% end %> } -) \ No newline at end of file +) diff --git a/app/views/spree/favorite_products/create.js.erb b/app/views/spree/favorite_products/create.js.erb index ac9ab6e..1ea3296 100644 --- a/app/views/spree/favorite_products/create.js.erb +++ b/app/views/spree/favorite_products/create.js.erb @@ -1,4 +1,4 @@ <% if @success %> - $('.favorite_link').attr('href', '<%= favorite_product_path(:id => params[:id]) %>').data('method', 'delete').text('<%= Spree.t(:unmark_as_favorite) %>') + $('.favorite_link').attr('href', '<%= favorite_product_path(:id => params[:id]) %>').data('method', 'delete').removeClass('btn-primary').addClass('btn-danger').text('<%= Spree.t(:unmark_as_favorite) %>') <% end %> alert("<%=j @message%>"); diff --git a/app/views/spree/favorite_products/destroy.js.erb b/app/views/spree/favorite_products/destroy.js.erb index 1254f04..c0f5b00 100644 --- a/app/views/spree/favorite_products/destroy.js.erb +++ b/app/views/spree/favorite_products/destroy.js.erb @@ -1,6 +1,6 @@ <% if @success %> if($('.favorite_link').length) { - $('.favorite_link').attr('href', '<%= favorite_products_path(:id => @favorite.product_id) %>').data('method', 'post').text('<%= Spree.t(:mark_as_favorite) %>') + $('.favorite_link').attr('href', '<%= favorite_products_path(:id => @favorite.product_id) %>').data('method', 'post').addClass('btn-primary').removeClass('btn-danger').text('<%= Spree.t(:mark_as_favorite) %>') alert('Successfully unmarked as favorite'); } else { $("#favorite_product_<%= @favorite.product_id%>").remove(); diff --git a/spec/controllers/spree/admin/favorite_products_controller_spec.rb b/spec/controllers/spree/admin/favorite_products_controller_spec.rb index 9fdca49..d8957b1 100644 --- a/spec/controllers/spree/admin/favorite_products_controller_spec.rb +++ b/spec/controllers/spree/admin/favorite_products_controller_spec.rb @@ -21,6 +21,7 @@ @favorite_products = double('favorite_products') allow(@favorite_products).to receive(:order_by_favorite_users_count).and_return(@favorite_products) + allow(@favorite_products).to receive(:group_by_id).and_return(@favorite_products) @search = double('search', result: @favorite_products) allow(@favorite_products).to receive(:search).and_return(@search) allow(@favorite_products).to receive(:page).and_return(@favorite_products)