mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-03 06:59:14 +00:00
First cut at an interface for updating tag rules
This commit is contained in:
@@ -17,8 +17,9 @@ angular.module("admin.enterprises")
|
||||
{ name: t('shipping_methods'), icon_class: "icon-truck", show: "showShippingMethods()" }
|
||||
{ name: t('payment_methods'), icon_class: "icon-money", show: "showPaymentMethods()" }
|
||||
{ name: t('enterprise_fees'), icon_class: "icon-tasks", show: "showEnterpriseFees()" }
|
||||
{ name: t('inventory_settings'), icon_class: "icon-list-ol", show: "showInventorySettings()" }
|
||||
{ name: t('shop_preferences'), icon_class: "icon-shopping-cart", show: "showShopPreferences()" }
|
||||
{ name: t('inventory_settings'), icon_class: "icon-list-ol", show: "enterpriseIsShop()" }
|
||||
{ name: t('tag_rules'), icon_class: "icon-random", show: "enterpriseIsShop()" }
|
||||
{ name: t('shop_preferences'), icon_class: "icon-shopping-cart", show: "enterpriseIsShop()" }
|
||||
]
|
||||
|
||||
$scope.select(0)
|
||||
@@ -42,8 +43,5 @@ angular.module("admin.enterprises")
|
||||
$scope.showEnterpriseFees = ->
|
||||
enterprisePermissions.can_manage_enterprise_fees && ($scope.Enterprise.sells != "none" || $scope.Enterprise.is_primary_producer)
|
||||
|
||||
$scope.showInventorySettings = ->
|
||||
$scope.Enterprise.sells != "none"
|
||||
|
||||
$scope.showShopPreferences = ->
|
||||
$scope.enterpriseIsShop = ->
|
||||
$scope.Enterprise.sells != "none"
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
angular.module("admin.enterprises").controller "TagRulesCtrl", ($scope) ->
|
||||
$scope.groupedTagRules = $scope.Enterprise.tag_rules.reduce (groupedTagRules, rule) ->
|
||||
key = rule.preferred_customer_tags
|
||||
groupedTagRules[key] ||= []
|
||||
groupedTagRules[key].push rule
|
||||
groupedTagRules
|
||||
, {}
|
||||
@@ -0,0 +1,4 @@
|
||||
angular.module("admin.enterprises").directive "discountOrder", ->
|
||||
restrict: "E"
|
||||
replace: true
|
||||
templateUrl: "admin/tag_rules/discount_order.html"
|
||||
@@ -0,0 +1,26 @@
|
||||
%div
|
||||
%input{ type: "hidden",
|
||||
id: "enterprise_tag_rules_attributes_{{rule.id}}_id",
|
||||
name: "enterprise[tag_rules_attributes][{{rule.id}}][id]",
|
||||
ng: { value: "rule.id" } }
|
||||
|
||||
%input{ type: "hidden",
|
||||
id: "enterprise_tag_rules_attributes_{{rule.id}}_calculator_attributes_id",
|
||||
name: "enterprise[tag_rules_attributes][{{rule.id}}][calculator_attributes][id]",
|
||||
ng: { value: "rule.calculator.id" } }
|
||||
|
||||
%input{ type: "hidden",
|
||||
id: "enterprise_tag_rules_attributes_{{rule.id}}_calculator_attributes_type",
|
||||
name: "enterprise[tag_rules_attributes][{{rule.id}}][calculator_attributes][type]",
|
||||
value: "TagRule::FlatPercentItemTotal" }
|
||||
|
||||
%span.text-big Apply a discount of
|
||||
%span.input-symbol.after
|
||||
%span.text-big %
|
||||
%input.text-big{ type: "number",
|
||||
id: "enterprise_tag_rules_attributes_{{rule.id}}_calculator_attributes_preferred_flat_percent",
|
||||
name: "enterprise[tag_rules_attributes][{{rule.id}}][calculator_attributes][preferred_flat_percent]",
|
||||
min: 0,
|
||||
max: 100,
|
||||
ng: { model: "rule.calculator.preferred_flat_percent" } }
|
||||
%span.text-big to order subtotals
|
||||
@@ -69,6 +69,21 @@ div#group_buy_calculation {
|
||||
text-indent:1em;
|
||||
}
|
||||
}
|
||||
|
||||
&.after {
|
||||
|
||||
span {
|
||||
position: absolute;
|
||||
transform: translate(0,-55%);
|
||||
top:50%;
|
||||
right: 0.5em;
|
||||
pointer-events:none;
|
||||
}
|
||||
|
||||
input {
|
||||
padding-right: 1.2em;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
th.actions {
|
||||
|
||||
13
app/assets/stylesheets/admin/tag_rules.css.scss
Normal file
13
app/assets/stylesheets/admin/tag_rules.css.scss
Normal file
@@ -0,0 +1,13 @@
|
||||
.customer_tag {
|
||||
border: 1px solid #cee1f4;
|
||||
|
||||
.header {
|
||||
padding: 8px 10px;
|
||||
background-color: #eff5fc;
|
||||
border-bottom: 1px solid #cee1f4;
|
||||
}
|
||||
|
||||
.tag_rule {
|
||||
padding: 8px 10px;
|
||||
}
|
||||
}
|
||||
@@ -7,3 +7,12 @@
|
||||
font-size: 1.2rem;
|
||||
font-weight: 300;
|
||||
}
|
||||
|
||||
.text-red {
|
||||
color: #DA5354;
|
||||
}
|
||||
|
||||
|
||||
input.text-big {
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ module Admin
|
||||
class EnterprisesController < ResourceController
|
||||
before_filter :load_enterprise_set, :only => :index
|
||||
before_filter :load_countries, :except => [:index, :register, :check_permalink]
|
||||
before_filter :load_methods_and_fees, :only => [:new, :edit, :update, :create]
|
||||
before_filter :load_methods_and_fees, :only => [:edit, :update]
|
||||
before_filter :load_groups, :only => [:new, :edit, :update, :create]
|
||||
before_filter :load_taxons, :only => [:new, :edit, :update, :create]
|
||||
before_filter :check_can_change_sells, only: :update
|
||||
|
||||
@@ -48,6 +48,7 @@ class Enterprise < ActiveRecord::Base
|
||||
|
||||
accepts_nested_attributes_for :address
|
||||
accepts_nested_attributes_for :producer_properties, allow_destroy: true, reject_if: lambda { |pp| pp[:property_name].blank? }
|
||||
accepts_nested_attributes_for :tag_rules, allow_destroy: true
|
||||
|
||||
has_attached_file :logo,
|
||||
styles: { medium: "300x300>", small: "180x180>", thumb: "100x100>" },
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
class Api::Admin::Calculator::FlatPercentItemTotalSerializer < ActiveModel::Serializer
|
||||
attributes :id, :preferred_flat_percent
|
||||
|
||||
def preferred_flat_percent
|
||||
object.preferred_flat_percent.to_i
|
||||
end
|
||||
end
|
||||
@@ -7,4 +7,5 @@ class Api::Admin::EnterpriseSerializer < ActiveModel::Serializer
|
||||
|
||||
has_one :owner, serializer: Api::Admin::UserSerializer
|
||||
has_many :users, serializer: Api::Admin::UserSerializer
|
||||
has_many :tag_rules, serializer: Api::Admin::TagRuleSerializer
|
||||
end
|
||||
|
||||
19
app/serializers/api/admin/tag_rule_serializer.rb
Normal file
19
app/serializers/api/admin/tag_rule_serializer.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
class Api::Admin::TagRuleSerializer < ActiveModel::Serializer
|
||||
def serializable_hash
|
||||
rule_specific_serializer.serializable_hash
|
||||
end
|
||||
|
||||
def rule_specific_serializer
|
||||
"Api::Admin::#{object.class.to_s}Serializer".constantize.new(object)
|
||||
end
|
||||
end
|
||||
|
||||
module Api::Admin::TagRule
|
||||
class BaseSerializer < ActiveModel::Serializer
|
||||
attributes :id, :enterprise_id, :type, :preferred_customer_tags
|
||||
end
|
||||
|
||||
class DiscountOrderSerializer < BaseSerializer
|
||||
has_one :calculator, serializer: Api::Admin::Calculator::FlatPercentItemTotalSerializer
|
||||
end
|
||||
end
|
||||
@@ -54,3 +54,7 @@
|
||||
%fieldset.alpha.no-border-bottom{ ng: { show: "menu.selected.name=='Shop Preferences'" } }
|
||||
%legend Shop Preferences
|
||||
= render 'admin/enterprises/form/shop_preferences', f: f
|
||||
|
||||
%fieldset.alpha.no-border-bottom{ ng: { show: "menu.selected.name=='Tag Rules'" } }
|
||||
%legend Tag Rules
|
||||
= render 'admin/enterprises/form/tag_rules', f: f
|
||||
|
||||
10
app/views/admin/enterprises/form/_tag_rules.html.haml
Normal file
10
app/views/admin/enterprises/form/_tag_rules.html.haml
Normal file
@@ -0,0 +1,10 @@
|
||||
.row{ ng: { controller: "TagRulesCtrl" } }
|
||||
.eleven.columns.alpha.omega
|
||||
.eleven.columns.alpha.omega
|
||||
.customer_tag{ ng: { repeat: "(tags, rules) in groupedTagRules" }, bindonce: true }
|
||||
.header
|
||||
%h3
|
||||
For customers tagged
|
||||
%span.text-red #{{ tags.split(",").join(", #") }}
|
||||
.tag_rule{ ng: { repeat: "rule in rules" } }
|
||||
%discount-order{ bo: { if: "rule.type == 'TagRule::DiscountOrder'" } }
|
||||
@@ -1001,6 +1001,7 @@ Please follow the instructions there to make your enterprise visible on the Open
|
||||
payment_methods: "Payment Methods"
|
||||
enterprise_fees: "Enterprise Fees"
|
||||
inventory_settings: "Inventory Settings"
|
||||
tag_rules: "Tag Rules"
|
||||
shop_preferences: "Shop Preferences"
|
||||
validation_msg_relationship_already_established: "^That relationship is already established."
|
||||
validation_msg_at_least_one_hub: "^At least one hub must be selected"
|
||||
|
||||
@@ -282,6 +282,31 @@ feature %q{
|
||||
end
|
||||
end
|
||||
|
||||
describe "tag rules", js: true do
|
||||
let!(:enterprise) { create(:distributor_enterprise) }
|
||||
|
||||
context "updating" do
|
||||
let!(:tag_rule) { create(:tag_rule, enterprise: enterprise, preferred_customer_tags: "member" ) }
|
||||
|
||||
before do
|
||||
login_to_admin_section
|
||||
visit main_app.edit_admin_enterprise_path(enterprise)
|
||||
end
|
||||
|
||||
it "saves changes to the rule" do
|
||||
click_link "Tag Rules"
|
||||
|
||||
expect(first('.customer_tag .header')).to have_content "For customers tagged #member"
|
||||
expect(page).to have_input "enterprise[tag_rules_attributes][#{tag_rule.id}][calculator_attributes][preferred_flat_percent]", with: "0"
|
||||
fill_in "enterprise[tag_rules_attributes][#{tag_rule.id}][calculator_attributes][preferred_flat_percent]", with: 45
|
||||
|
||||
click_button 'Update'
|
||||
|
||||
expect(tag_rule.calculator.preferred_flat_percent).to eq 45
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
context "as an Enterprise user", js: true do
|
||||
let(:supplier1) { create(:supplier_enterprise, name: 'First Supplier') }
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
describe "TagRulesCtrl", ->
|
||||
ctrl = null
|
||||
scope = null
|
||||
enterprise = null
|
||||
|
||||
beforeEach ->
|
||||
module('admin.enterprises')
|
||||
enterprise =
|
||||
tag_rules: [
|
||||
{ id: 1, preferred_customer_tags: "member" },
|
||||
{ id: 2, preferred_customer_tags: "member" },
|
||||
{ id: 3, preferred_customer_tags: "local" }
|
||||
]
|
||||
|
||||
inject ($rootScope, $controller) ->
|
||||
scope = $rootScope
|
||||
scope.Enterprise = enterprise
|
||||
ctrl = $controller 'TagRulesCtrl', {$scope: scope}
|
||||
|
||||
describe "initialization", ->
|
||||
it "groups rules by preferred_customer_tags", ->
|
||||
expect(scope.groupedTagRules).toEqual {
|
||||
member: [{ id: 1, preferred_customer_tags: "member" }, { id: 2, preferred_customer_tags: "member" }],
|
||||
local: [{ id: 3, preferred_customer_tags: "local" }]
|
||||
}
|
||||
Reference in New Issue
Block a user