Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Admin: re-do promotion rules, nicer UI, better flow
Damian Legawiece8bfb60cd0d1
diff --git a/admin/app/controllers/spree/admin/promotion_rules_controller.rb b/admin/app/controllers/spree/admin/promotion_rules_controller.rbnew file mode 100644index 0000000000..88b2bba4d6--- /dev/null+++ b/admin/app/controllers/spree/admin/promotion_rules_controller.rb@@ -0,0 +1,39 @@+module Spree+ module Admin+ class PromotionActionsController < ResourceController+ belongs_to 'spree/promotion', find_by: :id++ helper_method :allowed_rule_types++ private++ def model_class+ @model_class = if params.dig(:promotion_rule, :type).present?+ if allowed_rule_types.map(&:to_s).include?(params.dig(:promotion_rule, :type))+ params.dig(:promotion_rule, :type).safe_constantize+ else+ raise 'Unknown promotion rule type'+ end+ else+ Spree::PromotionAction+ end+ end++ def build_resource+ model_class.new(promotion: parent)+ end++ def allowed_rule_types+ Rails.application.config.spree.promotions.rules+ end++ def location_after_save+ collection_url+ end++ def collection_url+ spree.admin_promotion_path(parent)+ end+ end+ end+enddiff --git a/admin/app/views/spree/admin/promotion_actions/_action.html.erb b/admin/app/views/spree/admin/promotion_actions/_action.html.erbdeleted file mode 100644index 1c86ffa5d3..0000000000--- a/admin/app/views/spree/admin/promotion_actions/_action.html.erb+++ /dev/null@@ -1,19 +0,0 @@-<li class="list-group-item p-0" id="<%= spree_dom_id(promotion_action) %>">- <%= turbo_frame_tag promotion_action do %>- <div class="d-flex justify-content-between align-items-center p-3">- <div>- <h6><%= promotion_action.human_name %></h6>- <p class="text-muted mb-0"><%= promotion_action.human_description %></p>- </div>- <div class="dropdown">- <button class="btn btn-light btn-sm" type="button" data-toggle="dropdown" aria-expanded="false">- <%= icon('dots', class: 'mr-0') %>- </button>- <div class="dropdown-menu">- <%= link_to_with_icon 'pencil', Spree.t('actions.edit'), spree.edit_admin_promotion_action_path(promotion_action.promotion, promotion_action), class: 'dropdown-item' %>- <%= link_to_with_icon 'trash', Spree.t('actions.destroy'), spree.admin_promotion_action_path(promotion_action.promotion, promotion_action), class: 'dropdown-item text-danger', data: { turbo_method: :delete, turbo_confirm: Spree.t(:are_you_sure), turbo_frame: '_top' } %>- </div>- </div>- </div>- <% end %>-</li>diff --git a/admin/app/views/spree/admin/promotion_actions/_promotion_action.html.erb b/admin/app/views/spree/admin/promotion_actions/_promotion_action.html.erbnew file mode 100644index 0000000000..40f7b7e60c--- /dev/null+++ b/admin/app/views/spree/admin/promotion_actions/_promotion_action.html.erb@@ -0,0 +1,31 @@+<%= turbo_frame_tag promotion_action do %>+ <div class="card mb-3" id="<%= spree_dom_id(promotion_action) %>">+ <div class="card-header d-flex justify-content-between align-items-center">+ <h5 class="card-title"><%= promotion_action.human_name %></h5>++ <div class="dropdown">+ <button class="btn btn-light btn-sm" type="button" data-toggle="dropdown" aria-expanded="false">+ <%= icon('dots', class: 'mr-0') %>+ </button>+ <div class="dropdown-menu">+ <%= link_to_with_icon 'pencil', Spree.t('actions.edit'), spree.edit_admin_promotion_action_path(promotion_action.promotion, promotion_action), class: 'dropdown-item' %>+ <%= link_to_with_icon 'trash', Spree.t('actions.destroy'), spree.admin_promotion_action_path(promotion_action.promotion, promotion_action), class: 'dropdown-item text-danger', data: { turbo_method: :delete, turbo_confirm: Spree.t(:are_you_sure), turbo_frame: '_top' } %>+ </div>+ </div>+ </div>+ <div class="card-body">+ <p class="text-muted"><%= promotion_action.human_description %></p>++ <% if promotion_action.respond_to?(:calculator) && promotion_action.calculator.preferences.present? %>+ <h6><%= Spree.t(:calculator) %>: <%= promotion_action.calculator.description %></h6>+ <p class="mb-0">+ <% promotion_action.calculator.preferences.each do |key, value| %>+ <% next if value.blank? %>+ <span><%= Spree.t(key) %></span>+ <code><%= value %></code>+ <% end %>+ </p>+ <% end %>+ </div>+ </div>+<% end %>diff --git a/admin/app/views/spree/admin/promotion_actions/edit.html.erb b/admin/app/views/spree/admin/promotion_actions/edit.html.erbindex 4cbc3907d8..5291fba60e 100644--- a/admin/app/views/spree/admin/promotion_actions/edit.html.erb+++ b/admin/app/views/spree/admin/promotion_actions/edit.html.erb@@ -1,12 +1,16 @@<%= turbo_frame_tag @promotion_action do %>- <h6 class="border-bottom p-3"><%= @promotion_action.human_name %></h6><%= form_with model: @promotion_action, url: spree.admin_promotion_action_path(@promotion, @promotion_action), method: :put, scope: :promotion_action do |f| %>- <div class="p-3">- <%= render "spree/admin/promotion_actions/forms/#{@promotion_action.key}", f: f %>- </div>- <div class="form-actions d-flex justify-content-between p-3">- <%= turbo_save_button_tag Spree.t('actions.update') %>- <%= link_to Spree.t('actions.discard'), spree.admin_promotion_path(@promotion), class: 'btn btn-light' %>+ <div class="card mb-3" id="<%= spree_dom_id(@promotion_action) %>">+ <div class="card-header d-flex justify-content-between align-items-center">+ <h5 class="card-title"><%= @promotion_action.human_name %></h5>+ </div>+ <div class="card-body pb-0">+ <%= render "spree/admin/promotion_actions/forms/#{@promotion_action.key}", f: f %>+ </div>+ <div class="card-footer d-flex justify-content-between">+ <%= turbo_save_button_tag Spree.t('actions.update') %>+ <%= link_to Spree.t('actions.discard'), spree.admin_promotion_path(@promotion), class: 'btn btn-light' %>+ </div></div><% end %><% end %>diff --git a/admin/app/views/spree/admin/promotion_actions/new.html.erb b/admin/app/views/spree/admin/promotion_actions/new.html.erbindex 4b7950cd49..44f3eae342 100644--- a/admin/app/views/spree/admin/promotion_actions/new.html.erb+++ b/admin/app/views/spree/admin/promotion_actions/new.html.erb@@ -37,8 +37,8 @@</div><% if @promotion_action.type.present? %><div class="modal-footer d-flex justify-content-between">+ <button type="button" class="btn btn-light" data-dismiss="modal"><%= Spree.t('actions.discard') %></button><%= turbo_save_button_tag Spree.t('actions.create'), data: { turbo_frame: '_top' } %>- <%= link_to Spree.t('actions.discard'), spree.admin_promotion_path(@promotion), class: 'btn btn-light' %></div><% end %></div>diff --git a/admin/app/views/spree/admin/promotion_rules/_promotion_rule.html.erb b/admin/app/views/spree/admin/promotion_rules/_promotion_rule.html.erbnew file mode 100644index 0000000000..1a5eab2cdd--- /dev/null+++ b/admin/app/views/spree/admin/promotion_rules/_promotion_rule.html.erb@@ -0,0 +1,30 @@+<%= turbo_frame_tag promotion_rule do %>+ <div class="card mb-3" id="<%= spree_dom_id(promotion_rule) %>">+ <div class="card-header d-flex justify-content-between align-items-center">+ <h5 class="card-title"><%= promotion_rule.human_name %></h5>++ <div class="dropdown">+ <button class="btn btn-light btn-sm" type="button" data-toggle="dropdown" aria-expanded="false">+ <%= icon('dots', class: 'mr-0') %>+ </button>+ <div class="dropdown-menu">+ <%= link_to_with_icon 'pencil', Spree.t('actions.edit'), spree.edit_admin_promotion_rule_path(promotion_rule.promotion, promotion_rule), class: 'dropdown-item' %>+ <%= link_to_with_icon 'trash', Spree.t('actions.destroy'), spree.admin_promotion_rule_path(promotion_rule.promotion, promotion_rule), class: 'dropdown-item text-danger', data: { turbo_method: :delete, turbo_confirm: Spree.t(:are_you_sure), turbo_frame: '_top' } %>+ </div>+ </div>+ </div>+ <div class="card-body">+ <p class="text-muted"><%= promotion_rule.human_description %></p>++ <% if promotion_rule.preferences.present? %>+ <p class="mb-0">+ <% promotion_rule.preferences.each do |key, value| %>+ <% next if value.blank? %>+ <span><%= Spree.t(key) %></span>+ <code><%= value %></code>+ <% end %>+ </dl>+ <% end %>+ </div>+ </div>+<% end %>diff --git a/admin/app/views/spree/admin/promotions/_actions.html.erb b/admin/app/views/spree/admin/promotions/_actions.html.erbindex ee241734ad..dc08bf9b72 100644--- a/admin/app/views/spree/admin/promotions/_actions.html.erb+++ b/admin/app/views/spree/admin/promotions/_actions.html.erb@@ -3,18 +3,18 @@<h5 class="card-title"><%= Spree.t(:promotion_actions) %></h5>-<span data-toggle="modal" data-target="#modal"><%= link_to_with_icon 'plus', Spree.t(:add_one), spree.new_admin_promotion_action_path(@promotion), class: 'btn btn-light btn-sm', data: { turbo_frame: 'dialog_modal' } %></span></div><div class="card-body"><% if @promotion.promotion_actions.empty? %>- <%= render 'spree/admin/shared/no_resource_found', resource_name: Spree.t(:promotion_actions), model_class: Spree::PromotionAction, new_object_url: spree.new_admin_promotion_action_path(@promotion) %>+ <div class="text-muted p-5 d-flex align-items-center w-100 justify-content-center">+ <%= Spree.t(:no_resource_found, resource: Spree.t(:promotion_actions)) %>+ <%= link_to_with_icon 'plus', Spree.t(:add_one), spree.new_admin_promotion_action_path(@promotion), class: 'btn btn-light btn-sm', data: { turbo_frame: 'dialog_modal' } %>+ </div><% else %>- <ul class="list-group">- <%= render collection: @promotion.promotion_actions, partial: 'spree/admin/promotion_actions/action', as: :promotion_action %>- </ul>+ <%= render collection: @promotion.promotion_actions, partial: 'spree/admin/promotion_actions/promotion_action', as: :promotion_action %><% end %></div></div>\ No newline at end of filediff --git a/admin/app/views/spree/admin/promotions/_rule_form.html.erb b/admin/app/views/spree/admin/promotions/_rule_form.html.erbdeleted file mode 100644index 84c24f8d7e..0000000000--- a/admin/app/views/spree/admin/promotions/_rule_form.html.erb+++ /dev/null@@ -1,35 +0,0 @@-<div class="bg-gray-25 border rounded pt-3 px-3 pb-0 nested-form-wrapper border-bottom mb-3 position-relative" data-controller="rule-form" data-new-record="<%= rf.object.new_record? %>">- <% if rf.object.new_record? %>- <% @promotion_rules.each do |promotion_rule| %>- <template id="rule-form-template-<%= promotion_rule.to_s.demodulize %>" data-rule-form-target="template">- <p class="text-muted form-text">- <%= Spree.t("promotion_rule_types.#{promotion_rule.to_s.demodulize.underscore}.description") %>- </p>- <%= render "spree/admin/promotions/rules/#{promotion_rule.to_s.demodulize.underscore}", rule_form: rf, prefix: prefix %>- </template>- <% end %>-- <div class="form-group">- <%= rf.label :type, Spree.t(:rule_type) %>- <%= rf.select :type, @rule_types, {}, { class: 'custom-select', data: { action: 'rule-form#replace' } } %>- </div>- <% else %>- <h6 class="mb-3"><%= Spree.t("admin.promotion_rules.#{rf.object.type.demodulize.underscore}") %></h6>-- <%= rf.hidden_field :type, value: rf.object.type %>- <% end %>- <div class="form-group" data-rule-form-target="value">- <% if rf.object.type.present? %>- <p class="text-muted form-text">- <%= Spree.t("promotion_rule_types.#{rf.object.type.demodulize.underscore}.description") %>- </p>- <%= render "spree/admin/promotions/rules/#{rf.object.type.demodulize.underscore}", rule_form: rf, prefix: prefix %>- <% end %>- </div>- <div class="position-absolute right-0 top-0">- <button type="button" data-action="nested-form#remove" class="btn btn-sm btn-danger with-tip mt-1 mr-1 rounded-sm px-1" title="<%= Spree.t(:remove_condition) %>">- <%= icon('trash', class: 'mr-0') %>- </button>- </div>- <%= rf.hidden_field :_destroy if rf.object.persisted? %>-</div>diff --git a/admin/app/views/spree/admin/promotions/_rules.html.erb b/admin/app/views/spree/admin/promotions/_rules.html.erbnew file mode 100644index 0000000000..53df86a4bb--- /dev/null+++ b/admin/app/views/spree/admin/promotions/_rules.html.erb@@ -0,0 +1,34 @@+<div class="card mb-4">+ <div class="card-header d-flex justify-content-between">+ <h5 class="card-title">+ <%= Spree.t(:rules) %>+ </h5>+ <span data-toggle="modal" data-target="#modal">+ <%= link_to_with_icon 'plus', Spree.t(:add_one), spree.new_admin_promotion_rule_path(@promotion), class: 'btn btn-light btn-sm' %>+ </span>+ </div>+ <div class="card-body">+ <div class="form-group mb-0">+ <div class="d-block d-lg-flex gap-3">+ <%= form_with model: @promotion, url: spree.admin_promotion_path(@promotion), method: :put, data: { controller: 'auto-submit' } do |f| %>+ <% Spree::Promotion::MATCH_POLICIES.each do |policy| %>+ <div class="custom-control custom-radio custom-control-inline">+ <%= f.radio_button :match_policy, policy, class: 'custom-control-input', data: { action: 'auto-submit#submit' } %>+ <%= f.label "match_policy_#{policy}", class: 'custom-control-label' %>+ </div>+ <% end %>+ <% end %>+ </div>+ </div>+ </div>+ <div class="card-body pb-0">+ <% if @promotion.promotion_rules.empty? %>+ <div class="text-muted p-5 d-flex align-items-center w-100 justify-content-center">+ <%= Spree.t(:no_resource_found, resource: Spree.t(:promotion_rules)) %>+ <%= link_to_with_icon 'plus', Spree.t(:add_one), spree.new_admin_promotion_rule_path(@promotion), class: 'btn btn-light btn-sm', data: { turbo_frame: 'dialog_modal' } %>+ </div>+ <% else %>+ <%= render collection: @promotion.promotion_rules, partial: 'spree/admin/promotion_rules/promotion_rule', as: :promotion_rule %>+ <% end %>+ </div>+</div>diff --git a/admin/app/views/spree/admin/promotions/_rules_form.html.erb b/admin/app/views/spree/admin/promotions/_rules_form.html.erbdeleted file mode 100644index adc570764a..0000000000--- a/admin/app/views/spree/admin/promotions/_rules_form.html.erb+++ /dev/null@@ -1,39 +0,0 @@-<div class="card mb-4">- <div class="card-header d-flex justify-content-between">- <h5 class="card-title">- <%= Spree.t(:rules) %>- </h5>- </div>- <div class="card-body">- <div class="form-group mb-4">- <div class="d-block d-lg-flex gap-3">- <% Spree::Promotion::MATCH_POLICIES.each do |policy| %>- <div class="custom-control custom-radio custom-control-inline">- <%= f.radio_button :match_policy, policy, class: 'custom-control-input' %>- <%= f.label "match_policy_#{policy}", class: 'custom-control-label' %>- </div>- <% end %>- </div>- </div>-- <div data-controller="nested-form">- <template data-nested-form-target="template">- <%= f.fields_for :promotion_rules, Spree::PromotionRule.new(type: @promotion_rules.first.to_s), child_index: 'NEW_RECORD' do |rf, index| %>- <%= render 'spree/admin/promotions/rule_form', rf: rf, prefix: "promotion[promotion_rules_attributes][#{rf.index}]" %>- <% end %>- </template>-- <%= f.fields_for :promotion_rules, @promotion.promotion_rules do |rf, index| %>- <%= render 'spree/admin/promotions/rule_form', rf: rf, prefix: "promotion[promotion_rules_attributes][#{rf.index}]" %>- <% end %>-- <!-- Inserted elements will be injected before that target. -->- <div data-nested-form-target="target" class="mb-2"></div>-- <button type="button" class="btn btn-link w-100 py-3" data-action="nested-form#add">- <%= icon('plus', class: 'mr-2') %>- <%= Spree.t(:add_one)%>- </button>- </div>- </div>-</div>\ No newline at end of filediff --git a/admin/app/views/spree/admin/promotions/calculators/_default_fields.html.erb b/admin/app/views/spree/admin/promotions/calculators/_default_fields.html.erbdeleted file mode 100644index 736b2d45dc..0000000000--- a/admin/app/views/spree/admin/promotions/calculators/_default_fields.html.erb+++ /dev/null@@ -1,11 +0,0 @@-<% calculator.preferences.keys.map do |key| %>- <div class="form-group">- <% field_name = "#{prefix}[calculator_attributes][preferred_#{key}]" %>- <%= label_tag field_name, Spree.t(key.to_s) %>- <%= preference_field_tag(- field_name,- calculator.get_preference(key),- type: calculator.preference_type(key),- key: key) %>- </div>-<% end %>diff --git a/admin/app/views/spree/admin/promotions/calculators/tiered_flat_rate/_fields.html.erb b/admin/app/views/spree/admin/promotions/calculators/tiered_flat_rate/_fields.html.erbdeleted file mode 100644index ea44c29b67..0000000000--- a/admin/app/views/spree/admin/promotions/calculators/tiered_flat_rate/_fields.html.erb+++ /dev/null@@ -1,46 +0,0 @@-<div class="form-group">- <%= label_tag "#{prefix}[calculator_attributes][preferred_base_amount]",- Spree.t(:base_amount) %>- <%= preference_field_tag(- "#{prefix}[calculator_attributes][preferred_base_amount]",- calculator.preferred_base_amount,- type: calculator.preference_type(:base_amount)) %>-</div>--<div class="form-group mb-0">- <%= label_tag nil, Spree.t(:tiers) %>- <%= content_tag :div, nil, class: "hidden js-original-tiers",- data: { :'original-tiers' => Hash[calculator.preferred_tiers.sort] } %>-</div>--<div class="js-tiers"></div>-<button class="btn btn-primary js-add-tier"><%= Spree.t(:add) %></button>--<script type="text/x-handlebars-template" id="tier-input-name">- <%= prefix %>[calculator_attributes][preferred_tiers][{{base}}]-</script>--<script type="text/x-handlebars-template" id="tier-fields-template">- <div class="tier card mb-4">- <div class="card-body p-3">- <div class="form-group">- <div class="input-group">- <div class="input-group-prepend">- <div class="input-group-text">$</div>- </div>- <input class="js-base-input form-control" type="text" value={{baseField.value}}>- </div>- </div>- <div class="form-group">- <div class="input-group">- <div class="input-group-prepend">- <div class="input-group-text">$</div>- </div>- <input class="js-value-input form-control"- name="{{valueField.name}}" type="text" value={{valueField.value}}>- </div>- </div>- <a class="js-remove-tier btn btn-light btn-sm"><span class="icon icon-delete"></span></a>- </div>- </div>-</script>diff --git a/admin/app/views/spree/admin/promotions/calculators/tiered_percent/_fields.html.erb b/admin/app/views/spree/admin/promotions/calculators/tiered_percent/_fields.html.erbdeleted file mode 100644index 184b17153b..0000000000--- a/admin/app/views/spree/admin/promotions/calculators/tiered_percent/_fields.html.erb+++ /dev/null@@ -1,46 +0,0 @@-<div class="form-group">- <%= label_tag "#{prefix}[calculator_attributes][preferred_base_percent]",- Spree.t(:base_percent) %>- <%= preference_field_tag(- "#{prefix}[calculator_attributes][preferred_base_percent]",- calculator.preferred_base_percent,- type: calculator.preference_type(:base_percent)) %>-</div>--<div class="form-group mb-0">- <%= label_tag nil, Spree.t(:tiers) %>- <%= content_tag :div, nil, class: "hidden js-original-tiers",- data: { :'original-tiers' => Hash[calculator.preferred_tiers.sort] } %>-</div>--<div class="js-tiers"></div>-<button class="btn btn-primary js-add-tier"><%= Spree.t(:add) %></button>--<script type="text/x-handlebars-template" id="tier-input-name">- <%= prefix %>[calculator_attributes][preferred_tiers][{{base}}]-</script>--<script type="text/x-handlebars-template" id="tier-fields-template">- <div class="tier card mb-4">- <div class="card-body p-3">- <div class="form-group">- <div class="input-group">- <div class="input-group-prepend">- <div class="input-group-text">$</div>- </div>- <input class="js-base-input form-control" type="text" value={{baseField.value}}>- </div>- </div>- <div class="form-group">- <div class="input-group">- <div class="input-group-prepend">- <div class="input-group-text">%</div>- </div>- <input class="js-value-input form-control"- name="{{valueField.name}}" type="text" value={{valueField.value}}>- </div>- </div>- <a class="js-remove-tier btn btn-light btn-sm"><span class="icon icon-delete"></span></a>- </div>- </div>-</script>diff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rbindex a0f8bdd9ff..7975976da5 100644--- a/core/app/models/spree/promotion.rb+++ b/core/app/models/spree/promotion.rb@@ -37,7 +37,7 @@ module Spreehas_many :orders, through: :order_promotions, class_name: 'Spree::Order'has_many :store_promotions, class_name: 'Spree::StorePromotion'has_many :stores, class_name: 'Spree::Store', through: :store_promotions- accepts_nested_attributes_for :promotion_actions, :promotion_rules, allow_destroy: true+ accepts_nested_attributes_for :promotion_actions, :promotion_rules## Callbacksdiff --git a/core/app/models/spree/promotion_action.rb b/core/app/models/spree/promotion_action.rbindex 4ffd70737c..f80842c0a6 100644--- a/core/app/models/spree/promotion_action.rb+++ b/core/app/models/spree/promotion_action.rb@@ -25,14 +25,23 @@ module Spreetype == 'Spree::Promotion::Actions::FreeShipping'end+ # Returns the human name of the promotion action+ #+ # @return [String] eg. Free Shippingdef human_nameSpree.t("promotion_action_types.#{key}.name")end+ # Returns the human description of the promotion action+ #+ # @return [String]def human_descriptionSpree.t("promotion_action_types.#{key}.description")end+ # Returns the key of the promotion action+ #+ # @return [String] eg. free_shippingdef keytype.demodulize.underscoreenddiff --git a/core/app/models/spree/promotion_rule.rb b/core/app/models/spree/promotion_rule.rbindex 1fd1931069..d9c1878a38 100644--- a/core/app/models/spree/promotion_rule.rb+++ b/core/app/models/spree/promotion_rule.rb@@ -32,6 +32,27 @@ module Spree@eligibility_errors ||= ActiveModel::Errors.new(self)end+ # Returns the human name of the promotion rule+ #+ # @return [String] eg. Currency+ def human_name+ Spree.t("promotion_rule_types.#{key}.name")+ end++ # Returns the human description of the promotion rule+ #+ # @return [String]+ def human_description+ Spree.t("promotion_rule_types.#{key}.description")+ end++ # Returns the key of the promotion rule+ #+ # @return [String] eg. currency+ def key+ type.demodulize.underscore+ end+privatedef unique_per_promotion