Skip to content

Instantly share code, notes, and snippets.

View pikender's full-sized avatar

Pikender Sharma pikender

  • Faridabad, Haryana
View GitHub Profile
diff --git a/nectar-web-router.ex b/nectar-web-router.ex
index 943ae30..90330bb 100644
--- a/nectar-web-router.ex
+++ b/nectar-web-router.ex
@@ -1,3 +1,6 @@
+## Library Code
+## Defines DSL to be used by Service Code
+## in order to get properly consumed by Consume
defmodule Nectar.RouterExtension do
defmacro __using__(_opts) do
@pikender
pikender / generate_incremental_diff.sh
Last active April 12, 2016 17:10
Easy way to generate diff b/w commits for a range while sharing blog post
#!/usr/bin/env bash
if [[ $# -eq 2 ]] ; then
echo "Start Commit: $1\nEnd Commit: $2"
else
>&2 echo "Usage: DRY_RUN=1 $0 [Start-Commit] [End-Commit]\nExample: $0 d2b931acb891d74014d2c5f6a1996f69c222e01c 5c3b13ad8e0c4cf742adc990593926eb25d36fcd"
exit 1
fi
echo "Is Dry run ?? $DRY_RUN"
@pikender
pikender / nectar-web-product.ex.test-fn-extension.bash
Last active April 13, 2016 07:57
Check Nectar Product to have same behaviour while refactoring (creating model support function extension)
nectarcommerce ~/elixir$ iex -S mix
## File Compilation Removed
iex(1)> Nectar.Product.fn_from_outside
"support function"
iex(2)> Nectar.Product.get_name(%Nectar.Product{name: "Name"})
"Name"
iex(3)> a = Nectar.Repo.get(Nectar.Product, 1)
[debug] SELECT p0."id", p0."name", p0."description", p0."available_on", p0."discontinue_on", p0."slug", p0."inserted_at", p0."updated_at" FROM "products" AS p0 WHERE (p0."id" = $1) [1] OK query=271.8ms queue=31.0ms
%Nectar.Product{__meta__: #Ecto.Schema.Metadata<:loaded>,
available_on: #Ecto.Date<2016-03-29>,
@pikender
pikender / nectar-web-product.ex
Last active April 13, 2016 07:57
Model Support Function Extension
## Library Code
## Defines DSL to be used by Service Code
## in order to get properly consumed by Consumer
defmodule Nectar.ModelExtension do
defmacro __using__(_opts) do
quote do
Module.register_attribute(__MODULE__, :method_block, accumulate: true)
import Nectar.ModelExtension, only: [include_method: 1]
@before_compile Nectar.ModelExtension
@pikender
pikender / nectar-web-session-view.ex.test-extension.bash
Created April 13, 2016 13:59
Check Nectar SessionView to have same behaviour while refactoring (creating view partial extension)
nectarcommerce ~/elixir$ iex -S mix phoenix.server
## File Compilation Removed
iex(1)> Nectar.SessionView.render("from_extension.html")
"Please provide an override"
@pikender
pikender / nectar-view-extension-macro.ex
Created April 22, 2016 15:47
Phoenix View Extension
defmodule Extensions.ViewExtension do
defmacro __using__(_opts) do
quote do
Module.register_attribute(__MODULE__, :partials, accumulate: true)
import Extensions.ViewExtension, only: [provide_partial: 2]
@before_compile Extensions.ViewExtension
defmacro __using__(_opts) do
quote do
defmodule ExtendCheckoutView do
use Extensions.ViewExtension
use NectarWallet.NectarExtension, install: "checkout_view"
end
defmodule Nectar.Web do
def view do
quote do
use Nectar.Extender # ensure it is first import, since Phoenix.View will create a some methods due to which it will not be matchable
...
end
end
end
defmodule NectarWallet.NectarExtension do
defmacro __using__([install: install_type]) do
do_install(install_type)
end
defp do_install("checkout_view") do
quote do
provide_partial(NectarWallet.WalletView, "payment.nectar_wallet.html")
end
end
defmodule NectarWallet.WalletView do
use Nectar.Web, :view
def render("payment.nectar_wallet.html", assigns) do
"Pay with my wallet"
end
end