Skip to content

Instantly share code, notes, and snippets.

View paulcsmith's full-sized avatar

Paul Smith paulcsmith

View GitHub Profile
@paulcsmith
paulcsmith / Filtering params.ex
Last active February 4, 2016 16:55
Building queries using plain functions
def index(conn, params) do
Post
|> filter_upcoming(params)
|> filter_attributes(params, whitelist: ~w(title body)
|> filter_timespan(params)
# Or simplify it if it's used in multiple controllers
Post |> filter_post_params(params)
end
@paulcsmith
paulcsmith / changed.ex
Last active February 2, 2016 17:33
Idea for check whether something changed from one value to another in a changeset
changeset |> changed?(:status, from: "pending", to: "canceled")
changeset |> changed?(:status, from: "attending") # for checking if it changed, but we don't care what it changed to
def changed?(changeset, field, from: from, to: to) do
Ecto.Changeset.get_field(changeset, field) == from
&& booking_changeset.changes[field] == to
end
@paulcsmith
paulcsmith / layout.html.eex
Last active January 29, 2016 03:59
Render title and meta tags in Phoenix
<head>
<!-- you could put a helper in the Layout View but it's probably not necessary -->
<%= render_existing(@view_module, "seo." <> @view_template, assigns) || render("default_seo.html") %>
</head>
@paulcsmith
paulcsmith / bamboo_assertions.ex
Created January 23, 2016 03:15
Bamboo assert_email_delivered/1 function
# Something like this might make testing simpler
def assert_email_delivered(email) do
email = email |> Bamboo.Mailer.normalize_addresses
assert_received({:delivered_bamboo_email, ^email})
end
@paulcsmith
paulcsmith / resources.md
Last active January 20, 2016 17:02
Helpful Resources for learning Elixir/Ecto/Phoenix

Books & in depth resources

  • Phoenix Guides - This is one the best ways to get started. The guides cover most of the fundamental topics and explain the ins and outs of how Phoenix works. I would start here and look up the parts of Elixir you don't understand as you go.
  • Programming Phoenix - Still in beta, but very well done. I would recommend getting it even while in beta. It has a brief introduction to Elixir and covers all the important concepts in Phoenix
  • Elixir Getting Started Guide - This is the official Elixir getting started guide. You don't need to know everything here to get started, but it covers a lot of fundamentals in sections 1-21.

Specific Phoenix Guides that are very helpful

Bypass fake_stripe do
get "soemthing" do
send_resp conn, ""
end
end
defmodule MyApp.AnnouncementTraits do
def with_subscriber(announcement, user) do
create(:subscription, announcement: announcement, user: user)
announcement
end
def tag_with_interest(announcement, interest) do
create(:announcement_interest, announcement: announcement, interest: interest)
announcement
end
defmodule MyApp.Factory do
use ExMachina.Ecto, repo: MyApp.Repo
def factory(:interest, attrs) do
%Interest{
name: sequence(:interest_name, &"interest-#{&1}")
}
end
def factory(:user, attrs) do
@paulcsmith
paulcsmith / factory.ex
Last active October 12, 2015 22:42
Defining factories in ExMachina. Some ideas
# This is a bit more flexible. Worried people will try to call it directly :S
# Also throws warnings if you put functions in between the function definitions
def factory(:user, attrs) do
%User{
first_name: "Paul" ,
last_name: "Smith"
email: put_new_email(attrs)
}
end
@paulcsmith
paulcsmith / cheat_sheet.md
Last active September 22, 2015 20:42
ExMachina Cheat Sheet - v0.3.0
  • factory(factory_name) to define a factory
factory(:user) do
  %User{name: "John"}
end
  • create(:user, name: "Jane")
  • build(:user, name: "Jane")
  • assoc(:author, factory: :user) (can only be used inside factory definition)