range1 = ::Scheduling::DateRange.new(1.day.ago..Time.now)
range2 = ::Scheduling::DateRange.new(2.days.ago..Time.now)
range1.exclusion(range2) => []
range2.exclusion(range1) => [::Scheduling::DateRange.new(2.days.ago..1.day.ago)]
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
# controller | |
def accept_invitation | |
if invitation.accepted? | |
render json: { errors: ['Invitation already accepted'] }, status: 422 | |
else | |
user = User.build_from_invitation(invitation) | |
user.save! | |
invitation.accept | |
invitation.save! | |
UserMailer.notify_affiliate_payment(invitation).deliver_later |
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
def accept_invitation | |
Wf.new | |
.chain(user: :user) { AcceptInvitation.new(invitation).call } | |
.chain {|outflow| render json: { user: outflow.user } } | |
.on_dam do |error_pool| | |
render json: { errors: error_pool.full_messages }, status: 422 | |
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
# User model | |
class User < ActiveRecord::Base | |
has_many :invitations | |
end | |
# Invitation Model | |
class Invitation < ActiveRecord::Base | |
belongs_to :inviter, class_name: 'User' | |
def accept | |
self.accepted = true | |
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
class AcceptInvitation | |
include Waterfall | |
include ActiveModel::Validations | |
CENTS_PAID_FOR_AFFILIATION = 100 | |
validate :ensure_invitation_not_accepted | |
def initialize(invitation) | |
@invitation = invitation |
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
class CreateUserFromInvitation | |
include Waterfall | |
def initialize(invitation) | |
@invitation = invitation | |
end | |
def call | |
chain { build_user } | |
when_falsy { user.save } |
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
class CreditUser | |
include Waterfall | |
def initialize(user:, cents:) | |
@user, @cents = user, cents | |
end | |
def call | |
# credit logic goes here and dams on error | |
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
# test utility for the script | |
def assert(actual, expected) | |
if actual == expected | |
puts "ok #{expected}" | |
else | |
puts "ko, you expected: #{expected} but got #{actual}" | |
end | |
end | |
# first implementation, more functional programming friendly, pure method |
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
class ParamsSlicer | |
def initialize(params, *whitelist) | |
@params = params | |
@nested_whitelist = whitelist.extract_options! | |
@whitelist = whitelist | |
end | |
def call | |
params.slice(*whitelist).tap do |result| | |
nested_whitelist.each do |k, v| |
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
# frozen_string_literal: true | |
source "https://rubygems.org" | |
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } | |
gem 'twilio-ruby' | |
gem 'dotenv' | |
gem 'waterfall' |