Skip to content

Instantly share code, notes, and snippets.

View namtx's full-sized avatar
🐤
Fullstuck developer

0x6e616d7478 namtx

🐤
Fullstuck developer
View GitHub Profile
@namtx
namtx / App.jsx
Created July 7, 2018 07:56
React `ref` with `HOC`
import React, { Component } from "react";
import logo from "./logo.svg";
import "./App.css";
import InputField from "./FancyInput";
class App extends Component {
constructor(props) {
super(props);
this.textInput = React.createRef();
// this.handleSubmit = this.handleSubmit.bind(this);
@namtx
namtx / my_log_subscriber.rb
Created June 12, 2018 06:01
How to use ActiveSupport::LogSubscriber
require 'active_support/log_subscriber'
module ActiveStorage
class LogSubscriber < ActiveSupport::LogSubscriber
def service_upload(event)
message = event.payload[:something]
info event, color(message, GREEN)
end
end
end
@namtx
namtx / encrypter.rb
Last active June 7, 2018 02:02
adapter
class Encrypter
def initialize(key)
@key = key
end
def encrypt(reader, writer)
key_index = 0
while not reader.eof?
clear_char = reader.getc
encrypted_char = clear_char ^ @key[key_index]
writer.putc(encrypted_char)
@namtx
namtx / greeting.rb
Last active May 17, 2018 08:54
Ruby Metaprogramming
class Greeting
def initialize text
@text = text
end
def welcome
@text
end
end
class Quesion < ApplicationRecord
SUBMITITABLE_TYPES = %w(Open MultipleChoice Scale).freeze
validates :maximum, presence: true, if: :scale?
validates :minimum, presence: true, if: :scale?
validates :question_type, presence: true, inclusion: SUBMITITABLE_TYPES
validates :title, presence: true
def summary
case question_type
![lgtm](http://i.imgur.com/XTVGHwg.gif)
@namtx
namtx / postgres-cheatsheet.md
Created September 22, 2017 02:13 — forked from Kartones/postgres-cheatsheet.md
PostgreSQL command line cheatsheet

PSQL

Magic words:

psql -U postgres

Some interesting flags (to see all, use -h):

  • -E: will describe the underlaying queries of the \ commands (cool for learning!)
  • -l: psql will list all databases and then exit (useful if the user you connect with doesn't has a default database, like at AWS RDS)
@namtx
namtx / presenters.md
Created June 15, 2017 08:26 — forked from somebox/presenters.md
Thoughts About Rails Presenters

Thoughts about Rails Presenters

This is a collection of links, examples and rants about Presenters/Decorators in Rails.


The "Decorator" pattern slowly started gaining popularity in Rails several years ago. It is not part of core Rails, and there's many different interpretations about how it should work in practice.

Jay Fields wrote about it in 2007 (before he switched back to Java and then Clojure): http://blog.jayfields.com/2007/03/rails-presenter-pattern.html

problems

class Address < ActiveRecord::Base
  belongs_to :customer
end

class Customer <ActiveRecord::Base
  has_one :address
  has_many :invoices
end