Skip to content

Instantly share code, notes, and snippets.

🎯
Focusing

Tracy Lum talum

🎯
Focusing
Block or report user

Report or block talum

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@talum
talum / invoice.ex
Created May 28, 2019
Invoice changeset with constraint
View invoice.ex
defmodule Registrar.Billing.Invoice do
import Ecto.Changeset
def changeset(%Invoice{} = invoice, attrs \\ %{}) do
  invoice
  |> cast(attrs, [
   :amount_due,
   :due_date,
   :scheduled_send_date,
   :email
@talum
talum / invoice.ex
Created May 28, 2019
invoice changeset
View invoice.ex
defmodule Registrar.Billing.Invoice do
import Ecto.Changeset
def changeset(%Invoice{} = invoice, attrs \\ %{}) do
  invoice
  |> cast(attrs, [
   :amount_due,
   :due_date,
   :scheduled_send_date
  ])
@talum
talum / invoice.ex
Created May 28, 2019
Closed invoice
View invoice.ex
defmodule Registrar.Billing.Invoice do
import Ecto.Changeset
def mark_closed_changeset(%Invoice{} = invoice, attrs \\ %{}) do
  invoice
  |> cast(attrs, [
   :closed_at
  ])
  |> validate_required([
   :closed_at
@talum
talum / invoice.ex
Created May 28, 2019
Paid Changeset
View invoice.ex
defmodule Registrar.Billing.Invoice do
import Ecto.Changeset
def mark_paid_changeset(%Invoice{} = invoice, attrs \\ %{}) do
  invoice
  |> cast(attrs, [
   :paid_at
  ])
  |> validate_required([
   :paid_at
View changeset_demo.ex
iex> attributes = %{amount_due: 3000, due_date: "2019-04-30", scheduled_send_date: "2019-04-25",
iex> closed_at: "2019-05-01"}
iex> changeset = Registrar.Billing.Invoice.changeset(%Invoice{}, attributes)
iex> #Ecto.Changeset<
  action: nil,
  changes: %{amount_due: 3000, due_date: ~D[2019-04-30], scheduled_send_date: ~D[2019-04-25]},
  errors: [],
  data: #Registrar.Billing.Invoice<>,
  valid?: true
>
View invoice.ex
defmodule Registrar.Billing.Invoice do
# assume that we have an Ecto.Schema defined with all the fields this module needs to know about
import Ecto.Changeset
def changeset(%Invoice{} = invoice, attrs \\ %{}) do
  invoice
  |> cast(attrs, [
   :amount_due,
   :due_date,
   :scheduled_send_date
@talum
talum / invoices_controller.rb
Created May 28, 2019
Invoices Controller 2
View invoices_controller.rb
class InvoicesController < ApplicationController
  def update
  # The developer reasons that adding a timestamp to the params to
  # leverage the existing code will do the trick
  if params[:cancel]
  invoice_params[:closed_at] = Time.current
  end
View invoices_controller.rb
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
Invoice Model
View invoice.rb
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
View invoice_migration.rb
# 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
You can’t perform that action at this time.