spree_favorite_products 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -13,7 +13,7 @@ Installation
13
13
  Add spree_favorite_products to your Gemfile:
14
14
 
15
15
  ```ruby
16
- gem 'spree_favorite_products'
16
+ gem 'spree_favorite_products', '2.0.2'
17
17
  ```
18
18
 
19
19
  If you're still using Spree 2.0.x, you should do this instead:
@@ -3,13 +3,20 @@ module Spree
3
3
  class FavoriteProductsController < Spree::Admin::BaseController
4
4
 
5
5
  def index
6
- @favorite_products = Spree::Product.favorite.page(params[:page])
6
+ @search = Spree::Product.favorite.search(params[:q])
7
+ @favorite_products = @search.result.order_by_favorite_users_count(sort_in_ascending_users_count?).page(params[:page])
7
8
  end
8
9
 
9
10
  def users
10
11
  @product = Spree::Product.where(:id => params[:id]).first
11
12
  @users = @product.favorite_users.page(params[:page])
12
13
  end
14
+
15
+
16
+ private
17
+ def sort_in_ascending_users_count?
18
+ params[:q] && params[:q][:s] == 'favorite_users_count asc'
19
+ end
13
20
  end
14
21
  end
15
22
  end
@@ -3,6 +3,10 @@ Spree::Product.class_eval do
3
3
  has_many :favorite_users, :through => :favorites, :class_name => 'Spree::User', :source => 'user'
4
4
 
5
5
  def self.favorite
6
- joins(:favorites).uniq
6
+ joins(:favorites).group('spree_favorites.product_id')
7
+ end
8
+
9
+ def self.order_by_favorite_users_count(asc=false)
10
+ order("count(spree_favorites.user_id) #{asc ? 'asc' : 'desc'}")
7
11
  end
8
12
  end
@@ -0,0 +1,11 @@
1
+ Deface::Override.new(
2
+ :virtual_path => 'spree/admin/shared/_tabs',
3
+ :name => 'select_products_tab',
4
+ :replace => "erb[silent]:contains('if can? :admin, Spree::Product')",
5
+ :closing_selector => "erb[silent]:contains('end')",
6
+ :text => %Q{
7
+ <% if can? :admin, Spree::Product %>
8
+ <%= tab :products, :option_types, :properties, :prototypes, :variants, :product_properties, :taxonomies, :favorite_products, :icon => 'icon-th-large' %>
9
+ <% end %>
10
+ }
11
+ )
@@ -19,9 +19,9 @@
19
19
  <thead>
20
20
  <tr data-hook="admin_products_index_headers">
21
21
  <th><%= Spree.t(:sku) %></th>
22
- <th colspan="2"><%= Spree.t(:name) %></th>
23
- <th><%= Spree.t(:master_price) %></th>
24
- <th>Favorite Users</th>
22
+ <th colspan="2"><%= sort_link @search, :name, Spree.t(:name) %></th>
23
+ <th><%= sort_link @search, :master_default_price_amount, Spree.t(:master_price) %></th>
24
+ <th><%= sort_link @search, 'favorite_users_count', 'Favorite Users' %></th>
25
25
  </tr>
26
26
  </thead>
27
27
  <tbody>
@@ -29,7 +29,7 @@
29
29
  <tr <%= "style='color: red;'" if product.deleted? %> id="<%= spree_dom_id product %>" data-hook="admin_products_index_rows" class="<%= cycle('odd', 'even') %>">
30
30
  <td class="align-center"><%= product.sku rescue '' %></td>
31
31
  <td class="align-center"><%= mini_image(product) %></td>
32
- <td><%= product.try(:name) %></td>
32
+ <td><%= link_to product.try(:name), edit_admin_product_path(product) %></td>
33
33
  <td class="align-center"><%= product.display_price.to_html rescue '' %></td>
34
34
  <td class='align-center'><%= link_to product.favorite_users.count, users_admin_favorite_product_path(:id => product.id) %></td>
35
35
  </tr>
