diff --git a/app/controllers/base_controller.rb b/app/controllers/base_controller.rb new file mode 100644 index 0000000000..8c951f993a --- /dev/null +++ b/app/controllers/base_controller.rb @@ -0,0 +1,10 @@ +class BaseController < ApplicationController + include Spree::Core::ControllerHelpers + include Spree::Core::RespondWith + + helper 'spree/base' + + # Spree::Core::ControllerHelpers declares helper_method get_taxonomies, so we need to + # include Spree::ProductsHelper so that method is available on the controller + include Spree::ProductsHelper +end diff --git a/app/controllers/distributors_controller.rb b/app/controllers/distributors_controller.rb new file mode 100644 index 0000000000..97c6d81691 --- /dev/null +++ b/app/controllers/distributors_controller.rb @@ -0,0 +1,35 @@ +class DistributorsController < BaseController + def show + options = {:distributor_id => params[:id]} + options.merge(params.reject { |k,v| k == :id }) + + @distributor = Distributor.find params[:id] + + @searcher = Spree::Config.searcher_class.new(options) + @products = @searcher.retrieve_products + end + + def select + distributor = 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 + 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/controllers/spree/distributors_controller.rb b/app/controllers/spree/distributors_controller.rb deleted file mode 100644 index ecce2dfd6b..0000000000 --- a/app/controllers/spree/distributors_controller.rb +++ /dev/null @@ -1,39 +0,0 @@ -module Spree - class DistributorsController < BaseController - helper 'spree/products' - - def show - options = {:distributor_id => params[:id]} - options.merge(params.reject { |k,v| k == :id }) - - @distributor = Distributor.find params[:id] - - @searcher = Config.searcher_class.new(options) - @products = @searcher.retrieve_products - end - - def select - distributor = 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 - order = current_order(true) - - if order.can_change_distributor? - order.distributor = nil - order.save! - end - - redirect_to root_path - end - end -end diff --git a/app/controllers/spree/suppliers_controller.rb b/app/controllers/spree/suppliers_controller.rb deleted file mode 100644 index 57c82cdf6d..0000000000 --- a/app/controllers/spree/suppliers_controller.rb +++ /dev/null @@ -1,19 +0,0 @@ -module Spree - class SuppliersController < BaseController - helper 'spree/products' - - def index - @suppliers = Supplier.all - end - - def show - options = {:supplier_id => params[:id]} - options.merge(params.reject { |k,v| k == :id }) - - @supplier = Supplier.find params[:id] - - @searcher = Config.searcher_class.new(options) - @products = @searcher.retrieve_products - end - end -end diff --git a/app/controllers/suppliers_controller.rb b/app/controllers/suppliers_controller.rb new file mode 100644 index 0000000000..3b76225449 --- /dev/null +++ b/app/controllers/suppliers_controller.rb @@ -0,0 +1,15 @@ +class SuppliersController < BaseController + def index + @suppliers = Supplier.all + end + + def show + options = {:supplier_id => params[:id]} + options.merge(params.reject { |k,v| k == :id }) + + @supplier = Supplier.find params[:id] + + @searcher = Spree::Config.searcher_class.new(options) + @products = @searcher.retrieve_products + end +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index db628debaf..4f41a1a0ed 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -4,4 +4,15 @@ module ApplicationHelper cms_page_content(:content, Cms::Page.find_by_full_path('/')) end end + + + # Pass URL helper calls on to spree where applicable so that we don't need to use + # spree.foo_path in any view rendered from non-spree-namespaced controllers. + def method_missing(method, *args, &block) + if (method.to_s.end_with?('_path') || method.to_s.end_with?('_url')) && spree.respond_to?(method) + spree.send(method, *args) + else + super + end + end end diff --git a/app/views/spree/distributors/show.html.haml b/app/views/distributors/show.html.haml similarity index 100% rename from app/views/spree/distributors/show.html.haml rename to app/views/distributors/show.html.haml diff --git a/app/views/spree/products/_source_sidebar.html.haml b/app/views/spree/products/_source_sidebar.html.haml index f4ce33d3ea..88e1ed1cf5 100644 --- a/app/views/spree/products/_source_sidebar.html.haml +++ b/app/views/spree/products/_source_sidebar.html.haml @@ -3,8 +3,8 @@ %ul.filter_choices - @suppliers.each do |supplier| - if supplier.has_products_on_hand? - %li.nowrap= link_to supplier.name, supplier - = button_to 'Browse All Suppliers', suppliers_path, :method => :get + %li.nowrap= link_to supplier.name, [main_app, supplier] + = button_to 'Browse All Suppliers', main_app.suppliers_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, select_distributor_path(distributor) + = link_to distributor.name, main_app.select_distributor_path(distributor) - elsif order.distributor == distributor - = link_to distributor.name, 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', deselect_distributors_path, :method => :get + = button_to 'Browse All Distributors', main_app.deselect_distributors_path, :method => :get diff --git a/app/views/spree/suppliers/index.html.haml b/app/views/suppliers/index.html.haml similarity index 100% rename from app/views/spree/suppliers/index.html.haml rename to app/views/suppliers/index.html.haml diff --git a/app/views/spree/suppliers/show.html.haml b/app/views/suppliers/show.html.haml similarity index 100% rename from app/views/spree/suppliers/show.html.haml rename to app/views/suppliers/show.html.haml diff --git a/config/routes.rb b/config/routes.rb index 0befa2892e..fefb914567 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,16 +1,18 @@ Openfoodweb::Application.routes.draw do - # Mount Spree's routes - mount Spree::Core::Engine, :at => '/' -end + root :to => 'spree/home#index' - -Spree::Core::Engine.routes.prepend do resources :suppliers resources :distributors do get :select, :on => :member get :deselect, :on => :collection end + # Mount Spree's routes + mount Spree::Core::Engine, :at => '/' +end + + +Spree::Core::Engine.routes.prepend do namespace :admin do resources :distributors do post :bulk_update, :on => :collection, :as => :bulk_update @@ -20,5 +22,4 @@ Spree::Core::Engine.routes.prepend do match '/admin/reports/orders_and_distributors' => 'admin/reports#orders_and_distributors', :as => "orders_and_distributors_admin_reports", :via => [:get, :post] match '/admin/reports/group_buys' => 'admin/reports#group_buys', :as => "group_buys_admin_reports", :via => [:get, :post] - end diff --git a/spec/controllers/distributors_controller_spec.rb b/spec/controllers/distributors_controller_spec.rb index 3595b780e0..488ee24b39 100644 --- a/spec/controllers/distributors_controller_spec.rb +++ b/spec/controllers/distributors_controller_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' require 'spree/core/current_order' -describe Spree::DistributorsController do +describe DistributorsController do include Spree::Core::CurrentOrder before :each do