Skip to content

Instantly share code, notes, and snippets.

@victorgiraldes
Created December 10, 2019 13:43
Show Gist options
  • Save victorgiraldes/5a1f69f89e5234d1c2a97f24772fe2d6 to your computer and use it in GitHub Desktop.
Save victorgiraldes/5a1f69f89e5234d1c2a97f24772fe2d6 to your computer and use it in GitHub Desktop.
class Supply::SupplyCreator < ComposableOperations::Operation
processes :params
attr_accessor :company, :user
attr_reader :validator, :supply
delegate :errors, to: :validator
def execute
build
validate(@supply)
persist
@supply
end
private
def build
@supply = company.supplies.new(params)
@supply.user = user
@supply
end
def validate(supply)
@validator = Supply::SupplyCreateValidator.new(supply, company)
return if @validator.valid?
halt @validator.errors
end
def persist
if @supply.save
ActiveRecord::Base.transaction do
generate_debit(@supply)
if @supply.discount > 0
apply_discount(@supply)
end
update_stock_group(@supply)
end
end
end
def generate_debit(supply)
@operation = FinancialService::GenerateDebit.new supply
@operation.kind = "supply"
@operation.perform
end
def apply_discount(supply)
@operation = Supply::ApplyDiscount.new supply
@operation.perform
end
def update_stock_group(supply)
unless params[:stock_group].blank?
supply.stocks.each{|s| s.update_attribute(:stock_group_id, params[:stock_group].to_i)}
end
end
def up_score
return unless user.Potencial?
user.up_score(1.8)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment