Skip to content

Commit

Permalink
Started work on new version of extension
Browse files Browse the repository at this point in the history
  • Loading branch information
romul committed Feb 22, 2011
1 parent a9a4b33 commit 3abdbde
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 40 deletions.
88 changes: 54 additions & 34 deletions app/controllers/compare_products_controller.rb
@@ -1,60 +1,80 @@
class CompareProductsController < Spree::BaseController

before_filter :find_taxon
before_filter :verify_comparable_taxon
before_filter :find_products
before_filter :find_taxon, :only => [:show, :add]
#before_filter :verify_comparable_taxon, :only => :show

helper :products, :taxons

def show
@properties = @products.map(&:properties).flatten.uniq # We return the list of properties here so we can use them latter.
end

private
def add
session[:comparable_product_ids] ||= []
product = Product.find_by_permalink(params[:id])
if product && (product.taxons.include?(@taxon) || @products.size < 1)
session[:comparable_product_ids] << product.id
session[:comparable_product_ids].uniq!
flash[:notice] = I18n.t(:added_to_comparison, :scope => :compare_products)
else
session[:comparable_product_ids].delete(product.id)
flash[:error] = I18n.t(:invalid_taxon, :scope => :compare_products)
end
respond_to do |format|
format.html { redirect_back_or_default(products_path) }
format.js { render :layout => false }
end
end

# Find the taxon from the url.
def find_taxon
permalink = ''

if params[:taxon_path]
permalink = "#{params[:taxon_path]}/"
elsif params[:taxon]
permalink = "#{params[:taxon]}/"
def remove
session[:comparable_product_ids] ||= []
product = Product.find_by_permalink(params[:id])
if product
session[:comparable_product_ids].delete(product.id)
flash[:notice] = I18n.t(:removed_from_comparison, :product => product.name, :scope => :compare_products)
end

@taxon = Taxon.find_by_permalink(permalink) unless permalink.blank?

if @taxon.nil?
flash[:error] = I18n.t('compare_products.invalid_taxon')

redirect_to products_path
respond_to do |format|
format.html { redirect_back_or_default(products_path) }
format.js { render :layout => false }
end
end

# Verifies that the comparison can be made inside this taxon.
def verify_comparable_taxon
unless @taxon.is_comparable?
flash[:error] = I18n.t('compare_products.taxon_not_comparable')

redirect_to "/t/#{@taxon.permalink}"
def destroy
session[:comparable_product_ids] = []
flash[:notice] = I18n.t(:comparison_cleared, :scope => :compare_products)
respond_to do |format|
format.html { redirect_back_or_default(products_path) }
format.js { render :layout => false }
end
end

# Find the products inside the taxon, manually adding product ids to
# the url will silently be ignored if they can't be compared inside
# the taxon or don't exists.
private

def find_taxon
find_products
if @products.size > 1
@taxon = @products.inject(@products[0].taxons){|t, p| t & p.taxons }.select{|t| t.is_comparable? }.first

if @taxon.nil?
flash[:error] = I18n.t('compare_products.invalid_taxon')
end
elsif @products.size == 1
@taxon = @products[0].taxons.select{|t| t.is_comparable? }.first
end
end


def find_products
product_ids = params[:product_ids].split('/')
product_ids = session[:comparable_product_ids]

if product_ids.length > 4
if product_ids.length >= 4
flash[:notice] = I18n.t('compare_products.limit_is_4')
product_ids = product_ids[0..3]
product_ids = product_ids[0...3]
elsif product_ids.length < 2
flash[:error] = I18n.t('compare_products.insufficient_data')
redirect_to "/t/#{@taxon.permalink}"
end

@products = @taxon.products.find(:all, :conditions => { :id => product_ids}, :include => { :product_properties => :property }, :limit => 4)
@products = Product.find(:all, :conditions => { :id => product_ids}, :include => { :product_properties => :property }, :limit => 4)
end

end
end
24 changes: 24 additions & 0 deletions app/views/compare_products/add.js.erb
@@ -0,0 +1,24 @@
$("#comparable_products_wrapper").html("<%= escape_javascript(render(:partial => "shared/comparable_products")) %>");

