Skip to content

Commit

Permalink
Merge pull request #13 from friendlycart/use-different-path-for-frien…
Browse files Browse the repository at this point in the history
…dly-promotions

Use different path for friendly promotions
  • Loading branch information
mamhoff authored Oct 6, 2023
2 parents 904c07e + 9a8a5ab commit f99bcab
Show file tree
Hide file tree
Showing 23 changed files with 110 additions and 54 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% admin_breadcrumb(link_to plural_resource_name(Spree::Promotion), solidus_friendly_promotions.admin_promotions_path) %>
<% admin_breadcrumb(link_to plural_resource_name(Spree::PromotionCategory), solidus_friendly_promotions.admin_promotion_categories_path) %>
<% admin_breadcrumb(link_to plural_resource_name(SolidusFriendlyPromotions::Promotion), solidus_friendly_promotions.admin_promotions_path) %>
<% admin_breadcrumb(link_to plural_resource_name(SolidusFriendlyPromotions::PromotionCategory), solidus_friendly_promotions.admin_promotion_categories_path) %>
<% admin_breadcrumb(@promotion_category.name) %>

<%= form_for @promotion_category, url: object_url, method: :put do |f| %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% admin_breadcrumb(link_to plural_resource_name(Spree::Promotion), solidus_friendly_promotions.admin_promotions_path) %>
<% admin_breadcrumb(link_to plural_resource_name(Spree::PromotionCategory), solidus_friendly_promotions.admin_promotion_categories_path) %>
<% admin_breadcrumb(link_to plural_resource_name(SolidusFriendlyPromotions::Promotion), solidus_friendly_promotions.admin_promotions_path) %>
<% admin_breadcrumb(link_to plural_resource_name(SolidusFriendlyPromotions::PromotionCategory), solidus_friendly_promotions.admin_promotion_categories_path) %>
<% admin_breadcrumb(t('spree.new_promotion_category')) %>

<%= form_for :promotion_category, url: collection_url do |f| %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<% admin_breadcrumb link_to plural_resource_name(Spree::Promotion), spree.admin_promotions_path %>
<% admin_breadcrumb link_to(@promotion.name, spree.edit_admin_promotion_path(@promotion)) %>
<% admin_breadcrumb plural_resource_name(Spree::PromotionCode) %>
<% admin_breadcrumb link_to plural_resource_name(SolidusFriendlyPromotions::Promotion), solidus_friendly_promotions.admin_promotions_path %>
<% admin_breadcrumb link_to(@promotion.name, solidus_friendly_promotions.edit_admin_promotion_path(@promotion)) %>
<% admin_breadcrumb plural_resource_name(SolidusFriendlyPromotions::PromotionCode) %>

<% content_for :page_actions do %>
<li>
<% if can?(:create, Spree::PromotionCode) && !@promotion.apply_automatically? %>
<%= link_to t('spree.create_promotion_code'), new_admin_promotion_promotion_code_path(promotion_id: @promotion.id), class: 'btn btn-primary' %>
<% if can?(:create, SolidusFriendlyPromotions::PromotionCode) && !@promotion.apply_automatically? %>
<%= link_to t('spree.create_promotion_code'), solidus_friendly_promotions.new_admin_promotion_promotion_code_path(promotion_id: @promotion.id), class: 'btn btn-primary' %>
<% end %>

<%= link_to t('spree.download_promotion_codes_list'), admin_promotion_promotion_codes_path(promotion_id: @promotion.id, format: :csv), class: 'btn btn-primary' %>
<%= link_to t('spree.download_promotion_codes_list'), solidus_friendly_promotions.admin_promotion_promotion_codes_path(promotion_id: @promotion.id, format: :csv), class: 'btn btn-primary' %>
</li>
<% end %>

Expand All @@ -18,7 +18,7 @@

