spree_compare_products 0.40.90 → 0.50.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,60 +1,75 @@
1
1
  class CompareProductsController < Spree::BaseController
2
-
3
- before_filter :find_taxon
4
- before_filter :verify_comparable_taxon
5
- before_filter :find_products
6
-
2
+ COMPARE_LIMIT = 4
3
+ before_filter :find_taxon, :only => [:show, :add]
4
+
7
5
  helper :products, :taxons
8
-
6
+
9
7
  def show
10
- @properties = @products.map(&:properties).flatten.uniq # We return the list of properties here so we can use them latter.
8
+ if @comparable_products.count > 1
9
+ @properties = @comparable_products.includes(:product_properties => :property).map(&:properties).flatten.uniq # We return the list of properties here so we can use them latter.
10
+ else
11
+ flash[:error] = I18n.t(:insufficient_data, :scope => :compare_products)
12
+ end
11
13
  end
12
-
13
- private
14
-
15
- # Find the taxon from the url.
16
- def find_taxon
17
- permalink = ''
18
-
19
- if params[:taxon_path]
20
- permalink = "#{params[:taxon_path]}/"
21
- elsif params[:taxon]
22
- permalink = "#{params[:taxon]}/"
14
+
15
+ def add
16
+ product = Product.find_by_permalink(params[:id])
17
+ if product && (product.taxons.map(&:id).include?(@taxon.try(:id)) || @comparable_products.size < 1)
18
+ if session[:comparable_product_ids].include?(product.id)
19
+ flash[:notice] = I18n.t(:already_in_list, :product => product.name, :scope => :compare_products)
20
+ else
21
+ if @comparable_products.size < COMPARE_LIMIT
22
+ @added_product = product
23
+ session[:comparable_product_ids] << product.id
24
+ flash[:notice] = I18n.t(:added_to_comparsion, :product => product.name, :scope => :compare_products)
25
+ else
26
+ flash[:notice] = I18n.t(:limit_is_reached, :scope => :compare_products, :count => COMPARE_LIMIT)
27
+ end
28
+ end
29
+ else
30
+ session[:comparable_product_ids].delete(product.id)
31
+ flash[:error] = I18n.t(:invalid_taxon, :scope => :compare_products)
32
+ end
33
+ respond_to do |format|
34
+ format.html { redirect_back_or_default(products_path) }
35
+ format.js { render :layout => false }
23
36
  end
24
-
25
- @taxon = Taxon.find_by_permalink(permalink) unless permalink.blank?
26
-
27
- if @taxon.nil?
28
- flash[:error] = I18n.t('compare_products.invalid_taxon')
29
-
30
- redirect_to products_path
37
+ end
38
+
39
+ def remove
40
+ session[:comparable_product_ids] ||= []
41
+ product = Product.find_by_permalink(params[:id])
42
+ if product
43
+ @deleted_product = product if session[:comparable_product_ids].include?(product.id)
44
+ session[:comparable_product_ids].delete(product.id)
45
+ flash[:notice] = I18n.t(:removed_from_comparsion, :product => product.name, :scope => :compare_products)
46
+ end
47
+ respond_to do |format|
48
+ format.html { redirect_back_or_default(products_path) }
49
+ format.js { render :layout => false }
31
50
  end
32
51
  end
33
-
34
- # Verifies that the comparison can be made inside this taxon.
35
- def verify_comparable_taxon
36
- unless @taxon.is_comparable?
37
- flash[:error] = I18n.t('compare_products.taxon_not_comparable')
38
-
39
- redirect_to "/t/#{@taxon.permalink}"
52
+
53
+ def destroy
54
+ session[:comparable_product_ids] = []
55
+ flash[:notice] = I18n.t(:comparsion_cleared, :scope => :compare_products)
56
+ respond_to do |format|
57
+ format.html { redirect_back_or_default(products_path) }
58
+ format.js { render :layout => false }
40
59
  end