notice_div = $("div.notice");
error_div = $("div.errors");
<% notice = flash.delete(:notice)
if notice %>
error_div.hide();
if (notice_div.length > 0) {
notice_div.html("<%= notice %>");
notice_div.show();
} else {
$("#content .breadcrumbs").after('<div class="flash notice"><%= notice %></div>');
} <%
end
error = flash.delete(:error)
if error %>
notice_div.hide();
if (error_div.length > 0) {
error_div.html("<%= notice %>");
error_div.show();
} else {
$("#content .breadcrumbs").after('<div class="flash errors"><%= error %></div>');
}
<% end %>
7 changes: 7 additions & 0 deletions app/views/compare_products/remove.js.erb
@@ -0,0 +1,7 @@
notice_div = $("div.notice");
<% notice = flash.delete(:notice) %>
if (notice_div.length > 0) {
notice_div.html("<%= notice %>");
} else {
$("#content .breadcrumbs").after('<div class="flash notice"><%= notice %></div>');
}
9 changes: 9 additions & 0 deletions app/views/shared/_comparable_products.html.erb
@@ -0,0 +1,9 @@
<% if session[:comparable_product_ids] %>
<h5><%= t(:сomparable_products, :scope => :compare_products) %></h5>
<ul id="comparable_products">
<% Product.find(session[:comparable_product_ids]).each do |p| %>
<li><%= p.name %> <%= link_to image_tag('remove.png'), remove_from_comparison_path(p), :remote => true %></li>
<% end %>
</ul>
<%= link_to t(:compare, :scope => :compare_products), compare_products_path %>
<% end %>
4 changes: 1 addition & 3 deletions app/views/shared/_compare_product_field.html.erb
@@ -1,5 +1,3 @@
<% if @comparable %>
<span class="comparable">
<%= check_box_tag('product_id[]', product.id, false, :id => "compare_product_#{product.id}", :class => 'compare') %>
</span>
<%= link_to t(:add_to_comparison, :scope => :compare_products), add_to_comparison_path(product), :class => 'compare', :style => "z-index:10;", :remote => true %>
<% end %>
1 change: 1 addition & 0 deletions config/locales/en-US.yml
Expand Up @@ -8,3 +8,4 @@ en:
limit_is_4: You can only compare up to 4 products.
product_comparison: Product comparison
taxon_not_comparable: "Products inside this taxon can't be compared"
add_to_comparison: "Compare"
5 changes: 5 additions & 0 deletions config/locales/ru.yml
Expand Up @@ -8,3 +8,8 @@ ru:
limit_is_4: "Вы можете сравнить не более четырёх товаров одновременно."
product_comparison: "Сравнение товаров"
taxon_not_comparable: "Товары данной категории не могут быть сравнены."
add_to_comparison: "Cравнить"
added_to_comparison: "Добавлено к сравнению"
removed_from_comparison: "%{product} убран из сранения"
comparison_cleared: "Сравнение очищено"
сomparable_products: "Сравниваемые товары"
7 changes: 5 additions & 2 deletions config/routes.rb
@@ -1,4 +1,7 @@
Rails.application.routes.draw do
match '/t/compare_products' => 'compare_products#show', :as => 'compare_products'
match '/compare_products' => 'compare_products#show', :as => 'compare_products'
match '/compare_products/add/:id' => 'compare_products#add', :as => 'add_to_comparison'
match '/compare_products/remove/:id' => 'compare_products#remove', :as => 'remove_from_comparison'
match '/compare_products/destroy' => 'compare_products#destroy', :as => 'destroy_comparison'
match '/t/*taxon_path/compare/*product_ids' => 'compare_products#show'
end
end
12 changes: 12 additions & 0 deletions lib/spree_compare_products_hooks.rb
Expand Up @@ -6,4 +6,16 @@ class SpreeCompareProductsHooks < Spree::ThemeSupport::HookListener

insert_after :products_list_item, 'shared/compare_product_field'

insert_after :sidebar do
%(
<div class="sidebar-item" id="comparable_products_wrapper">
<%= render 'shared/comparable_products' %>
</div>
)
end

insert_after :inside_head do
"<%= javascript_include_tag 'product_comparison' %>"
end

end
Binary file added public/images/remove.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion public/javascripts/product_comparison.js
Expand Up @@ -101,4 +101,7 @@ var ProductComparison = function(params) {

$(document).ready(function() {
var productComparison = new ProductComparison();
});
$('#comparable_products li a').live('ajax:before', function() {
$(this).parent('li').fadeOut();
});
});

0 comments on commit 3abdbde

Please sign in to comment.