diff --git a/app/assets/javascripts/admin/tag_rules/directives/new_rule_dialog.js.coffee b/app/assets/javascripts/admin/tag_rules/directives/new_rule_dialog.js.coffee index 29861f3eba..779404924f 100644 --- a/app/assets/javascripts/admin/tag_rules/directives/new_rule_dialog.js.coffee +++ b/app/assets/javascripts/admin/tag_rules/directives/new_rule_dialog.js.coffee @@ -1,4 +1,4 @@ -angular.module("admin.tagRules").directive 'newTagRuleDialog', ($rootScope, $compile, $templateCache, DialogDefaults) -> +angular.module("admin.tagRules").directive 'newTagRuleDialog', ($rootScope, $compile, $templateCache, DialogDefaults, ruleTypes) -> restrict: 'A' scope: tagGroup: '=' @@ -7,12 +7,7 @@ angular.module("admin.tagRules").directive 'newTagRuleDialog', ($rootScope, $com # Compile modal template template = $compile($templateCache.get('admin/new_tag_rule_dialog.html'))(scope) - scope.ruleTypes = [ - { id: "FilterProducts", name: t('js.tag_rules.show_hide_variants') } - { id: "FilterShippingMethods", name: t('js.tag_rules.show_hide_shipping') } - { id: "FilterPaymentMethods", name: t('js.tag_rules.show_hide_payment') } - { id: "FilterOrderCycles", name: t('js.tag_rules.show_hide_order_cycles') } - ] + scope.ruleTypes = ruleTypes scope.ruleType = scope.ruleTypes[0].id diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 716ab39bf8..e10e2c06b8 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -48,6 +48,9 @@ module Admin @object = Enterprise.where(permalink: params[:id]). includes(users: [:ship_address, :bill_address]).first @object.build_custom_tab if @object.custom_tab.nil? + + load_tag_rule_types + return unless params[:stimulus] @enterprise.is_primary_producer = params[:is_primary_producer] @@ -375,6 +378,19 @@ module Admin @properties = Spree::Property.pluck(:name) end + def load_tag_rule_types + # Load rule types + @tag_rule_types = [ + { id: "FilterShippingMethods", name: t('js.tag_rules.show_hide_shipping') }, + { id: "FilterPaymentMethods", name: t('js.tag_rules.show_hide_payment') }, + { id: "FilterOrderCycles", name: t('js.tag_rules.show_hide_order_cycles') } + ] + + return unless helpers.feature?(:inventory, @object) + + @tag_rule_types.prepend({ id: "FilterProducts", name: t('js.tag_rules.show_hide_variants') }) + end + def setup_property @enterprise.producer_properties.build end diff --git a/app/views/admin/enterprises/form/_tag_rules.html.haml b/app/views/admin/enterprises/form/_tag_rules.html.haml index 875d09a8b8..7e73f424e8 100644 --- a/app/views/admin/enterprises/form/_tag_rules.html.haml +++ b/app/views/admin/enterprises/form/_tag_rules.html.haml @@ -1,4 +1,6 @@ -.row{ "ng-controller": "TagRulesCtrl" } += render partial: "admin/json/injection_ams", locals: { ngModule: "admin.tagRules", name: "ruleTypes", json: @tag_rule_types.to_json } + +.row{ "ng-app" => "admin.tagRules", "ng-controller": "TagRulesCtrl" } .eleven.columns.alpha.omega %ofn-sortable{ axis: "y", handle: ".header", items: '.customer_tag', position: "tagGroup.position", "after-sort": "updateRuleCounts()" } .no_tags{ "ng-show": "tagGroups.length == 0" } diff --git a/spec/system/admin/tag_rules_spec.rb b/spec/system/admin/tag_rules_spec.rb index f369fcc94d..dfc9922b3e 100644 --- a/spec/system/admin/tag_rules_spec.rb +++ b/spec/system/admin/tag_rules_spec.rb @@ -32,25 +32,15 @@ RSpec.describe 'Tag Rules' do "shipping_methods_visibility" end - # New FilterProducts Rule - click_button '+ Add A New Rule' - select2_select 'Show or Hide variants in my shop', from: 'rule_type_selector' - click_button "Add Rule" - within(".customer_tag #tr_1") do - fill_in_tag "volunteers-only1" - select2_select "VISIBLE", - from: "enterprise_tag_rules_attributes_1_preferred_matched_" \ - "variants_visibility" - end - # New FilterPaymentMethods Rule click_button '+ Add A New Rule' select2_select 'Show or Hide payment methods at checkout', from: 'rule_type_selector' click_button "Add Rule" - within(".customer_tag #tr_2") do + + within(".customer_tag #tr_1") do fill_in_tag "volunteers-only2" select2_select "VISIBLE", - from: "enterprise_tag_rules_attributes_2_preferred_matched_" \ + from: "enterprise_tag_rules_attributes_1_preferred_matched_" \ "payment_methods_visibility" end @@ -58,10 +48,10 @@ RSpec.describe 'Tag Rules' do click_button '+ Add A New Rule' select2_select 'Show or Hide order cycles in my shopfront', from: 'rule_type_selector' click_button "Add Rule" - within(".customer_tag #tr_3") do + within(".customer_tag #tr_2") do fill_in_tag "volunteers-only3" select2_select "NOT VISIBLE", - from: "enterprise_tag_rules_attributes_3_preferred_matched_" \ + from: "enterprise_tag_rules_attributes_2_preferred_matched_" \ "order_cycles_visibility" end @@ -81,11 +71,6 @@ RSpec.describe 'Tag Rules' do expect(tag_rule.preferred_shipping_method_tags).to eq "volunteers-only" expect(tag_rule.preferred_matched_shipping_methods_visibility).to eq "hidden" - tag_rule = TagRule::FilterProducts.last - expect(tag_rule.preferred_customer_tags).to eq "volunteer" - expect(tag_rule.preferred_variant_tags).to eq "volunteers-only1" - expect(tag_rule.preferred_matched_variants_visibility).to eq "visible" - tag_rule = TagRule::FilterPaymentMethods.last expect(tag_rule.preferred_customer_tags).to eq "volunteer" expect(tag_rule.preferred_payment_method_tags).to eq "volunteers-only2" @@ -101,6 +86,34 @@ RSpec.describe 'Tag Rules' do expect(tag_rule.preferred_exchange_tags).to eq "wholesale" expect(tag_rule.preferred_matched_order_cycles_visibility).to eq "hidden" end + + context "when inventory enabled", feature: :inventory do + it "allows creation of filter variant type" do + # Creating a new tag + expect(page).to have_content 'No tags apply to this enterprise yet' + expect(page).not_to have_selector '.customer_tag' + click_button '+ Add A New Tag' + fill_in_tag "volunteer" + + # New FilterProducts Rule + click_button '+ Add A New Rule' + select2_select 'Show or Hide variants in my shop', from: 'rule_type_selector' + click_button "Add Rule" + within(".customer_tag #tr_0") do + fill_in_tag "volunteers-only1" + select2_select "VISIBLE", + from: "enterprise_tag_rules_attributes_0_preferred_matched_" \ + "variants_visibility" + end + + click_button 'Update' + + tag_rule = TagRule::FilterProducts.last + expect(tag_rule.preferred_customer_tags).to eq "volunteer" + expect(tag_rule.preferred_variant_tags).to eq "volunteers-only1" + expect(tag_rule.preferred_matched_variants_visibility).to eq "visible" + end + end end context "updating" do