Update distributor details when distributor is changed

This commit is contained in:
Rohan Mitchell
2012-10-27 18:50:09 +11:00
parent 773adca838
commit c5941d6cb5
6 changed files with 48 additions and 9 deletions

View File

@@ -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);
});
});

View File

@@ -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 })

View File

@@ -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")

View File

@@ -0,0 +1 @@
distributors = <%= @distributor_details.to_json.html_safe %>;

View File

@@ -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.

View File

@@ -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