Elixir Tips and Tricks That You Might Not Know Of
defmodule App.Team do
use App, :schema
...
// app/instance-initializers/application.js | |
import appInst from 'appName/utils/application' | |
export function initialize( appInstance ) { | |
appInst.instance = appInstance | |
} | |
export default { | |
name: 'application', | |
initialize | |
}; |
defmodule Blog.Attachment do | |
use Blog, :schema | |
@primary_key {:id, :binary_id, autogenerate: true} | |
@storage_path "attachments" | |
schema "attachments" do | |
field :file, :any, virtual: true | |
belongs_to :post, Post |
Ecto.Repo by default doesn't allow overrides. So when you need to compose functions but maintain an API parity with the Ecto.Repo. You'd need to compose the functions in another module and things can get messy really fast that way.
Can't invoke super
== Compilation error on file lib/app/repo.ex ==
** (CompileError) lib/app/repo.ex:6: no super defined for all/2 in module App.Repo. Overridable functions available are:
model = Repo.get!(Model, id) | |
Ecto.Changeset.change(model) | |
|> Ecto.Model.Timestamps.put_timestamp(:deleted_at, Ecto.DateTime, true) | |
|> Repo.update |
I wrote a middleware class that sets a singleton "Context" based upon either a domain or subdomain. This was an untested class and was refactored for testing with some help from @adamwathan (https://gist.github.com/adamwathan/d8dd4ea5337eb3f975b8).
<?php | |
require __DIR__ . '/vendor/autoload.php'; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\HandlerStack; | |
use GuzzleHttp\Handler\CurlFactory; | |
use GuzzleHttp\Handler\CurlHandler; | |
use GuzzleHttp\Handler\CurlMultiHandler; | |
use GuzzleHttp\Handler\EasyHandle; |
// Carbon | |
use Carbon\Carbon; | |
$visitorTraffic = PageView::select('id', 'title', 'created_at') | |
->get() | |
->groupBy(function($date) { | |
return Carbon::parse($date->created_at)->format('Y'); // grouping by years | |
//return Carbon::parse($date->created_at)->format('m'); // grouping by months | |
}); |
The goal is to support soft delete functionality in Ecto.Repo
. With the suggestion by @imranismail, another repo is created and the remaining functionalities are delegate to the original MyApp.Repo
.
The new repo get/2
and all/1
functions will exclude the soft deleted record by default. delete/1
and delete_all/1
will update the delete_at
column by default instead of deleting.
MyApp.Repo.get(MyApp.User, 1) //will return nil if record is in soft delete state