diff --git a/app/assets/javascripts/store/products.js b/app/assets/javascripts/store/products.js index 3e972161a6..0dabff5f4d 100644 --- a/app/assets/javascripts/store/products.js +++ b/app/assets/javascripts/store/products.js @@ -10,6 +10,15 @@ $(document).ready(function() { // Product page with master price only $(".add-to-cart input.title:not(#quantity):not(.max_quantity)").change(products_update_price_without_variant).change(); + + // Product page other + $("#distributor_id").change(function() { + var distributor_html = distributors[$(this).val()]; + if(!distributor_html) { + distributor_html = 'When you select a distributor for your order, their address and pickup times will be displayed here.'; + } + $("#product-distributor-details .distributor-details").html(distributor_html); + }); }); diff --git a/app/controllers/distributors_controller.rb b/app/controllers/distributors_controller.rb index 97c6d81691..2dc50f4af6 100644 --- a/app/controllers/distributors_controller.rb +++ b/app/controllers/distributors_controller.rb @@ -1,4 +1,14 @@ class DistributorsController < BaseController + def index + @distributors = Distributor.all + + respond_to do |format| + format.js do + @distributor_details = Hash[@distributors.map { |d| [d.id, render_to_string(:partial => 'distributors/details', :locals => {:distributor => d})] }] + end + end + end + def show options = {:distributor_id => params[:id]} options.merge(params.reject { |k,v| k == :id }) diff --git a/app/overrides/add_distributor_details_to_product.rb b/app/overrides/add_distributor_details_to_product.rb index 6671ed40b5..4d1b1d344f 100644 --- a/app/overrides/add_distributor_details_to_product.rb +++ b/app/overrides/add_distributor_details_to_product.rb @@ -2,3 +2,8 @@ Deface::Override.new(:virtual_path => "spree/products/show", :insert_before => "[data-hook='cart_form']", :partial => "spree/products/distributor_details", :name => "product_distributor_details") + +Deface::Override.new(:virtual_path => "spree/products/show", + :insert_after => "[data-hook='product_show']", + :text => "<%= javascript_include_tag main_app.distributors_path(:format => :js) %>", + :name => "product_distributor_details_js") diff --git a/app/views/distributors/index.js.erb b/app/views/distributors/index.js.erb new file mode 100644 index 0000000000..aed01164da --- /dev/null +++ b/app/views/distributors/index.js.erb @@ -0,0 +1 @@ +distributors = <%= @distributor_details.to_json.html_safe %>; \ No newline at end of file diff --git a/app/views/spree/products/_distributor_details.html.haml b/app/views/spree/products/_distributor_details.html.haml index 808468f806..e68b6c49ca 100644 --- a/app/views/spree/products/_distributor_details.html.haml +++ b/app/views/spree/products/_distributor_details.html.haml @@ -1,7 +1,8 @@ %fieldset#product-distributor-details.columns.five.omega %legend Distributor - - order = current_order(false) - - if order.andand.distributor.present? - = render 'distributors/details', :distributor => order.distributor - - else - When you select a distributor for your order, their address and pickup times will be displayed here. + .distributor-details + - order = current_order(false) + - if order.andand.distributor.present? + = render 'distributors/details', :distributor => order.distributor + - else + When you select a distributor for your order, their address and pickup times will be displayed here. diff --git a/spec/requests/consumer/product_spec.rb b/spec/requests/consumer/product_spec.rb index 76a647d074..40b65cace8 100644 --- a/spec/requests/consumer/product_spec.rb +++ b/spec/requests/consumer/product_spec.rb @@ -61,10 +61,23 @@ feature %q{ end end - context "with Javascript" do - it "changes distributor details when the distributor is changed" + context "with Javascript", js: true do + it "changes distributor details when the distributor is changed" do + d1 = create(:distributor) + d2 = create(:distributor) + d3 = create(:distributor) + p = create(:product, :distributors => [d1, d2, d3]) + + visit spree.product_path p + + [d1, d2, d3].each do |d| + select d.name, :from => 'distributor_id' + + within '#product-distributor-details' do + page.should have_selector 'h2', :text => d.name + end + end + end end end - - end