From c3659612ed12df092d8256cf25aa601319c1cf9c Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 16 Jan 2015 10:12:17 +1100 Subject: [PATCH] enterprise routes use permalink --- .../admin/enterprises_controller.rb | 7 ++++++- app/controllers/api/enterprises_controller.rb | 4 ++-- app/controllers/enterprises_controller.rb | 4 ++-- app/models/enterprise.rb | 2 +- .../admin/enterprises_controller_spec.rb | 20 +++++++++---------- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index af2f2f5425..9232144c21 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -18,7 +18,7 @@ module Admin end def set_sells - enterprise = Enterprise.find(params[:id]) + enterprise = Enterprise.find_by_permalink(params[:id]) || Enterprise.find(params[:id]) attributes = { sells: params[:sells] } attributes[:producer_profile_only] = params[:sells] == "none" && !!params[:producer_profile_only] attributes[:shop_trial_start_date] = Time.now if params[:sells] == "own" @@ -63,6 +63,11 @@ module Admin end alias_method_chain :build_resource, :address + # Overriding method on Spree's resource controller, + # so that resources are found using permalink + def find_resource + Enterprise.find_by_permalink(params[:id]) + end private diff --git a/app/controllers/api/enterprises_controller.rb b/app/controllers/api/enterprises_controller.rb index cba50c4923..d40d67409d 100644 --- a/app/controllers/api/enterprises_controller.rb +++ b/app/controllers/api/enterprises_controller.rb @@ -29,7 +29,7 @@ module Api end def update - @enterprise = Enterprise.find(params[:id]) + @enterprise = Enterprise.find_by_permalink(params[:id]) || Enterprise.find(params[:id]) authorize! :update, @enterprise if @enterprise.update_attributes(params[:enterprise]) @@ -40,7 +40,7 @@ module Api end def update_image - @enterprise = Enterprise.find(params[:id]) + @enterprise = Enterprise.find_by_permalink(params[:id]) || Enterprise.find(params[:id]) authorize! :update, @enterprise if params[:logo] && @enterprise.update_attributes( { logo: params[:logo] } ) diff --git a/app/controllers/enterprises_controller.rb b/app/controllers/enterprises_controller.rb index 571d17e3eb..27a335d9c4 100644 --- a/app/controllers/enterprises_controller.rb +++ b/app/controllers/enterprises_controller.rb @@ -24,7 +24,7 @@ class EnterprisesController < BaseController end def show - @enterprise = Enterprise.find params[:id] + @enterprise = Enterprise.find_by_permalink(params[:id]) || Enterprise.find(params[:id]) # User can view this page if they've already chosen their distributor, or if this page # is for a supplier, they may use it to select a distributor that sells this supplier's @@ -53,7 +53,7 @@ class EnterprisesController < BaseController end def shop - distributor = Enterprise.is_distributor.find params[:id] + distributor = Enterprise.is_distributor.find_by_permalink(params[:id]) || Enterprise.is_distributor.find(params[:id]) order = current_order(true) if order.distributor and order.distributor != distributor diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 9c516666a7..fbb525cfe8 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -202,7 +202,7 @@ class Enterprise < ActiveRecord::Base end def to_param - "#{id}-#{name.parameterize}" + permalink end def relatives diff --git a/spec/controllers/admin/enterprises_controller_spec.rb b/spec/controllers/admin/enterprises_controller_spec.rb index 01bfebfa42..6b29d00107 100644 --- a/spec/controllers/admin/enterprises_controller_spec.rb +++ b/spec/controllers/admin/enterprises_controller_spec.rb @@ -90,7 +90,7 @@ module Admin it "does not allow 'sells' to be changed" do profile_enterprise.enterprise_roles.build(user: user).save controller.stub spree_current_user: user - enterprise_params = { id: profile_enterprise.id, enterprise: { sells: 'any' } } + enterprise_params = { id: profile_enterprise, enterprise: { sells: 'any' } } spree_put :update, enterprise_params profile_enterprise.reload @@ -101,7 +101,7 @@ module Admin context "as super admin" do it "allows 'sells' to be changed" do controller.stub spree_current_user: admin_user - enterprise_params = { id: profile_enterprise.id, enterprise: { sells: 'any' } } + enterprise_params = { id: profile_enterprise, enterprise: { sells: 'any' } } spree_put :update, enterprise_params profile_enterprise.reload @@ -131,7 +131,7 @@ module Admin context "allows setting 'sells' to 'none'" do it "is allowed" do - spree_post :set_sells, { id: enterprise.id, sells: 'none' } + spree_post :set_sells, { id: enterprise, sells: 'none' } expect(response).to redirect_to spree.admin_path expect(flash[:success]).to eq "Congratulations! Registration for #{enterprise.name} is complete!" expect(enterprise.reload.sells).to eq 'none' @@ -139,7 +139,7 @@ module Admin context "setting producer_profile_only to true" do it "is allowed" do - spree_post :set_sells, { id: enterprise.id, sells: 'none', producer_profile_only: true } + spree_post :set_sells, { id: enterprise, sells: 'none', producer_profile_only: true } expect(response).to redirect_to spree.admin_path expect(enterprise.reload.producer_profile_only).to eq true end @@ -159,7 +159,7 @@ module Admin end it "is disallowed" do - spree_post :set_sells, { id: enterprise.id, sells: 'own' } + spree_post :set_sells, { id: enterprise, sells: 'own' } expect(response).to redirect_to spree.admin_path trial_expiry = Date.today.strftime("%Y-%m-%d") expect(flash[:error]).to eq "Sorry, but you've already had a trial. Expired on: #{trial_expiry}" @@ -175,7 +175,7 @@ module Admin end it "is allowed, but trial start date is not reset" do - spree_post :set_sells, { id: enterprise.id, sells: 'own' } + spree_post :set_sells, { id: enterprise, sells: 'own' } expect(response).to redirect_to spree.admin_path trial_expiry = (Date.today + 30.days).strftime("%Y-%m-%d") expect(flash[:notice]).to eq "Welcome back! Your trial expires on: #{trial_expiry}" @@ -186,7 +186,7 @@ module Admin context "if a trial has not started" do it "is allowed" do - spree_post :set_sells, { id: enterprise.id, sells: 'own' } + spree_post :set_sells, { id: enterprise, sells: 'own' } expect(response).to redirect_to spree.admin_path expect(flash[:success]).to eq "Congratulations! Registration for #{enterprise.name} is complete!" expect(enterprise.reload.sells).to eq 'own' @@ -196,7 +196,7 @@ module Admin context "setting producer_profile_only to true" do it "is ignored" do - spree_post :set_sells, { id: enterprise.id, sells: 'own', producer_profile_only: true } + spree_post :set_sells, { id: enterprise, sells: 'own', producer_profile_only: true } expect(response).to redirect_to spree.admin_path expect(enterprise.reload.producer_profile_only).to be false end @@ -205,7 +205,7 @@ module Admin context "setting 'sells' to any" do it "is not allowed" do - spree_post :set_sells, { id: enterprise.id, sells: 'any' } + spree_post :set_sells, { id: enterprise, sells: 'any' } expect(response).to redirect_to spree.admin_path expect(flash[:error]).to eq "Unauthorised" expect(enterprise.reload.sells).to eq 'none' @@ -214,7 +214,7 @@ module Admin context "settiing 'sells' to 'unspecified'" do it "is not allowed" do - spree_post :set_sells, { id: enterprise.id, sells: 'unspecified' } + spree_post :set_sells, { id: enterprise, sells: 'unspecified' } expect(response).to redirect_to spree.admin_path expect(flash[:error]).to eq "Unauthorised" expect(enterprise.reload.sells).to eq 'none'