diff --git a/app/controllers/products_controller_decorator.rb b/app/controllers/products_controller_decorator.rb new file mode 100644 index 0000000..781b270 --- /dev/null +++ b/app/controllers/products_controller_decorator.rb @@ -0,0 +1,22 @@ +ProductsController.class_eval do + HTTP_REFERER_REGEXP = /^https?:\/\/[^\/]+\/t\/([a-z0-9\-\/]+)$/ + private + def show + @product = Product.find_by_permalink!(params[:id]) + return unless @product + + @variants = Variant.active.includes([:option_values, :images]).where(:product_id => @product.id) + @variant_opt_val = Hash[@variants.map{ |v| [v.option_values.map{ |ov| ov.option_type_id.to_s+"_"+ov.id.to_s }.sort.join("-"), v.id ] }] + @product_properties = ProductProperty.includes(:property).where(:product_id => @product.id) + @selected_variant = @variants.detect { |v| v.available? } + + referer = request.env['HTTP_REFERER'] + + if referer && referer.match(HTTP_REFERER_REGEXP) + @taxon = Taxon.find_by_permalink($1) + end + + respond_with(@product) + end + +end \ No newline at end of file diff --git a/app/views/products/_dropdown_variants.erb b/app/views/products/_dropdown_variants.erb index 9b64854..486e8eb 100644 --- a/app/views/products/_dropdown_variants.erb +++ b/app/views/products/_dropdown_variants.erb @@ -1,4 +1,7 @@ <% end%> <% if @product.has_stock? || Spree::Config[:allow_backorders] %> diff --git a/public/javascripts/dropdown_variants.js b/public/javascripts/dropdown_variants.js index 63ca9f8..86b4060 100644 --- a/public/javascripts/dropdown_variants.js +++ b/public/javascripts/dropdown_variants.js @@ -1,5 +1,13 @@ jQuery(document).ready(function() { - $('#product-variants select').live('change', function (event) { - show_variant_images(this.value); - }); + $('ul#product-thumbnails').data("opt_val", <%= @variant_opt_val.to_json.html_safe %> ); + $('#product-variants').delegate('select', 'change', function (event) { + var opt_val = eval($('ul.thumbnails').data("opt_val")); + var arr = []; + $("#product-variants select option:selected").each(function () { + var ot_ov_str = $(this).parent().attr('id').match(/(option_types_)(\d+)/)[2]+"_"+$(this).attr('value'); + arr.push(ot_ov_str); + }); + arr.sort(); + show_variant_images(opt_val[arr.join("-")]); + }); }); \ No newline at end of file