<table>
<thead>
<th><%= Spree::PromotionCode.human_attribute_name :value %></th>
<th><%= SolidusFriendlyPromotions::PromotionCode.human_attribute_name :value %></th>
</thead>
<tbody>
<% @promotion_codes.each do |promotion_code| %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% admin_breadcrumb link_to plural_resource_name(Spree::Promotion), solidus_friendly_promotions.admin_promotions_path %>
<% admin_breadcrumb link_to plural_resource_name(SolidusFriendlyPromotions::Promotion), solidus_friendly_promotions.admin_promotions_path %>
<% admin_breadcrumb link_to(@promotion.name, solidus_friendly_promotions.edit_admin_promotion_path(@promotion)) %>
<% admin_breadcrumb plural_resource_name(Spree::PromotionCode) %>
<% admin_breadcrumb plural_resource_name(SolidusFriendlyPromotions::PromotionCode) %>

<% content_for :page_actions do %>
<li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<%= form_with model: promotion_rule, scope: :promotion_rule, url: solidus_friendly_promotions.admin_promotion_promotion_rule_path(@promotion, promotion_rule), method: :patch do |form| %>
<h6 class='promotion-title'><%= promotion_rule.class.model_name.human %></h6>
<% if can?(:destroy, promotion_rule) %>
<%= link_to_with_icon 'trash', '', spree.admin_promotion_promotion_rule_path(@promotion, promotion_rule), method: :delete, class: 'delete' %>
<%= link_to_with_icon 'trash', '', solidus_friendly_promotions.admin_promotion_promotion_rule_path(@promotion, promotion_rule), method: :delete, class: 'delete' %>
<% end %>
<p>
<%= promotion_rule.class.human_attribute_name(:description) %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= turbo_frame_tag @promotion, "new_#{@level}_promotion_rule" do %>
<div class="promotion_rule promotion-block">
<h6 class='promotion-title'> <%= t(:add_rule, scope: :solidus_friendly_promotions) %></h6>
<%= link_to_with_icon 'trash', '', spree.edit_admin_promotion_path(@promotion), class: 'delete' %>
<%= link_to_with_icon 'trash', '', solidus_friendly_promotions.edit_admin_promotion_path(@promotion), class: 'delete' %>
<%= render 'type_select', level: @level %>
<% flash.each do |severity, message| %>
<%= content_tag(:div, "", data: { controller: :flash, severity: severity, message: message }) %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="row">
<div class="col-6">
<div class="field">
<%= Spree::Promotion::Rules::FirstRepeatPurchaseSince.human_attribute_name(:form_text) %>
<%= SolidusFriendlyPromotions::Rules::FirstRepeatPurchaseSince.human_attribute_name(:form_text) %>
</div>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="row">
<div class="col-6">
<div class="field">
<%= Spree::Promotion::Rules::NthOrder.human_attribute_name(:form_text) %>
<%= SolidusFriendlyPromotions::Rules::NthOrder.human_attribute_name(:form_text) %>
</div>
</div>
<div class="col-6">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="col-12">
<div class="field">
<label>
<%= t('spree.product_rule.label', select: select_tag("#{param_prefix}[preferred_match_policy]", options_for_select(Spree::Promotion::Rules::Product::MATCH_POLICIES.map{|s| [t("spree.product_rule.match_#{s}"),s] }, promotion_rule.preferred_match_policy), {class: 'select_product custom-select'})).html_safe %>
<%= t('spree.product_rule.label', select: select_tag("#{param_prefix}[preferred_match_policy]", options_for_select(SolidusFriendlyPromotions::Rules::Product::MATCH_POLICIES.map{|s| [t("spree.product_rule.match_#{s}"),s] }, promotion_rule.preferred_match_policy), {class: 'select_product custom-select'})).html_safe %>
</label>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
</div>
<div class="field">
<label>
<%= t('spree.taxon_rule.label', select: select_tag("#{param_prefix}[preferred_match_policy]", options_for_select(Spree::Promotion::Rules::Taxon::MATCH_POLICIES.map{|s| [t("spree.taxon_rule.match_#{s}"),s] }, promotion_rule.preferred_match_policy), {class: 'select_taxon custom-select'})).html_safe %>
<%= t('spree.taxon_rule.label', select: select_tag("#{param_prefix}[preferred_match_policy]", options_for_select(SolidusFriendlyPromotions::Rules::Taxon::MATCH_POLICIES.map{|s| [t("spree.taxon_rule.match_#{s}"),s] }, promotion_rule.preferred_match_policy), {class: 'select_taxon custom-select'})).html_safe %>
</label>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
</div>
<div class="form-group">
<label>
<%= t('spree.user_role_rule.label', select: select_tag("#{param_prefix}[preferred_match_policy]", options_for_select(Spree::Promotion::Rules::UserRole::MATCH_POLICIES.map{ |s| [t("spree.user_role_rule.match_#{s}"),s] }, promotion_rule.preferred_match_policy), { class: 'custom-select'})).html_safe %>
<%= t('spree.user_role_rule.label', select: select_tag("#{param_prefix}[preferred_match_policy]", options_for_select(SolidusFriendlyPromotions::Rules::UserRole::MATCH_POLICIES.map{ |s| [t("spree.user_role_rule.match_#{s}"),s] }, promotion_rule.preferred_match_policy), { class: 'custom-select'})).html_safe %>
</label>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

