mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-05 02:41:33 +00:00
Adding update logic to business model config controller
This commit is contained in:
@@ -1,3 +1,30 @@
|
||||
class Admin::BusinessModelConfigurationController < Spree::Admin::BaseController
|
||||
require 'open_food_network/business_model_configuration_validator'
|
||||
|
||||
class Admin::BusinessModelConfigurationController < Spree::Admin::BaseController
|
||||
before_filter :load_settings, only: [:edit, :update]
|
||||
before_filter :require_valid_settings, only: [:update]
|
||||
|
||||
def update
|
||||
Spree::Config.set(params[:settings])
|
||||
flash[:success] = t(:successfully_updated, :resource => t(:business_model_configuration))
|
||||
redirect_to_edit
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def redirect_to_edit
|
||||
redirect_to main_app.edit_admin_business_model_configuration_path
|
||||
end
|
||||
|
||||
def load_settings
|
||||
@settings = OpenFoodNetwork::BusinessModelConfigurationValidator.new(params[:settings] || {
|
||||
account_invoices_monthly_fixed: Spree::Config[:account_invoices_monthly_fixed],
|
||||
account_invoices_monthly_rate: Spree::Config[:account_invoices_monthly_rate],
|
||||
account_invoices_monthly_cap: Spree::Config[:account_invoices_monthly_cap]
|
||||
})
|
||||
end
|
||||
|
||||
def require_valid_settings
|
||||
render :edit unless @settings.valid?
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
# This class is a lightweight model used to validate preferences for business model configuration
|
||||
# when they are submitted to the BusinessModelConfigurationController
|
||||
|
||||
module OpenFoodNetwork
|
||||
class BusinessModelConfigurationValidator
|
||||
include ActiveModel::Validations
|
||||
|
||||
attr_accessor :account_invoices_monthly_fixed, :account_invoices_monthly_rate, :account_invoices_monthly_cap
|
||||
|
||||
validates :account_invoices_monthly_fixed, presence: true, numericality: { greater_than_or_equal_to: 0 }
|
||||
validates :account_invoices_monthly_rate, presence: true, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 1 }
|
||||
validates :account_invoices_monthly_cap, presence: true, numericality: { greater_than_or_equal_to: 0 }
|
||||
|
||||
def initialize(attr, button=nil)
|
||||
attr.each { |k,v| instance_variable_set("@#{k}", v) }
|
||||
@button = button
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,9 +1,17 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Admin::AccountsAndBillingSettingsController, type: :controller do
|
||||
describe Admin::BusinessModelConfigurationController, type: :controller do
|
||||
let(:user) { create(:user) }
|
||||
let(:admin) { create(:admin_user) }
|
||||
|
||||
before do
|
||||
Spree::Config.set({
|
||||
account_invoices_monthly_fixed: 5,
|
||||
account_invoices_monthly_rate: 0.02,
|
||||
account_invoices_monthly_cap: 50
|
||||
})
|
||||
end
|
||||
|
||||
describe "edit" do
|
||||
context "as an enterprise user" do
|
||||
before { allow(controller).to receive(:spree_current_user) { user } }
|
||||
@@ -23,4 +31,53 @@ describe Admin::AccountsAndBillingSettingsController, type: :controller do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "update" do
|
||||
context "as an enterprise user" do
|
||||
before { allow(controller).to receive(:spree_current_user) { user } }
|
||||
|
||||
it "does not allow access" do
|
||||
spree_get :update
|
||||
expect(response).to redirect_to spree.unauthorized_path
|
||||
end
|
||||
end
|
||||
|
||||
context "as super admin" do
|
||||
before {allow(controller).to receive(:spree_current_user) { admin } }
|
||||
let(:params) { { settings: { } } }
|
||||
|
||||
context "when settings are invalid" do
|
||||
before do
|
||||
params[:settings][:account_invoices_monthly_fixed] = ''
|
||||
params[:settings][:account_invoices_monthly_rate] = '2'
|
||||
params[:settings][:account_invoices_monthly_cap] = '-1'
|
||||
spree_get :update, params
|
||||
end
|
||||
|
||||
it "does not allow them to be set" do
|
||||
expect(response).to render_template :edit
|
||||
expect(assigns(:settings).errors.count).to be 4
|
||||
expect(Spree::Config.account_invoices_monthly_fixed).to eq 5
|
||||
expect(Spree::Config.account_invoices_monthly_rate).to eq 0.02
|
||||
expect(Spree::Config.account_invoices_monthly_cap).to eq 50
|
||||
end
|
||||
end
|
||||
|
||||
context "when required settings are valid" do
|
||||
before do
|
||||
params[:settings][:account_invoices_monthly_fixed] = '10'
|
||||
params[:settings][:account_invoices_monthly_rate] = '0.05'
|
||||
params[:settings][:account_invoices_monthly_cap] = '30'
|
||||
end
|
||||
|
||||
it "sets global config to the specified values" do
|
||||
spree_get :update, params
|
||||
expect(assigns(:settings).errors.count).to be 0
|
||||
expect(Spree::Config.account_invoices_monthly_fixed).to eq 10
|
||||
expect(Spree::Config.account_invoices_monthly_rate).to eq 0.05
|
||||
expect(Spree::Config.account_invoices_monthly_cap).to eq 30
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user