Skip to content

Instantly share code, notes, and snippets.

@talum
talum / invoices_controller.rb
Created May 28, 2019 15:56
Invoice Controller
class InvoicesController < ApplicationController
  def edit
  @invoice = Invoice.find(params[:id])
  end
  def update
  invoice = Invoice.find(params[:id])
  invoice.update!(invoice_params)
  redirect_to invoice
@talum
talum / invoice.rb
Created May 28, 2019 15:55
Invoice Model
class Invoice < ActiveRecord::Base
  validates_presence_of :amount_due, :due_date, :scheduled_send_date
  validate :closeable_if_unpaid
  def closeable_if_unpaid
  if paid_at.present? && closed_at.present?
  errors.add(:closed_at, "cannot be closed")
  end
  end
end
@talum
talum / invoice_migration.rb
Created May 28, 2019 15:45
Invoice Migrations
# db/schema.rb
create_table "invoices", force: :cascade do |t|
  t.datetime "closed_at"
  t.datetime "due_date"
  t.datetime "paid_at"
  t.datetime "scheduled_send_date"
  t.datetime "sent_at"
  t.integer "amount_due"
  t.string "invoice_type"
end
@talum
talum / ch3and4.md
Created November 27, 2018 04:11
microservices_patterns

Microservices Patterns

Chapter 3 Interprocess Communication in a microservice architecture

Intro

IPC (Interprocess Communication)

  • REST w/ JSON - the fashionable choice
  • Messaging

Considerations

class Lesson < Curriculum
belongs_to :canonical_material, class_name: CourseWork::CanonicalMaterial
has_one :lab, foreign_key: :curriculum_id
def lab?
if $rollout.active?(:remove_content)
canonical_material.lab?
else
!!lab
end
class AssignedRepo < ActiveRecord::Base
def contents
if $rollout.active?(:remove_content)
raise 'Contents called from assigned repo.' # Remove method when removing :remove_content rollout
else
return self.none if canonical.nil?
canonical.contents
end
end
@talum
talum / sti_3.rb
Last active November 25, 2018 19:29
class Track < Curriculum
def labs
if $rollout.active?(:remove_content)
CourseWork::CanonicalMaterial
.joins(:lessons)
.where(curriculums: {track_id: self.id})
.where(kind: CourseWork::CanonicalMaterial::LAB_KINDS)
else
Lab.where(track_id: self.id)
end
class Api::V1::SearchesController < ApiController
def new
gh_id_mappings = if $rollout.active?(:remove_content)
GithubRepository.track_data_for_github_repos(repos.pluck(:id), current_user.active_track_id)
else
GithubRepository.github_id_mappings_for_global_search(current_user)
end
# omitted
end
@talum
talum / sti_1b.rb
Last active November 25, 2018 19:09
# some other caller
def set_projects_progress_status!
project_repo_ids = AssignedRepo.team_projects_for(batch_id: batch.id, track_id: track.id).pluck(:canonical_id)
end
def set_projects_progress_status!
project_repo_ids = AssignedRepo.team_projects_for(batch_id: batch.id, track_id: track.id).canonical_github_repository_ids
end
# Assigned Repo
@talum
talum / sti_1a.rb
Last active November 25, 2018 19:07
STI examples
def completed_labs_count(student, canonical_repo_ids)
if $rollout.active?(:remove_content)
student.canonical_progresses.complete.
joins(github_repository: :canonical_material).
where(github_repository_id: canonical_repo_ids).
where(kind: CourseWork::CanonicalMaterial::LAB_KINDS).
uniq(:id).
count
else
student.canonical_progresses.complete.