Skip to content

Instantly share code, notes, and snippets.

View nicolasblanco's full-sized avatar
🎯
Focusing

Nicolas Blanco nicolasblanco

🎯
Focusing
View GitHub Profile
def add(conn, %{"product_id" => product_id}) do
product = Front.get_product!(product_id)
conn = case conn.assigns[:current_order] do
nil ->
order = Front.create_order_with_product!(product)
conn |> put_session(:order_id, order.id)
order ->
Front.add_product_to_order!(order, product)
conn
@nicolasblanco
nicolasblanco / translation_fields.ex
Last active July 1, 2017 00:02
Translation Fields
defmodule TranslationFields do
@moduledoc """
Usage:
defmodule Product do
use TranslationFields
@translated_locales ~w(en fr)
@translated_fields ~w(name description)
schema "products" do
class Foo
def self.bar
42
end
private
def self.private_bar
"oh noes"
end
!!undefined
// false
!!""
// false
!!0
// false
!!null
// false
!!1
// true
[1, 2, 3, 4].map(res => res * 2)
// [2, 4, 6, 8]
['number', 'of', 'letters'].reduce((m, o) => m += o.length, 0)
// 15
class WizardsController < ApplicationController
before_action :load_user_wizard, except: %i(validate_step)
def validate_step
current_step = params[:current_step]
@user_wizard = wizard_user_for_step(current_step)
@user_wizard.user.attributes = user_wizard_params
session[:user_attributes] = @user_wizard.user.attributes
@nicolasblanco
nicolasblanco / user.rb
Last active November 29, 2016 22:28
wizard rails app 2
module Wizard
module User
STEPS = %w(step1 step2 step3 step4).freeze
class Base
include ActiveModel::Model
attr_accessor :user
delegate *::User.attribute_names.map { |attr| [attr, "#{attr}="] }.flatten, to: :user
@nicolasblanco
nicolasblanco / user.rb
Created November 29, 2016 15:32
wizard rails app
class User < ApplicationRecord
validates :email, presence: true, format: { with: /@/ }
validates :first_name, presence: true
validates :last_name, presence: true
validates :address_1, presence: true
validates :zip_code, presence: true
validates :city, presence: true
validates :country, presence: true
validates :phone_number, presence: true
end
# config/environments/production.rb
config.action_mailer.smtp_settings = {
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'your_application_name.herokuapp.com',
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
@nicolasblanco
nicolasblanco / simple_exposure.rb
Last active November 9, 2016 16:09
Exposure Pattern
# Exposure pattern to use declarative interfaces in controller (which is the basic feature of the library decent_exposure).
#
# Using this pattern you can dramatically reduce or remove completely the need for instance variables in controllers and views.
#
# Example :
#
# BEFORE (traditional way in Ruby on Rails)
#
# class ArticlesController < ApplicationController
# before_action :load_current_blog