Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started work on new version of extension
- Loading branch information
Showing
11 changed files
with
122 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters