Skip to content

Instantly share code, notes, and snippets.

@JamesAndresCM
JamesAndresCM / authorization.ex
Created January 25, 2024 02:39 — forked from nikneroz/authorization.ex
Elixir + Phoenix Framework 1.3 + Guardian 1.0 + JWT(Refresh, Revoke, Recover) + Comeonin
# Elixir + Phoenix Framework 1.3 + Guardian + JWT(Refresh, Revoke, Recover) + Comeonin
### User model bootstrap
Let's generate User model and controller.
```bash
mix ecto.create # create DB table
mix phx.gen.json Accounts User users email:string password_hash:string # scaffold users structure
```

The Complete Guide to Nested Forms in Phoenix

I recently spent some time dealing with nested forms in Phoenix. Nested forms are great when you want to create multiple database records in a single transaction and associate them with each other. I am new to Phoenix and really struggled to find any resources that helped me with my specific problem. I decided to document what I learned in the process in hopes of helping others that are new to Elixir and Phoenix.

Here is my attempt at a one stop shop to learn everything you will need to know about nested forms. If you would like to view the GitHub repo you can check it out here.

Thanks to Heartbeat and Jose for excellent blog posts on nested forms. Also shoutout to Josh for showing me some examples at Ruby

Estos serian los perfiles?, que en realidad el modelo es group

Customer.first.groups
  Customer Load (1.3ms)  SELECT  "customers".* FROM "customers" WHERE "customers"."deleted_at" IS NULL ORDER BY "customers"."id" ASC LIMIT $1  [["LIMIT", 1]]
  Group Load (1.4ms)  SELECT  "groups".* FROM "groups" WHERE "groups"."deleted_at" IS NULL AND "groups"."customer_id" = $1 LIMIT $2  [["customer_id", 1], ["LIMIT", 11]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Group id: 3, name: "Admin", created_at: "2023-03-23 19:29:42", updated_at: "2023-03-23 19:29:42", customer_id: 1, deleted_at: nil, local_id: "71877852-23aa-43fa-8ed0-ea46ed4b76e2", threshold_quantity: nil>, #<Group id: 13, name: "prueba", created_at: "2023-06-07 22:08:25", updated_at: "2023-06-07 22:08:25", customer_id: 1, deleted_at: nil, local_id: "b4f5e733-2571-49c8-a8bb-3352085c410a", threshold_quantity: nil>]>
@JamesAndresCM
JamesAndresCM / README.md
Created May 13, 2023 22:07 — forked from jesster2k10/README.md
JWT Auth + Refresh Tokens in Rails

JWT Auth + Refresh Tokens in Rails

This is just some code I recently used in my development application in order to add token-based authentication for my api-only rails app. The api-client was to be consumed by a mobile application, so I needed an authentication solution that would keep the user logged in indefinetly and the only way to do this was either using refresh tokens or sliding sessions.

I also needed a way to both blacklist and whitelist tokens based on a unique identifier (jti)

Before trying it out DIY, I considered using:

@JamesAndresCM
JamesAndresCM / ntfs-mac-m1-ventura.md
Created March 2, 2023 02:34
mount ntfs in mac m1

Install ntfs-3g

brew tap gromgit/homebrew-fuse
brew install --cask macfuse
brew install ntfs-3g-mac

Enable Security development in your mac (startup boot configuration -> utilities)

Identify your ntfs HDD

Async Proccess Elixir

run_query = fn query_def -> Process.sleep(2000); "#{query_def} result" end
async_query = fn query_def -> spawn(fn -> IO.puts(run_query.(query_def)) end) end
Enum.each(1..5, &async_query.("query #{&1}"))

Async Proccess Ruby 3

# first way
# frozen_string_literal: true
class EncryptionService
delegate :encrypt_and_sign, :decrypt_and_verify, to: :encryptor
class << self
def encrypt(secret:)
new.encrypt_and_sign(secret)
end
module Utilities
def self.included(base)
base.extend(ClassMethods)
end
def method_one
puts 'Hello from an instance method'
end
module ClassMethods
@JamesAndresCM
JamesAndresCM / parent_child.rb
Last active November 10, 2021 13:13
example parent child
# categories without link
rails g model Category name parent_id:integer:index
class Category < ApplicationRecord
has_many :subcategories, class_name: 'Category', foreign_key: 'parent_id', dependent: :destroy
belongs_to :parent, class_name: 'Category', foreign_key: 'parent_id', optional: true
end
# linked_categories
rails g model Category name
class ReviewConfiguration < BaseRecord
has_many :review_answers, dependent: :destroy
accepts_nested_attributes_for :review_answers, allow_destroy: true
end
def review_answers_attributes=(attributes)
attributes = attributes.map(&:deep_symbolize_keys)
review_answer_ids = attributes.map{|el| el[:id]}.compact
begin
review_answers << ReviewAnswer.where(id: review_answer_ids)