diff --git a/app/assets/stylesheets/store/openfoodweb.css.scss b/app/assets/stylesheets/store/openfoodweb.css.scss index a3cb0a29f1..d183ffd10b 100644 --- a/app/assets/stylesheets/store/openfoodweb.css.scss +++ b/app/assets/stylesheets/store/openfoodweb.css.scss @@ -128,8 +128,8 @@ ul.product-listing { } -/* Supplier and distributor description */ -.supplier-description, .distributor-description { +/* Enterprise description */ +.enterprise-description { margin-bottom: 2em; } diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 73238eaef2..1bfb4a7d42 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -11,8 +11,8 @@ class ApplicationController < ActionController::Base def load_data_for_sidebar - @suppliers = Supplier.all - @distributors = Distributor.with_active_products_on_hand.by_name + @suppliers = Enterprise.is_supplier.all + @distributors = Enterprise.is_distributor.with_distributed_active_products_on_hand.by_name end end diff --git a/app/controllers/enterprises_controller.rb b/app/controllers/enterprises_controller.rb new file mode 100644 index 0000000000..3528746df4 --- /dev/null +++ b/app/controllers/enterprises_controller.rb @@ -0,0 +1,43 @@ +class EnterprisesController < BaseController + def index + @enterprises = Enterprise.all + end + + def suppliers + @suppliers = Enterprise.is_supplier + end + + def show + options = {:enterprise_id => params[:id]} + options.merge(params.reject { |k,v| k == :id }) + + @enterprise = Enterprise.find params[:id] + + @searcher = Spree::Config.searcher_class.new(options) + @products = @searcher.retrieve_products + end + + def select_distributor + distributor = Enterprise.is_distributor.find params[:id] + + order = current_order(true) + + if order.can_change_distributor? + order.distributor = distributor + order.save! + end + + redirect_to distributor + end + + def deselect_distributor + order = current_order(true) + + if order.can_change_distributor? + order.distributor = nil + order.save! + end + + redirect_to root_path + end +end diff --git a/app/views/enterprises/index.html.haml b/app/views/enterprises/index.html.haml new file mode 100644 index 0000000000..a1e07dfc04 --- /dev/null +++ b/app/views/enterprises/index.html.haml @@ -0,0 +1,12 @@ +- content_for :sidebar do + %div{'data-hook' => "homepage_sidebar_navigation"} + = render 'spree/sidebar' + + +%h1 Enterprises + += cms_page_content(:content, Cms::Page.find_by_full_path('/enterprises')) + +%ul.enterprises + - @enterprises.each do |enterprise| + %li= link_to enterprise.name, enterprise diff --git a/app/views/enterprises/show.html.haml b/app/views/enterprises/show.html.haml new file mode 100644 index 0000000000..b5f8284b33 --- /dev/null +++ b/app/views/enterprises/show.html.haml @@ -0,0 +1,7 @@ +%h2= @enterprise.name + +.enterprise-description= @enterprise.long_description.andand.html_safe + +%h3 Available Now + += render :template => 'spree/products/index' diff --git a/app/views/enterprises/suppliers.html.haml b/app/views/enterprises/suppliers.html.haml new file mode 100644 index 0000000000..c67a7bc987 --- /dev/null +++ b/app/views/enterprises/suppliers.html.haml @@ -0,0 +1,12 @@ +- content_for :sidebar do + %div{'data-hook' => "homepage_sidebar_navigation"} + = render 'spree/sidebar' + + +%h1 Suppliers + += cms_page_content(:content, Cms::Page.find_by_full_path('/enterprises/suppliers')) + +%ul.enterprises + - @suppliers.each do |supplier| + %li= link_to supplier.name, supplier diff --git a/app/views/spree/products/_source_sidebar.html.haml b/app/views/spree/products/_source_sidebar.html.haml index 88e1ed1cf5..bd25db53a0 100644 --- a/app/views/spree/products/_source_sidebar.html.haml +++ b/app/views/spree/products/_source_sidebar.html.haml @@ -2,9 +2,9 @@ %h6.filter_name Shop by Supplier %ul.filter_choices - @suppliers.each do |supplier| - - if supplier.has_products_on_hand? + - if supplier.has_supplied_products_on_hand? %li.nowrap= link_to supplier.name, [main_app, supplier] - = button_to 'Browse All Suppliers', main_app.suppliers_path, :method => :get + = button_to 'Browse All Suppliers', main_app.suppliers_enterprises_path, :method => :get %h6.filter_name Shop by Distributor %ul.filter_choices @@ -12,10 +12,10 @@ - @distributors.each do |distributor| %li.nowrap - if order.nil? || order.can_change_distributor? - = link_to distributor.name, main_app.select_distributor_path(distributor) + = link_to distributor.name, main_app.select_distributor_enterprise_path(distributor) - elsif order.distributor == distributor = link_to distributor.name, [main_app, distributor] - else %span.inactive= distributor.name - if current_distributor && order.can_change_distributor? - = button_to 'Browse All Distributors', main_app.deselect_distributors_path, :method => :get + = button_to 'Browse All Distributors', main_app.deselect_distributor_enterprises_path, :method => :get diff --git a/config/routes.rb b/config/routes.rb index 7fb03b6950..67bd610c69 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,15 @@ Openfoodweb::Application.routes.draw do root :to => 'spree/home#index' + resources :enterprises do + get :suppliers, :on => :collection + get :select_distributor, :on => :member + get :deselect_distributor, :on => :collection + end + + # Deprecated resources :suppliers + # Deprecated resources :distributors do get :select, :on => :member get :deselect, :on => :collection @@ -11,9 +19,12 @@ Openfoodweb::Application.routes.draw do resources :enterprises do post :bulk_update, :on => :collection, :as => :bulk_update end + + # Deprecated resources :distributors do post :bulk_update, :on => :collection, :as => :bulk_update end + # Deprecated resources :suppliers end diff --git a/db/schema.rb b/db/schema.rb index 6267fefdd9..c03b5d21f4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -282,8 +282,8 @@ ActiveRecord::Schema.define(:version => 20121028070200) do t.string "start_year" t.string "issue_number" t.integer "address_id" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "gateway_customer_profile_id" t.string "gateway_payment_profile_id" end diff --git a/spec/controllers/enterprises_controller_spec.rb b/spec/controllers/enterprises_controller_spec.rb new file mode 100644 index 0000000000..1554984dfb --- /dev/null +++ b/spec/controllers/enterprises_controller_spec.rb @@ -0,0 +1,83 @@ +require 'spec_helper' +require 'spree/core/current_order' + +describe EnterprisesController do + include Spree::Core::CurrentOrder + + before :each do + stub!(:before_save_new_order) + stub!(:after_save_new_order) + + create(:itemwise_shipping_method) + end + + it "displays suppliers" do + s = create(:supplier_enterprise) + d = create(:distributor_enterprise) + + spree_get :suppliers + + assigns(:suppliers).should == [s] + end + + it "selects distributors" do + d = create(:distributor_enterprise) + + spree_get :select_distributor, :id => d.id + response.should be_redirect + + order = current_order(false) + order.distributor.should == d + end + + it "deselects distributors" do + d = create(:distributor_enterprise) + order = current_order(true) + order.distributor = d + order.save! + + spree_get :deselect_distributor + response.should be_redirect + + order.reload + order.distributor.should be_nil + end + + context "when a product has been added to the cart" do + it "does not allow selecting another distributor" do + # Given some distributors and an order with a product + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) + p = create(:product, :distributors => [d1]) + o = current_order(true) + + o.distributor = d1 + o.save! + o.add_variant(p.master, 1) + + # When I attempt to select a distributor + spree_get :select_distributor, :id => d2.id + + # Then my distributor should remain unchanged + o.reload + o.distributor.should == d1 + end + + it "does not allow deselecting distributors" do + # Given a distributor and an order with a product + d = create(:distributor_enterprise) + p = create(:product, :distributors => [d]) + o = current_order(true) + o.distributor = d + o.save! + o.add_variant(p.master, 1) + + # When I attempt to deselect the distributor + spree_get :deselect_distributor + + # Then my distributor should remain unchanged + o.reload + o.distributor.should == d + end + end +end diff --git a/spec/requests/consumer/add_to_cart_spec.rb b/spec/requests/consumer/add_to_cart_spec.rb index d528866647..de9ccce8da 100644 --- a/spec/requests/consumer/add_to_cart_spec.rb +++ b/spec/requests/consumer/add_to_cart_spec.rb @@ -10,8 +10,8 @@ feature %q{ scenario "adding a product to the cart with no distributor chosen" do # Given a product and some distributors - d1 = create(:distributor) - d2 = create(:distributor) + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) p = create(:product, :distributors => [d1]) create(:product, :distributors => [d2]) @@ -30,8 +30,8 @@ feature %q{ create(:itemwise_shipping_method) # Given a product, some distributors and a defined shipping cost - d1 = create(:distributor) - d2 = create(:distributor) + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) create(:product, :distributors => [d2]) p = create(:product, :price => 12.34) create(:product_distribution, :product => p, :distributor => d1, :shipping_method => create(:shipping_method)) @@ -67,8 +67,8 @@ feature %q{ it "does not allow the user to change distributor after a product has been added to the cart" do # Given a product and some distributors - d1 = create(:distributor) - d2 = create(:distributor) + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) p = create(:product, :distributors => [d1]) # When I add a product to my cart (which sets my distributor) @@ -87,7 +87,7 @@ feature %q{ context "adding a subsequent product to the cart" do it "does not allow the user to choose a distributor" do # Given a product under a distributor - d = create(:distributor) + d = create(:distributor_enterprise) p = create(:product, :distributors => [d]) # And a product in my cart @@ -103,8 +103,8 @@ feature %q{ it "does not allow the user to add a product from another distributor" do # Given two products, each at a different distributor - d1 = create(:distributor) - d2 = create(:distributor) + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) p1 = create(:product, :distributors => [d1]) p2 = create(:product, :distributors => [d2]) @@ -123,7 +123,7 @@ feature %q{ it "adds products with valid distributors" do # Given two products, each at the same distributor - d = create(:distributor) + d = create(:distributor_enterprise) p1 = create(:product, :distributors => [d]) p2 = create(:product, :distributors => [d]) @@ -146,7 +146,7 @@ feature %q{ context "group buys" do scenario "adding a product to the cart for a group buy" do # Given a group buy product and a distributor - d = create(:distributor) + d = create(:distributor_enterprise) p = create(:product, :distributors => [d], :group_buy => true) # When I add the item to my cart @@ -166,7 +166,7 @@ feature %q{ scenario "adding a product with variants to the cart for a group buy" do # Given a group buy product with variants and a distributor - d = create(:distributor) + d = create(:distributor_enterprise) p = create(:product, :distributors => [d], :group_buy => true) create(:variant, :product => p) @@ -187,7 +187,7 @@ feature %q{ scenario "adding a product to cart that is not a group buy does not show max quantity field" do # Given a group buy product and a distributor - d = create(:distributor) + d = create(:distributor_enterprise) p = create(:product, :distributors => [d], :group_buy => false) # When I view the add to cart form, there should not be a max quantity field @@ -198,7 +198,7 @@ feature %q{ scenario "adding a product with a max quantity less than quantity results in max_quantity==quantity" do # Given a group buy product and a distributor - d = create(:distributor) + d = create(:distributor_enterprise) p = create(:product, :distributors => [d], :group_buy => true) # When I add the item to my cart