Skip to content

Commit

Permalink
Fixes #12 and adds style to Mark/Unmark as favorite buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Pikender Sharma committed Feb 20, 2017
1 parent a501279 commit 6c19bd8
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/models/spree/product_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions app/overrides/add_link_to_mark_product_as_favorite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
}
)
)
2 changes: 1 addition & 1 deletion app/views/spree/favorite_products/create.js.erb
Original file line number Diff line number Diff line change
@@ -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%>");
2 changes: 1 addition & 1 deletion app/views/spree/favorite_products/destroy.js.erb
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6c19bd8

Please sign in to comment.