41
60
  end
42
-
43
- # Find the products inside the taxon, manually adding product ids to
44
- # the url will silently be ignored if they can't be compared inside
45
- # the taxon or don't exists.
46
- def find_products
47
- product_ids = params[:product_ids].split('/')
48
-
49
- if product_ids.length > 4
50
- flash[:notice] = I18n.t('compare_products.limit_is_4')
51
- product_ids = product_ids[0..3]
52
- elsif product_ids.length < 2
53
- flash[:error] = I18n.t('compare_products.insufficient_data')
54
- redirect_to "/t/#{@taxon.permalink}"
61
+
62
+ private
63
+
64
+ def find_taxon
65
+ if @comparable_products.size > 1
66
+ @taxon = @comparable_products.inject(@comparable_products[0].taxons) { |t, p| t & p.taxons }.select { |t| t.is_comparable? }.first
67
+
68
+ if @taxon.nil?
69
+ flash[:error] = I18n.t(:invalid_taxon, :scope => :compare_products)
70
+ end
71
+ elsif @comparable_products.size == 1
72
+ @taxon = @comparable_products[0].taxons.select { |t| t.is_comparable? }.first
55
73
  end
56
-
57
- @products = @taxon.products.find(:all, :conditions => { :id => product_ids}, :include => { :product_properties => :property }, :limit => 4)
58
74
  end
59
-
60
- end
75
+ end
@@ -11,7 +11,7 @@ module CompareProductsHelper
11
11
  def comparison_rows_for(products, properties)
12
12
  fields = [comparison_fields_for(products, properties)]
13
13
  products.each do |product|
14
- fields << (fields_for(product, properties))
14
+ fields << (product_fields_for(product, properties))
15
15
  end
16
16
  fields.transpose
17
17
  end
@@ -21,7 +21,8 @@ module CompareProductsHelper
21
21
  #
22
22
  # Example:
23
23
  # ["product1 image", "product1 name", ..., "product1 price"]