<div data-activation-type="single_code">
<div class="field">
<%= label_tag :single_code, Spree::PromotionCode.model_name.human, class: "required" %>
<%= label_tag :single_code, SolidusFriendlyPromotions::PromotionCode.model_name.human, class: "required" %>
<%= text_field_tag :single_code, @promotion.codes.first.try!(:value), class: "fullwidth", required: true %>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% admin_layout "full-width" %>

<% admin_breadcrumb(link_to plural_resource_name(Spree::Promotion), solidus_friendly_promotions.admin_promotions_path) %>
<% admin_breadcrumb(link_to plural_resource_name(SolidusFriendlyPromotions::Promotion), solidus_friendly_promotions.admin_promotions_path) %>
<% admin_breadcrumb(@promotion.name) %>

<%= content_for :head do %>
Expand All @@ -9,14 +9,14 @@

<% content_for :page_actions do %>
<li>
<% if can?(:show, Spree::PromotionCode) %>
<% if can?(:show, SolidusFriendlyPromotions::PromotionCode) %>
<%= link_to t('solidus_friendly_promotions.view_promotion_codes_list'), solidus_friendly_promotions.admin_promotion_promotion_codes_path(promotion_id: @promotion.id), class: 'btn btn-primary' %>

<%= link_to t('solidus_friendly_promotions.download_promotion_codes_list'), solidus_friendly_promotions.admin_promotion_promotion_codes_path(promotion_id: @promotion.id, format: :csv), class: 'btn btn-primary' %>
<% end %>

<% if can?(:show, Spree::PromotionCodeBatch) %>
<%= link_to plural_resource_name(Spree::PromotionCodeBatch), solidus_friendly_promotions.admin_promotion_promotion_code_batches_path(promotion_id: @promotion.id), class: 'btn btn-primary' %>
<% if can?(:show, SolidusFriendlyPromotions::PromotionCodeBatch) %>
<%= link_to plural_resource_name(SolidusFriendlyPromotions::PromotionCodeBatch), solidus_friendly_promotions.admin_promotion_promotion_code_batches_path(promotion_id: @promotion.id), class: 'btn btn-primary' %>
<% end %>
</li>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<% admin_breadcrumb(plural_resource_name(Spree::Promotion)) %>
<% admin_breadcrumb(plural_resource_name(SolidusFriendlyPromotions::Promotion)) %>

