Skip to content

Instantly share code, notes, and snippets.

View benvp's full-sized avatar
🤌

Benjamin von Polheim benvp

🤌
View GitHub Profile
@benvp
benvp / excercise.ex
Created July 10, 2017 15:47
LearningElixir.tv - Ecto Part 2
def reload(item) do
%struct{} = item
pk = struct.__schema__(:primary_key) |> List.first
%{^pk => id} = item
get(struct, id)
end
def max(queryable, fieldName) do
query = from x in queryable,
select: max(field(x, ^fieldName))

Keybase proof

I hereby claim:

  • I am benvp on github.
  • I am benvp (https://keybase.io/benvp) on keybase.
  • I have a public key ASA67CkwGjbTJnmroRS0FqsrYFm5aVggYrJba_M35IUbrQo

To claim this, I am signing this object:

@benvp
benvp / vscodeextensions.json
Created May 6, 2019 08:43
vscode extensions
// 20190506104306
// https://gist.githubusercontent.com/benlime/ae8087466b2d969f7916c7425356d1fa/raw/8e51a731c232af378db0557f3e8ad5765e61d671/extensions.json
[
{
"metadata": {
"id": "beb66794-aacf-4696-937a-465e2f9508c5",
"publisherId": "myax.appidocsnippets",
"publisherDisplayName": "myax"
},
@benvp
benvp / install_alpine.sh
Last active July 17, 2020 10:54
Phoenix LiveView Autocomplete
# in your phoenix project dir
cd assets
yarn add alpinejs
import 'alpinejs';
// ...
let liveSocket = new LiveSocket('/live', Socket, {
dom: {
// make LiveView work nicely with alpinejs
onBeforeElUpdated(from, to) {
if (from.__x) {
window.Alpine.clone(from.__x, to);
defmodule Autocomplete.Items do
@items [
%{id: 1, name: "Item 1", image_url: "images/items/1.png"},
%{id: 2, name: "Item 2", image_url: "images/items/2.png"},
%{id: 3, name: "Item 3", image_url: "images/items/3.png"},
%{id: 4, name: "Item 4", image_url: "images/items/4.png"}
]
def list_items() do
@items
defmodule AutocompleteWeb.ItemSearchLive do
use AutocompleteWeb, :live_view
alias Autocomplete.Items
def mount(_params, _session, socket) do
items = Items.list_items()
{:ok, assign(socket, suggestions: items, selected: [])}
end
scope "/", AutocompleteWeb do
pipe_through :browser
live "/", ItemSearchLive, :index
end
<div class="container">
<h2>Item search</h2>
<script>
// we add a global autocomplete function
// which will handle our client-side logic.
// we extend this later on...
function autocomplete() {
return {
isOpen: false,
def handle_event("suggest", %{"search" => search}, socket) do
suggestions = Items.list_items() |> suggest(search)
{:noreply, assign(socket, suggestions: suggestions)}
end
defp suggest(items, search) do
Enum.filter(items, fn i ->
i.name
|> String.downcase()