@@ -15,17 +15,18 @@ describe Spree::Admin::FavoriteProductsController do
15
15
  roles.stub(:includes).with(:permissions).and_return(roles)
16
16
  controller.stub(:authorize_admin).and_return(true)
17
17
  controller.stub(:authorize!).and_return(true)
18
+
19
+ @favorite_products = double('favorite_products')
20
+ @favorite_products.stub(:order_by_favorite_users_count).and_return(@favorite_products)
21
+ @search = double('search', :result => @favorite_products)
22
+ @favorite_products.stub(:search).and_return(@search)
23
+ @favorite_products.stub(:page).and_return(@favorite_products)
24
+ Spree::Product.stub(:favorite).and_return(@favorite_products)
18
25
  end
19
26
 
20
27
  describe "GET index" do
21
28
  def send_request
22
- get :index, :page => 1 ,:use_route => 'spree'
23
- end
24
-
25
- before(:each) do
26
- @favorite_products = double('favorite_products')
27
- @favorite_products.stub(:page).and_return(@favorite_products)
28
- Spree::Product.stub(:favorite).and_return(@favorite_products)
29
+ get :index, :page => 1 ,:use_route => 'spree', :q => { 's' => 'name desc' }
29
30
  end
30
31
 
31
32
  it "returns favorite products" do
@@ -33,6 +34,34 @@ describe Spree::Admin::FavoriteProductsController do
33
34
  send_request
34
35
  end
35
36
 
37
+ it "searches favorite products" do
38
+ @favorite_products.should_receive(:search).with('s' => 'name desc')
39
+ send_request
40
+ end
41
+
42
+ it "assigns @search" do
43
+ send_request
44
+ assigns(:search).should eq(@search)
45
+ end
46
+
47
+ context 'when order favorite products by users count in asc order' do
48
+ def send_request
49
+ get :index, :page => 1 ,:use_route => 'spree', :q => { :s => 'favorite_users_count asc' }
50
+ end
51
+
52
+ it "orders favorite products by users count in asc order" do
53
+ @favorite_products.should_receive(:order_by_favorite_users_count).with(true)
54
+ send_request
55
+ end
56
+ end
57
+
58
+ context 'when order favorite products by users count in desc order' do
59
+ it "orders favorite products by users count in asc order" do
60
+ @favorite_products.should_receive(:order_by_favorite_users_count).with(false)
61
+ send_request
62
+ end
63
+ end
64
+
36
65
  it "paginates favorite products" do
37
66
  @favorite_products.should_receive(:page).with("1")
38
67
  send_request
@@ -69,4 +98,21 @@ describe Spree::Admin::FavoriteProductsController do
69
98
  send_request
70
99
  end
71
100
  end
101
+
102
+ describe "#sort_in_ascending_users_count?" do
103
+
104
+ context 'when favorite_user_count asc present in params[q][s]' do
105
+ it "is true" do
106
+ get :index, :page => 1 ,:use_route => 'spree', :q => { 's' => 'favorite_users_count asc' }
107
+ controller.send(:sort_in_ascending_users_count?).should be_true
108
+ end
109
+ end
110
+
111
+ context 'when favorite_user_count not present in params' do
112
+ it "is false" do
113
+ get :index, :page => 1 ,:use_route => 'spree', :q => { 's' => 'name asc' }
114
+ controller.send(:sort_in_ascending_users_count?).should be_false
115
+ end
116
+ end
117
+ end
72
118
  end
@@ -1,25 +1,39 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Spree::Product do
4
+ before(:each) do
5
+ shipping_category = Spree::ShippingCategory.create! :name => 'shipping_category'
6
+ @favorite_product1 = Spree::Product.create! :name => 'favorite_product1', :price => 100, :shipping_category_id => shipping_category.id
7
+ @favorite_product2 = Spree::Product.create! :name => 'favorite_product2', :price => 100, :shipping_category_id => shipping_category.id
8
+ @product1 = Spree::Product.create! :name => 'product1', :price => 100, :shipping_category_id => shipping_category.id
9
+ @product2 = Spree::Product.create! :name => 'product2', :price => 100, :shipping_category_id => shipping_category.id
10
+ @user1 = Spree::User.create! :email => 'user1@example.com', :password => 'example', :password_confirmation => "example"
11
+ @user2 = Spree::User.create! :email => 'user2@example.com', :password => "example", :password_confirmation => 'example'
12
+ @user1.favorites.create! :product_id => @favorite_product1.id
13
+ @user2.favorites.create! :product_id => @favorite_product1.id
14
+ @user2.favorites.create! :product_id => @favorite_product2.id
15
+ end
16
+
4
17
  it { should have_many(:favorites) }