<% content_for :page_actions do %>
<% if can? :create, Spree::Promotion %>
<% if can? :create, SolidusFriendlyPromotions::Promotion %>
<li>
<%= link_to t('spree.new_promotion'), spree.new_admin_promotion_path, class: 'btn btn-primary' %>
<%= link_to t('solidus_friendly_promotions.new_promotion'), solidus_friendly_promotions.new_admin_promotion_path, class: 'btn btn-primary' %>
</li>
<li>
<%= link_to t('solidus_friendly_promotions.legacy_promotions'), spree.admin_promotions_path, class: 'btn btn-primary' %>
</li>
<% end %>
<% end %>
Expand Down Expand Up @@ -69,13 +72,13 @@
<table class="index">
<thead>
<tr>
<th><%= Spree::Promotion.human_attribute_name(:name) %></th>
<th><%= Spree::Promotion.human_attribute_name(:code) %></th>
<th><%= Spree::Promotion.human_attribute_name(:status) %></th>
<th><%= Spree::Promotion.human_attribute_name(:usage_limit) %></th>
<th><%= Spree::Promotion.human_attribute_name(:uses) %></th>
<th><%= Spree::Promotion.human_attribute_name(:starts_at) %></th>
<th><%= Spree::Promotion.human_attribute_name(:expires_at) %></th>
<th><%= SolidusFriendlyPromotions::Promotion.human_attribute_name(:name) %></th>
<th><%= SolidusFriendlyPromotions::Promotion.human_attribute_name(:code) %></th>
<th><%= SolidusFriendlyPromotions::Promotion.human_attribute_name(:status) %></th>
<th><%= SolidusFriendlyPromotions::Promotion.human_attribute_name(:usage_limit) %></th>
<th><%= SolidusFriendlyPromotions::Promotion.human_attribute_name(:uses) %></th>
<th><%= SolidusFriendlyPromotions::Promotion.human_attribute_name(:starts_at) %></th>
<th><%= SolidusFriendlyPromotions::Promotion.human_attribute_name(:expires_at) %></th>
<th class="actions"></th>
</tr>
</thead>
Expand Down Expand Up @@ -118,7 +121,7 @@
<% else %>
<div class="no-objects-found">
<%= render 'spree/admin/shared/no_objects_found',
resource: Spree::Promotion,
resource: SolidusFriendlyPromotions::Promotion,
new_resource_url: new_object_url %>
</div>
<% end %>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% admin_layout "full-width" %>

<% admin_breadcrumb(link_to plural_resource_name(Spree::Promotion), solidus_friendly_promotions.admin_promotions_path) %>
<% admin_breadcrumb(link_to plural_resource_name(SolidusFriendlyPromotions::Promotion), solidus_friendly_promotions.admin_promotions_path) %>
<% admin_breadcrumb(t('solidus_friendly_promotions.new_promotion')) %>

<%= form_for @promotion, url: collection_url do |f| %>
Expand Down
1 change: 1 addition & 0 deletions promotions/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ en:
new_promotion: New Promotion
new_promotion_category: New Promotion Category
new_promotion_code_batch: New Promotion Code Batch
legacy_promotions: Legacy Promotions
no_rules_addes: No Rules Added
promotion_successfully_created: Promotion has been successfully created!
promotion_total_changed_before_complete: One or more of the promotions on your order have become ineligible and were removed. Please check the new order amounts and try again.
Expand Down
16 changes: 9 additions & 7 deletions promotions/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

SolidusFriendlyPromotions::Engine.routes.draw do
namespace :admin do
resources :promotion_categories, except: [:show]
scope :friendly do
resources :promotion_categories, except: [:show]

resources :promotions do
resources :promotion_rules
resources :promotion_actions
resources :promotion_codes, only: [:index, :new, :create]
resources :promotion_code_batches, only: [:index, :new, :create] do
get "/download", to: "promotion_code_batches#download", defaults: {format: "csv"}
resources :promotions do
resources :promotion_rules
resources :promotion_actions
resources :promotion_codes, only: [:index, :new, :create]
resources :promotion_code_batches, only: [:index, :new, :create] do
get "/download", to: "promotion_code_batches#download", defaults: {format: "csv"}
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
}
expect(response).to be_redirect
expect(response).to redirect_to spree.edit_admin_promotion_path(promotion)
expect(response).to redirect_to solidus_friendly_promotions.edit_admin_promotion_path(promotion)
expect(promotion.actions.count).to eq(1)
end

Expand All @@ -24,7 +24,7 @@
promotion_action: {type: "Spree::InvalidType"}
}
expect(response).to be_redirect
expect(response).to redirect_to spree.edit_admin_promotion_path(promotion)
expect(response).to redirect_to solidus_friendly_promotions.edit_admin_promotion_path(promotion)
expect(promotion.actions.count).to eq(0)
end
end

0 comments on commit f99bcab

Please sign in to comment.