Skip to content

Commit

Permalink
Add custom base controller
Browse files Browse the repository at this point in the history
The controller code we were using relied on a patch to Solidus[1] that will
only be released with Solidus 4.2. We want to be compatible with Solidus
4 though, so we're incorporating that patch into our own BaseController
  and use that instead of Solidus' ResourceController.

[1] solidusio#5219
  • Loading branch information
mamhoff committed Oct 25, 2024
1 parent c52a92b commit 179202f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

module SolidusFriendlyPromotions
module Admin
class BaseController < Spree::Admin::ResourceController
def parent
@parent ||= self.class.parent_data[:model_class]
.includes(self.class.parent_data[:includes])
.find_by!(self.class.parent_data[:find_by] => params["#{parent_model_name}_id"])
instance_variable_set("@#{parent_model_name}", @parent)
rescue ActiveRecord::RecordNotFound => e
resource_not_found(flash_class: e.model.constantize, redirect_url: routes_proxy.polymorphic_url([:admin, parent_model_name.pluralize.to_sym]))
end

def new_object_url(options = {})
if parent?
routes_proxy.new_polymorphic_url([:admin, parent, model_class], options)
else
routes_proxy.new_polymorphic_url([:admin, model_class], options)
end
end

def edit_object_url(object, options = {})
if parent?
routes_proxy.polymorphic_url([:edit, :admin, parent, object], options)
else
routes_proxy.polymorphic_url([:edit, :admin, object], options)
end
end

def object_url(object = nil, options = {})
target = object || @object

if parent?
routes_proxy.polymorphic_url([:admin, parent, target], options)
else
routes_proxy.polymorphic_url([:admin, target], options)
end
end

def collection_url(options = {})
if parent?
routes_proxy.polymorphic_url([:admin, parent, model_class], options)
else
routes_proxy.polymorphic_url([:admin, model_class], options)
end
end

def routes_proxy
solidus_friendly_promotions
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

module SolidusFriendlyPromotions
module Admin
class PromotionCategoriesController < Spree::Admin::ResourceController
class PromotionCategoriesController < BaseController
private

def model_class
SolidusFriendlyPromotions::PromotionCategory
end

def routes_proxy
solidus_friendly_promotions
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module SolidusFriendlyPromotions
module Admin
class PromotionCodeBatchesController < Spree::Admin::ResourceController
class PromotionCodeBatchesController < BaseController
belongs_to "solidus_friendly_promotions/promotion"

create.after :build_promotion_code_batch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module SolidusFriendlyPromotions
module Admin
class PromotionCodesController < Spree::Admin::ResourceController
class PromotionCodesController < BaseController
before_action :load_promotion

def index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module SolidusFriendlyPromotions
module Admin
class PromotionsController < ::Spree::Admin::ResourceController
class PromotionsController < BaseController
before_action :load_data

helper "solidus_friendly_promotions/admin/promotion_rules"
Expand Down Expand Up @@ -59,10 +59,6 @@ def load_data
def location_after_save
solidus_friendly_promotions.edit_admin_promotion_url(@promotion)
end

def routes_proxy
solidus_friendly_promotions
end
end
end
end

0 comments on commit 179202f

Please sign in to comment.