5
18
  it { should have_many(:favorite_users).through(:favorites).class_name('Spree::User') }
6
19
 
7
20
  describe "Spree::Product.favorite" do
8
- before(:each) do
9
- shipping_category = Spree::ShippingCategory.create! :name => 'shipping_category'
10
- @favorite_product1 = Spree::Product.create! :name => 'favorite_product1', :price => 100, :shipping_category_id => shipping_category.id
11
- @favorite_product2 = Spree::Product.create! :name => 'favorite_product2', :price => 100, :shipping_category_id => shipping_category.id
12
- @product1 = Spree::Product.create! :name => 'product1', :price => 100, :shipping_category_id => shipping_category.id
13
- @product2 = Spree::Product.create! :name => 'product2', :price => 100, :shipping_category_id => shipping_category.id
14
- @user1 = Spree::User.create! :email => 'user1@example.com', :password => 'example', :password_confirmation => "example"
15
- @user2 = Spree::User.create! :email => 'user2@example.com', :password => "example", :password_confirmation => 'example'
16
- @user1.favorites.create! :product_id => @favorite_product1.id
17
- @user2.favorites.create! :product_id => @favorite_product1.id
18
- @user2.favorites.create! :product_id => @favorite_product2.id
19
- end
20
-
21
21
  it "returns favorite products" do
22
22
  Spree::Product.favorite.should =~ [@favorite_product1, @favorite_product2]
23
23
  end
24
24
  end
25
+
26
+ describe ".order_by_favorite_users_count" do
27
+ context 'when order not passed' do
28
+ it "returns products ordered by users_count in descending order" do
29
+ Spree::Product.favorite.order_by_favorite_users_count.should eq([@favorite_product1, @favorite_product2])
30
+ end
31
+ end
32
+
33
+ context 'when asc order passed' do
34
+ it "returns products ordered by users_count in ascending order" do
35
+ Spree::Product.favorite.order_by_favorite_users_count(true).should eq([@favorite_product2, @favorite_product1])
36
+ end
37
+ end
38
+ end
25
39
  end
@@ -2,7 +2,7 @@
2
2
  Gem::Specification.new do |s|
3
3
  s.platform = Gem::Platform::RUBY
4
4
  s.name = 'spree_favorite_products'
5
- s.version = '2.0.1'
5
+ s.version = '2.0.2'
6
6
  s.summary = 'users can mark product as favorite'
7
7
  s.description = 'This extension adds the following features: 1. Adds a link Mark as favorite on product detail page. 2. Favorite Products tab on header 3. Favorite Products tab in admin section'
8
8
  s.required_ruby_version = '>= 1.9.3'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spree_favorite_products
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
8
  - 0
9
- - 1
10
- version: 2.0.1
9
+ - 2
10
+ version: 2.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mohit Bansal
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2014-03-07 00:00:00 Z
18
+ date: 2014-04-30 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: spree_core
@@ -63,6 +63,7 @@ files:
63
63
  - app/overrides/.DS_Store
64
64
  - app/overrides/add_favorite_products_per_page_configuration.rb
65
65
  - app/overrides/add_favorite_products_tab.rb
66
+ - app/overrides/add_favorite_products_to_products_tab.rb
66
67
  - app/overrides/add_link_to_mark_product_as_favorite.rb
67
68
  - app/overrides/add_link_to_users_favorite_products.rb
68
69
  - app/views/spree/.DS_Store