Skip to content

Instantly share code, notes, and snippets.

View rafaels88's full-sized avatar
🔮
coming up with something

Rafael Soares dos Santos rafaels88

🔮
coming up with something
View GitHub Profile
@rafaels88
rafaels88 / sluggable.rb
Last active August 26, 2018 20:40
Hanami Sluggable (Slugify) Module
# lib/your_app/ext/sluggable.rb
module Sluggable
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def sluggable(field)
@field = field
@rafaels88
rafaels88 / factory_girl_helper.rb
Last active March 3, 2019 21:21
Factory Girl helper to work with Hanami Framework
# USAGE
# 1. Add 'factory_girl' in Gemfile as a dependency. But here, be careful. Factory Girl adds Active Support
# as its dependency, so it is up to you add it in :development and :test group or add it for all envs.
# For more details, read the comment from @cllns below.
# 2. Save this file in '/spec';
# 3. Load this file in 'spec_helper.rb' as the last require in the file, using:
# require_relative './factory_girl_helper'
@rafaels88
rafaels88 / quiqup.dispatch_order_stage_pipeline.ex
Created September 11, 2018 10:24
Quiqup.DispatchOrderStagePipeline
defmodule Quiqup.DispatchOrderStagePipeline do
def call(%{order_id: order_id}) do
with {:ok, %{courier: courier} = order} <- validate_order(order),
{:ok, true} <- courier_available?(courier),
{:ok, next_stage_slug} <- next_stage_slug_by(order),
{:persist_stage, {:ok, %Stage{} = stage}}
<- {:persist_stage, %{order: order} |> create_stage(next_stage_slug)},
{:push_notification, :ok}
<- {:push_notification, push_notification(%{courier: courier, stage: stage})},
{:emit_event, :ok}
@rafaels88
rafaels88 / quiqup.dispatch_order_stage_pipeline.ex
Created September 11, 2018 10:27
Quiqup - Opus Quiqup.DispatchOrderStagePipeline
defmodule Quiqup.DispatchOrderStagePipeline do
use Opus.Pipeline
check :validate_order
step :assign_next_stage_slug
step :assign_courier
check :courier_available?
step :persist_stage
link PushNotificationPipeline
tee :emit_event
@rafaels88
rafaels88 / quiqup.dispatch_order_stage_pipeline.ex
Created September 11, 2018 10:28
Quiqup - Opus, the stages
defmodule Quiqup.DispatchOrderStagePipeline do
use Opus.Pipeline
check :validate_order
step :persist_stage
def validate_order(%{order: order} = pipeline) do
# validation code returns true or false
end
@rafaels88
rafaels88 / quiqup.opus_step.ex
Created September 11, 2018 10:30
Quiqup - Opus Step
defmodule Quiqup.DispatchOrderStagePipeline do
use Opus.Pipeline
step :assign_courier
step :next_stage
def assign_courier(%{order: order} = pipeline) do
put_in(pipeline, [:courier], order.courier)
end
@rafaels88
rafaels88 / quiqup.opus_assign_convention.ex
Last active September 20, 2018 21:23
Quiqup - Opus assign convention
defmodule Quiqup.DispatchOrderStagePipeline do
use Opus.Pipeline
step :assign_availability
step :persist, if: :available?
step :send_alert, if: :persisted?
def assign_availability(pipeline) do
put_in(pipeline, [:availability], :unavailable)
end
@rafaels88
rafaels88 / quiqup.opus_link.ex
Last active September 13, 2018 05:14
Quiqup - Opus Link
@rafaels88
rafaels88 / quiqup.opus_link2.ex
Created September 11, 2018 10:34
Quiqup - Opus Link 2
defmodule Quiqup.PersistCollectStagePipeline do
step :persist_stage
def call?(%{order: order}) do
# check stuff and return true or false
end
def persist_stage(%{order: order})
end
@rafaels88
rafaels88 / quiqup.opus_stage_options.ex
Created September 11, 2018 10:35
Quiqup - Opus Stage Options
defmodule Quiqup.DispatchOrderStagePipeline do
use Opus.Pipeline
import Quiqup.Notifier, only: [emit_event: 1]
check :valid_input?, with: &match?(%{order: _}, &1)
check :validate_order, error_message: :order_is_not_valid
step :assign_next_stage_slug, raise: true, if: :should_assign?
step :assign_courier, instrument?: false
step :persist_stage, raise: [PersistanceError]
step :push_notification, retry_times: 3, retry_backoff: fn -> lin_backoff(10, 2) |> cap(100) end