24
- def fields_for(product, properties)
24
+
25
+ def product_fields_for(product, properties)
25
26
  [ link_to(small_image(product), product), link_to(product.name, product)].tap { |fields|
26
27
  properties.each do |property|
27
28
  fields << product.product_properties.find_by_property_id(property.id).try(:value)
@@ -0,0 +1,5 @@
1
+ Product.class_eval do
2
+ def can_be_compared?
3
+ !self.taxons.to_a.find{|t| t.is_comparable? }.nil?
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ <% if @added_product %>
2
+ var line_item = "<%= escape_javascript(render(:partial => "shared/comparable_line_item", :object => @added_product)) %>";
3
+ var product_id = <%= @added_product.id %>
4
+ onAddCompareProduct(line_item, product_id);
5
+ <% end %>
6
+
7
+ <% notice = flash.delete(:notice)
8
+ if notice %>
9
+ var notice = "<%= notice %>";
10
+ onCompareProductNotice(notice);
11
+ <% end
12
+ error = flash.delete(:error)
13
+ if error %>
14
+ var error = "<%= error %>";
15
+ onCompareProductError(error);
16
+ <% end %>
@@ -0,0 +1,15 @@
1
+ <% if @deleted_product %>
2
+ var product_id = <%= @deleted_product.id %>
3
+ onRemoveCompareProduct(product_id);
4
+ <% end %>
5
+
6
+ <% notice = flash.delete(:notice)
7
+ if notice %>
8
+ var notice = "<%= notice %>";
9
+ onCompareProductNotice(notice);
10
+ <% end
11
+ error = flash.delete(:error)
12
+ if error %>
13
+ var error = "<%= error %>";
14
+ onCompareProductError(error);
15
+ <% end %>
@@ -1,17 +1,15 @@
1
- <% content_for :head do %>
2
- <%= stylesheet_link_tag 'compiled/compare_products' %>
3
- <% end %>
1
+ <% @body_class = 'one-col' %>
4
2
 
5
- <% content_for :sidebar do %>
6
- <%= render :partial => 'shared/taxonomies' %>
3
+ <% content_for :head do %>
4
+ <%= stylesheet_link_tag 'compare_products' %>
7
5
  <% end %>
8
6
 
9
7
  <h1>
10
8
  <%= t('compare_products.product_comparison') %>
11
9
  </h1>
12
-
13
- <table>
14
- <% comparison_rows_for(@products, @properties).each do |row| %>
10
+ <% if @comparable_products.count > 1 %>
11
+ <table class="compare_product_show">
12
+ <% comparison_rows_for(@comparable_products, @properties).each do |row| %>
15
13
  <tr>
16
14
  <th><%= row.shift -%></th>
17
15
 
@@ -21,5 +19,8 @@
21
19
  </tr>
22
20
  <% end %>
23
21
  </table>
22
+ <% else %>
23
+ <p><%= t('compare_products.insufficient_data') %></p>
24
+ <% end %>
24
25
 
25
- <%= link_to t('continue_shopping'), seo_url(@taxon), :class => 'continue button' %>
26
+ <%= link_to t('continue_shopping'), @taxon ? seo_url(@taxon) : products_path, :class => 'continue button' %>
@@ -0,0 +1,3 @@
1
+ <% if @product.can_be_compared? %>
2
+ <%= link_to t(:add_to_comparison, :scope => :compare_products), add_to_comparison_path(@product), :class => 'compare', :remote => true %>
3
+ <% end %>
@@ -0,0 +1 @@
1
+ <li id="comp_item_<%= comparable_line_item.id %>"><%= link_to comparable_line_item.name, comparable_line_item %> <%= link_to "", remove_from_comparison_path(comparable_line_item), :remote => true, :class => "remove-comparable-item" %></li>
@@ -0,0 +1,9 @@
1
+ <div id="comparable_products_wrapper" >
2
+ <h3><%= t('сomparable_products', :scope => :compare_products) %></h3>
3
+ <ul id="comparable_products">
4
+ <%= render :partial => 'shared/comparable_line_item', :collection => @comparable_products %>
5
+ </ul>
6
+ <div style="display: none" class="no_products_message"><%= t(:no_products_to_compare, :scope => :compare_products) %></div>
7
+ <div id="compare_products_link"><%= link_to t(:compare, :scope => :compare_products), compare_products_path %></div>
8
+ <div id="clear_comparable_products_link"><%= link_to t(:compare_clear, :scope => :compare_products), destroy_comparison_path %></div>
9
+ </div>
@@ -1,5 +1,3 @@
1
- <% if @comparable %>
2
- <span class="comparable">
3
- <%= check_box_tag('product_id[]', product.id, false, :id => "compare_product_#{product.id}", :class => 'compare') %>
4
- </span>
1
+ <% if product.can_be_compared? %>
2
+ <%= link_to t(:add_to_comparison, :scope => :compare_products), add_to_comparison_path(product), :class => 'compare', :style => "z-index:10;", :remote => true %>
5
3
  <% end %>
@@ -0,0 +1,19 @@
1
+ <%= javascript_include_tag 'product_comparison' %>
2
+
3
+ <script type="text/javascript">
4
+ function onAddCompareProduct() {
5
+ // override in your application
6
+ }
7
+
8
+ function onRemoveCompareProduct() {
9
+ // override in your application
10
+ }
11
+
12
+ function onCompareProductNotice() {
13
+ // override in your application
14
+ }
15
+
16
+ function onCompareProductError() {
17
+ // override in your application
18
+ }
19
+ </script>
@@ -1,12 +1,7 @@
1
1
  <% content_for :head do %>
2
- <%= javascript_include_tag 'product_comparison' %>
3
-
2
+ <%= javascript_include_tag 'product_comparison' %>
4
3
  <%= stylesheet_link_tag 'compiled/compare_products' %>
5
4
  <% end %>
6
5
 
7
- <% form_tag(compare_products_path, :method => :get) do %>
8
- <%= hidden_field_tag('taxon_permalink', params[:id]) %>
9
- <% @comparable = true %>
10
- <%= render 'shared/products.html.erb', :products => @products, :taxon => @taxon %>
11
- <%= submit_tag t('compare_products.compare'), :id => 'compare', :class => 'button' %>
12
- <% end %>
6
+ <%= render 'shared/products.html.erb', :products => @comparable_products, :taxon => @taxon %>
7
+
@@ -1,5 +1 @@
1
- <% if @taxon && @taxon.taxonomy.comparable? && @products.length > 1 %>
2
- <%= render('taxons/comparable_taxon') %>
3
- <% else %>
4
- <%= render('shared/products.html.erb', :products => @products, :taxon => @taxon) %>
5
- <% end %>
1
+ <%= render('shared/products', :products => @products, :taxon => @taxon) %>
@@ -8,3 +8,5 @@ en:
8
8
  limit_is_4: You can only compare up to 4 products.
9
9
  product_comparison: Product comparison
10
10
  taxon_not_comparable: "Products inside this taxon can't be compared"
11
+ add_to_comparison: "Compare"
12
+ no_products_to_compare: "There is no products to compare"
@@ -1,10 +1,20 @@
1
1
  ru:
2
2
  compare_products:
3
+ add_to_comparison: "Добавить к сравнению"
4
+ added_to_comparsion: "%{product} добавлен к сравнению"
5
+ already_in_list: "%{product} уже находится в списке сравниваемых товаров."
3
6
  comparable_container: "разрешить сравнение товаров"
7
+ сomparable_products: "Сравниваемые товары"
4
8
  compare: "Сравнить"
9
+ compare_clear: "Очистить список"
5
10
  compare_selected: "Сравнить выбранные товары"
11
+ compare_products: "Сравнение товаров (%{count})"
12
+ comparsion_cleared: "Сравнение очищено"
6
13
  insufficient_data: "Недостаточно данных, нужно выбрать хотя бы 2 товара."
7
14
  invalid_taxon: "Нельзя сравнивать товары из разных категорий"
8
15
  limit_is_4: "Вы можете сравнить не более четырёх товаров одновременно."
16
+ limit_is_reached: "Вы можете сравнить не более %{count} товаров одновременно."
9
17
  product_comparison: "Сравнение товаров"
18
+ removed_from_comparsion: "%{product} убран из сравнения"
10
19
  taxon_not_comparable: "Товары данной категории не могут быть сравнены."
20
+ no_products_to_compare: "Вы можете добавить товары к сравнению, воспользовавшись соответствующей ссылкой на странице просмотра товара."
@@ -1,4 +1,7 @@
1
1
  Rails.application.routes.draw do
2
- match '/t/compare_products' => 'compare_products#show', :as => 'compare_products'
2
+ match '/compare_products' => 'compare_products#show', :as => 'compare_products'
3
+ match '/compare_products/add/:id' => 'compare_products#add', :as => 'add_to_comparison'
4
+ match '/compare_products/remove/:id' => 'compare_products#remove', :as => 'remove_from_comparison'
5
+ match '/compare_products/destroy' => 'compare_products#destroy', :as => 'destroy_comparison'
3
6
  match '/t/*taxon_path/compare/*product_ids' => 'compare_products#show'
4
- end
7
+ end
@@ -4,27 +4,54 @@ require 'spree_compare_products/comparable_container'
4
4
 
5
5
  module SpreeCompareProducts
6
6
  class Engine < Rails::Engine
7
-
7
+
8
8
  config.autoload_paths += %W(#{config.root}/lib)
9
-
9
+
10
10
  def self.activate
11
11
  Dir.glob(File.join(File.dirname(__FILE__), '../app/**/*_decorator*.rb')) do |c|
12
12
  Rails.env.production? ? require(c) : load(c)
13
13
  end
14
-
14
+
15
15
  Taxonomy.class_eval do
16
16
  include SpreeCompareProducts::ComparableContainer
17
17
  end
18
-
18
+
19
19
  Taxon.class_eval do
20
20
  include SpreeCompareProducts::ComparableContainer
21
-
21
+
22
22
  def is_comparable?
23
23
  comparable? || taxonomy.comparable?
24
24
  end
25
25
  end
26
26
  end
27
-
27
+
28
28
  config.to_prepare &method(:activate).to_proc
29
29
  end
30
- end
30
+ end
31
+
32
+ module SpreeBase
33
+ module InstanceMethods
34
+ protected
35
+
36
+ def find_comparable_products
37
+ session[:comparable_product_ids] ||= []
38
+ product_ids = session[:comparable_product_ids]
39
+ if product_ids.size > 0
40
+ @comparable_products = Product.where(:id => product_ids)
41
+ else
42
+ @comparable_products = []
43
+ end
44
+ end
45
+ end
46
+
47
+ class << self
48
+
49
+ def included_with_compare_products(receiver)
50
+ included_without_compare_products(receiver)
51
+ receiver.send :before_filter, 'find_comparable_products'
52
+ end
53
+
54
+ alias_method_chain :included, :compare_products
55
+ end
56
+ end
57
+
@@ -3,7 +3,13 @@ class SpreeCompareProductsHooks < Spree::ThemeSupport::HookListener
3
3
  insert_after :admin_inside_taxonomy_form, 'admin/taxonomies/comparable_field'
4
4
 
5
5
  replace :taxon_products, 'taxons/taxon_products'
6
+
7
+ #insert_after :products_list_item, 'shared/compare_product_field'
6
8
 
7
- insert_after :products_list_item, 'shared/compare_product_field'
9
+ insert_after :shared_login_bar, 'shared/comparable_products'
10
+
11
+ insert_after :inside_head, 'shared/compare_product_javascript'
12
+
13
+ #insert_after :cart_form, 'products/compare'
8
14
 
9
15
  end
Binary file
@@ -97,8 +97,3 @@ var ProductComparison = function(params) {
97
97
 
98
98
  _init(); // Initialize product comparison
99
99
  }
100
-
101
-
102
- $(document).ready(function() {
103
- var productComparison = new ProductComparison();
104
- });
@@ -0,0 +1,11 @@
1
+ table.compare_product_show th {
2
+ background: #dddddd;
3
+ padding: 10px;
4
+ text-align: right;
5
+ border: 1px solid #dddddd;
6
+ }
7
+
8
+ table.compare_product_show td {
9
+ border: 1px solid #dddddd;
10
+ text-align: center;
11
+ }
@@ -1,14 +1,10 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path('../lib', __FILE__)
3
- require 'spree_compare_products/version'
4
-
5
2
  Gem::Specification.new do |s|
6
3
  s.name = 'spree_compare_products'
7
- s.version = SpreeCompareProducts::VERSION
8
- s.platform = Gem::Platform::RUBY
9
- s.authors = [ 'Jorge Calás', 'Maik Kempe' ]
4
+ s.version = "0.50.0"
5
+ s.authors = [ 'Jorge Calás', 'Maik Kempe', 'Roman Smirnov', 'Alexander Shuhin' ]
10
6
  s.email = [ 'calas@qvitta.net', 'dev@breaking-limits.com' ]
11
- s.homepage = 'https://github.com/mkempe/spree_compare_products'
7
+ s.homepage = 'https://github.com/romul/spree_compare_products'
12
8
  s.summary = %q{Provides product comparison with other products within the same taxon.}
13
9
  # s.description = %q{}
14
10
 
@@ -22,5 +18,5 @@ Gem::Specification.new do |s|
22
18
 
23
19
  s.required_ruby_version = '>= 1.8.7'
24
20
 
25
- s.add_dependency('spree_core', '>= 0.40.99')
21
+ s.add_dependency('spree_core', '>= 0.50.2')
26
22
  end
metadata CHANGED
@@ -4,18 +4,20 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 40
8
- - 90
9
- version: 0.40.90
7
+ - 50
8
+ - 0
9
+ version: 0.50.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Jorge Cal\xC3\xA1s"
13
13
  - Maik Kempe
14
+ - Roman Smirnov
15
+ - Alexander Shuhin
14
16
  autorequire:
15
17
  bindir: bin
16
18
  cert_chain: []
17
19
 
18
- date: 2011-01-20 00:00:00 +03:00
20
+ date: 2011-05-17 00:00:00 +04:00
19
21
  default_executable:
20
22
  dependencies:
21
23
  - !ruby/object:Gem::Dependency
@@ -27,9 +29,9 @@ dependencies:
27
29
  - !ruby/object:Gem::Version
28
30
  segments:
29
31
  - 0
30
- - 40
31
- - 99
32
- version: 0.40.99
32
+ - 50
33
+ - 2
34
+ version: 0.50.2
33
35
  type: :runtime
34
36
  version_requirements: *id001
35
37
  description:
@@ -50,10 +52,17 @@ files:
50
52
  - app/controllers/compare_products_controller.rb
51
53
  - app/helpers/compare_products_helper.rb
52
54
  - app/metal/bookmarkable_comparison.rb
55
+ - app/models/product_decorator.rb
53
56
  - app/views/admin/taxonomies/_comparable_field.html.erb
54
57
  - app/views/admin/taxonomies/_list.html.erb
58
+ - app/views/compare_products/add.js.erb
59
+ - app/views/compare_products/remove.js.erb
55
60
  - app/views/compare_products/show.html.erb
61
+ - app/views/products/_compare.html.erb
62
+ - app/views/shared/_comparable_line_item.html.erb
63
+ - app/views/shared/_comparable_products.html.erb
56
64
  - app/views/shared/_compare_product_field.html.erb
65
+ - app/views/shared/_compare_product_javascript.html.erb
57
66
  - app/views/taxons/_comparable_taxon.html.erb
58
67
  - app/views/taxons/_taxon_products.html.erb
59
68
  - config/locales/en-US.yml
@@ -63,17 +72,16 @@ files:
63
72
  - config/routes.rb
64
73
  - lib/spree_compare_products.rb
65
74
  - lib/spree_compare_products/comparable_container.rb
66
- - lib/spree_compare_products/version.rb
67
75
  - lib/spree_compare_products_hooks.rb
68
76
  - lib/tasks/install.rake
69
77
  - lib/tasks/spree_compare_products.rake
78
+ - public/images/remove.png
70
79
  - public/javascripts/product_comparison.js
71
- - public/stylesheets/compiled/compare_products.css
72
- - public/stylesheets/sass/compare_products.sass
80
+ - public/stylesheets/compare_products.css
73
81
  - spec/spec_helper.rb
74
82
  - spree_compare_products.gemspec
75
83
  has_rdoc: true
76
- homepage: https://github.com/mkempe/spree_compare_products
84
+ homepage: https://github.com/romul/spree_compare_products
77
85
  licenses: []
78
86
 
79
87
  post_install_message:
@@ -1,3 +0,0 @@
1
- module SpreeCompareProducts
2
- VERSION = '0.40.90'
3
- end
@@ -1,8 +0,0 @@
1
- table th {
2
- background: #dddddd;
3
- padding: 10px;
4
- text-align: right;
5
- border: 1px solid #dddddd; }
6
- table td {
7
- border: 1px solid #dddddd;
8
- text-align: center; }
@@ -1,9 +0,0 @@
1
- table
2
- th
3
- :background #DDD
4
- :padding 10px
5
- :text-align right
6
- :border 1px solid #DDD
7
- td
8
- :border 1px solid #DDD
9
- :text-align center