Skip to content

Instantly share code, notes, and snippets.

View anchietajunior's full-sized avatar
💭
Working Hard

José Anchieta anchietajunior

💭
Working Hard
View GitHub Profile
def create
@payment = Payment.new(payment_params)
respond_to do |format|
if @payment.save
#Business Logic
ServiceInvoice.create(payment: @payment,
status: @payment.amount > 100000 ? “pending” : “issued”)
format.html { redirect_to @payment, notice: ‘Payment was successfully created.’ }
else
format.html { render :new }
class Payment < ApplicationRecord
has_one :service_invoice
end
class ServiceInvoice < ApplicationRecord
belongs_to :payment
validates :payment, presence: true
end
require 'rails_helper'
RSpec.describe CreateInvoiceService do
before(:each) do
@payment = Payment.create(amount: 200000, description: "Xbox One", quantity: 1)
end
context "creating payments" do
it "has a invoice with pending status" do
result = CreateInvoiceService.new({payment_id: @payment.id}).charge
class User
has_one :plan
has_many :payments
def update_plan(plan)
self.update_columns(plan: plan)
end
def create_payment
Payment.create(user: self)
@anchietajunior
anchietajunior / structs1.rb
Last active July 25, 2018 02:08
Structs Examples
Notebook = Struct.new(:cpu, :ram, :ssd) do
def all_configs
"PC configs: #{cpu} - #{ram} - #{ssd}"
end
end
notebook = Notebook.new("i7", "16GB", "256")
notebook = ["i7 CPU", "16GB RAM", "256 HD"]
Notebook = Struct.new(:cpu, :ram, :ssd)
notebook = Notebook.new("i7", "16GB", "256SSD")
class StatusService
Status = Struct.new(success?, errors)
def initialize(request_params)
@request_params = request_params
end
def call
if @request_params[:status].present? && @request_params[:status] == "success"
function getStarWarsPeople() {
fetch('https://swapi.co/api/people')
.then(data => data.json())
.then(data => data.results.map(person => {
console.log(person.name);
}))
console.log('Luke i`m your father!');
}