Skip to content

Instantly share code, notes, and snippets.

View 0legovich's full-sized avatar
🤝
You are welcome

Shpak Roman 0legovich

🤝
You are welcome
View GitHub Profile
@0legovich
0legovich / operation_proxy_container.rb
Created August 2, 2019 12:04
Пример использования dry-transaction и dry-container
# frozen_string_literal: true
class OperationProxyContainer # :nodoc:
extend Dry::Container::Mixin
Import = Dry::AutoInject(self)
register 'make_document_file' do
MakeOperationProxyDocumentFileService
end
@0legovich
0legovich / async_job_controller.rb
Last active August 2, 2019 21:03
Пример использования ActionController::Live для проверка статуса выполнения асинхронной джобы.
# frozen_string_literal: true
class AsyncJobController < ApplicationController # :nodoc:
include ActionController::Live
before_action :initialize_job_id, only: :status_live
JOB_DATA_KEYS = %i[pct_complete status message].freeze
PCT_FINISHED = 100
STATUS_FINISHED = 'complete'
@0legovich
0legovich / 1_preview.rb
Last active August 5, 2019 14:03
SaveResource module
# SaveResource предназначен для созданиия записи в БД с nested_attributes (или без них).
# Особенности:
# * если у записи уже есть persisted nested records и при обновлении их атрибуты не были переданы, то такие записи будут удалены;
# * перед сохранением записи в БД, с ней можно выполнить любое действие (например установить дефолтные параметры) с помощью передачи блока в конструктор.
# Примеры вызова:
def create
update_position = ->(operation) { operation.position = operation.user.positions.first! }
service = SaveResource::Common.new(Operation, params[:operation], &update_position)
# frozen_string_literal: true
# parse react json schema (https://github.com/mozilla-services/react-jsonschema-form)
# return Array with elements: Symbol, Special Hash
# Symbol - field name
# Special Hash: Hash, where
# * key - nested fields name,
# * value -
# Hash: key - nested object id, value - Array with elements: Symbol or Special Hash
class Checkout
attr_reader :items, :pricing_rules
def initialize(pricing_rules)
@pricing_rules = pricing_rules
@items = []
end
def add(item)
items.push item