Last active
November 27, 2018 17:24
-
-
Save joegaudet/0ec74254d925daf2bc09a615d3e96e8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
module Ordering | |
module Validate | |
class AutoFinalizeTest < ActiveSupport::TestCase | |
describe 'validating whether or not we can auto finalize an order' do | |
describe 'with feature disabled' do | |
it 'returns true' do | |
sut = AutoFinalize.as_subject | |
sut.flipper[:auto_finalize_orders].disable | |
refute sut.(Order.new) | |
end | |
end | |
describe 'with the feature enabled' do | |
let(:order) {Order.new} | |
let(:auto_finalize) { | |
sut = AutoFinalize.as_subject | |
sut.flipper[:auto_finalize_orders].enable | |
sut | |
} | |
describe 'having an invalid payment method' do | |
it 'returns false' do | |
auto_finalize.valid_payment.setup(order, false) | |
refute auto_finalize.(order) | |
end | |
end | |
describe 'having an valid payment method' do | |
before(:each) { auto_finalize.valid_payment.setup(order, true) } | |
it 'returns true when client notes are blank' do | |
assert auto_finalize.(order) | |
end | |
it 'returns false when client notes are present' do | |
order.notes = {client_notes: 'foo'} | |
refute auto_finalize.(order) | |
end | |
it 'returns true when the order is a meal plan order' do | |
order.is_meal_plan_order = true | |
assert auto_finalize.(order) | |
end | |
end | |
end | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DependencySupport | |
# Basic Inert Class maps args to return values | |
class Inert | |
attr_reader :call_history | |
def initialize | |
@return = {} | |
@call_history = [] | |
end | |
def call(*args) | |
@call_history << args | |
@return[args] | |
end | |
def setup(*args, ret) | |
@return[args] = ret | |
end | |
end | |
# Constructor that initializes the dependent object with default substitutes from | |
# the registry, or at most | |
def self.as_subject | |
Rails.logger.debug "Building Subject '#{self.to_s}' in inert mode" | |
subject = self.new | |
self.dependencies.each do |dependency| | |
if dependency.klass.respond_to?(:inert) | |
klass_or_instance = dependency.klass.inert | |
substitute = klass_or_instance.is_a?(Class) ? klass_or_instance.new : klass_or_instance | |
else | |
substitute = Inert.new | |
end | |
subject.substitute_dependency(dependency.name, substitute) | |
end | |
subject | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'flipper/adapters/active_record' | |
require 'flipper/adapters/memory' | |
require 'active_support/cache' | |
require 'flipper/adapters/active_support_cache_store' | |
module Utils | |
class Flipper | |
include DependencySupport | |
dependency :flipper, ::Flipper, static: true | |
delegate :features, :[], :preload_all, :memoize=, to: :flipper | |
def toggle(feature_name, *args) | |
flipper[feature_name].enabled?(*args) ? disable_feature(feature_name, *args) : enable_feature(feature_name, *args) | |
end | |
def enable_feature(feature_name, *args) | |
flipper[feature_name].enable(*args) | |
end | |
def disable_feature(feature_name, *args) | |
flipper[feature_name].disable(*args) | |
end | |
def self.build_in_memory | |
substitute(flipper: ::Flipper.new(::Flipper::Adapters::Memory.new)) | |
end | |
def self.inert | |
build_in_memory | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Ordering | |
module Validate | |
class AutoFinalize | |
include DependencySupport | |
dependency :flipper, Utils::Flipper | |
dependency :valid_payment, Payment | |
def call(order) | |
has_feature_enabled = flipper[:auto_finalize_orders].enabled?(order.client) | |
return false unless has_feature_enabled | |
valid_payment.(order) && (order.client_notes.blank? || order.is_meal_plan_order) | |
end | |
def self.call(order) | |
new.(order) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment