Skip to content

Instantly share code, notes, and snippets.

View aesmail's full-sized avatar
🇯🇵
日本語を勉強しています

Abdullah Esmail aesmail

🇯🇵
日本語を勉強しています
View GitHub Profile
@aesmail
aesmail / control_flow.ex
Created May 29, 2020 16:12
Elixir deciding which control flow to use
# boolean values, use if
if expression, do: this(), else: that()
# non-boolean multi-value single expressions, use case
case expression do
value1 -> one()
value2 -> two()
value3 -> three()
_ -> anything_else()
end
@aesmail
aesmail / email_format_validation.ex
Last active June 25, 2020 03:04
Email format validation helper text
defmodule MyApp.Blog.Author do
use Ecto.Schema
import Ecto.Changeset
schema "authors" do
field :name, :string
field :email, :string
# ...
end
@aesmail
aesmail / phoenix_channels_nativescript.md
Last active November 8, 2018 12:11
NativeScript + Phoenix Channels

I have been struggling (unnecessarily) to make my NativeScript app work seamlessly with Phoenix Channels.

I'm sure this is not the perfect solution, but after trying a lot of other solutions and none of them worked, this one worked for me like a charm.

I'm using:

  • macOS 10.12.6
  • phoenix 1.3.0
  • NativeScript 3.1.3
defmodule MyModule do
# this is valid sytax. However, you can't access x and my_function inside `def`
x = 2
my_function = fn (x) -> x * 2 end
# this is valid syntax. It will replace @my_variable with 2 anywehre inside the module at compile time.
@my_variable 2
# the following line will not compile
# because only the following values are supported here:
@aesmail
aesmail / Python's Decimal behaviour
Created April 27, 2016 10:17
Python: Difference when passing a number or a string to decimal.Decimal
# Scenario ONE
price = Decimal(33.000) # <-- passed in a number
factor = Decimal(0.3020)
price = price / factor
price = price * factor
price.quantize(Decimal("0.01"), rounding=ROUND_UP) # --> 33
# Scenario TWO
price = Decimal('33.000') # <-- passed in a string
factor = Decimal('0.3020')
# controller
def create
user = User.create(params[:user])
respond_with(user, include: :extras)
end
# nginx conf
server {
listen 80;
@aesmail
aesmail / gist:4365006
Created December 23, 2012 18:16
BackgroundLayer is not positioned in the center of the screen.
// BackgroundLayer
-(id) init {
self = [super init];
if(self != nil) {
CCSprite *backgroundImage;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
backgroundImage = [CCSprite spriteWithFile:@"background.png"];
} else {
backgroundImage = [CCSprite spriteWithFile:@"backgroundiPhone.png"];
}
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.references :blog, :null => false
t.datetime :post_date, :null => false
t.string :url, :null => false
t.string :author
t.string :title, :null => false
t.attachment :image, :null => false
t.text :body
@aesmail
aesmail / gist:3369047
Created August 16, 2012 10:11
syntax error in rails time zone conversion file
# code in Blog model
##########################
feed.entries.each do |entry|
puts "=== creating new post..."
post = Post.new(:title => entry.title)
post.blog_id = self.id
puts "=== blog_id = #{post.blog_id}"
# code never reaches past Post.new line
# I have a datetime field (post_date)
<% content_for(:calendar) do %>
<%= render 'calendar' %>
<% end %>
------------
<% content_for(:calendar) { render :partial => 'calendar' } %>
------------