Skip to content

Instantly share code, notes, and snippets.

@kadru
kadru / lvh_ssl.md
Created September 6, 2019 15:43 — forked from dagjaneiro/lvh_ssl.md
lvh.me ssl

Install nginx

$ brew install nginx

Edit nginx.conf

$ vim /usr/local/etc/nginx/nginx.conf
@kadru
kadru / order-rails-controller-callbacks.md
Created April 6, 2019 01:52 — forked from WaKeMaTTa/order-rails-controller-callbacks.md
Order of Rails Controller Callbacks

Order of Rails Controller Callbacks

Rails 4.x

Started GET "/" for 127.0.0.1 at 2017-05-19 14:17:18 +0200
  Processing by WelcomeController#index as HTML
    prepend_around_action
    prepend_before_action
 before_action
require 'net/ldap'
module Ldap
LDAP_SERVER_IP = 'buzon.uach.mx'.freeze
#LDAP_USERNAME = 'ldap_username'
#LDAP_PASSWORD = 'ldap_password'
#Obtiene el nombre mediante su cuenta de usuario
@kadru
kadru / ror_date_time.rb
Last active January 21, 2019 04:01
Ruby on Rails Dates and Times cheat sheet credits to https://www.varvet.com/blog/working-with-time-zones-in-ruby-on-rails
#the do´s
2.hours.ago # => Thu, 27 Aug 2015 14:39:36 AFT +04:30
1.day.from_now # => Fri, 28 Aug 2015 16:39:36 AFT +04:30
Time.zone.parse("2015-08-27T12:09:36Z") # => Thu, 27 Aug 2015 16:39:36 AFT +04:30
Time.current # => Thu, 27 Aug 2015 16:39:36 AFT +04:30
Time.current.utc.iso8601 # When supliyng an API ("2015-08-27T12:09:36Z")
Time.strptime("2015-08-27T12:09:36Z", "%Y-%m-%dT%H:%M:%S%z").in_time_zone # If you can’t use Time.zone.parse (Thu, 27 Aug 2015 16:39:36 AFT +04:30)
Date.current # If you really can’t have a Time or DateTime for some reason (Thu, 27 Aug 2015)
Date.current.in_time_zone # If you have a date and want to make the best out of it (Thu, 27 Aug 2015 00:00:00 AFT +04:30)
#the don´ts
@kadru
kadru / db_rake.sh
Created December 18, 2018 15:59
All Rails db Rake Tasks
db:create #Creates the database for the current RAILS_ENV environment. If RAILS_ENV is not specified it defaults to the development and test databases.
db:create:all #Creates the database for all environments.
db:drop #Drops the database for the current RAILS_ENV environment. If RAILS_ENV is not specified it defaults to the development and test databases.
db:drop:all #Drops the database for all environments.
db:migrate #Runs migrations for the current environment that have not run yet. By default it will run migrations only in the development environment.
db:migrate:redo #Runs db:migrate:down and db:migrate:up or db:migrate:rollback and db:migrate:up depending on the specified migration. I usually run this after creating and running a new migration to ensure the migration is reversable.
db:migrate:up #Runs the up for the given migration VERSION.
@kadru
kadru / transactions.markdown
Created December 16, 2018 04:32 — forked from jcasimir/transactions.markdown
Transactions

Transactions

As your business logic gets complex you may need to implement transactions. The classic example is a bank funds transfer from account A to account B. If the withdrawal from account A fails then the deposit to account B should either never take place or be rolled back.

Basics

All the complexity is handled by ActiveRecord::Transactions. Any model class or instance has a method named .transaction. When called and passed a block, that block will be executed inside a database transaction. If there's an exception raised, the transaction will automatically be rolled back.

Example

@kadru
kadru / rails http status codes
Created November 22, 2018 03:34 — forked from mlanett/rails http status codes
HTTP status code symbols for Rails
HTTP status code symbols for Rails
Thanks to Cody Fauser for this list of HTTP responce codes and their Ruby on Rails symbol mappings.
Status Code Symbol
1xx Informational
100 :continue
101 :switching_protocols
102 :processing
#Date Formatting I
'%Y' #Year with century (e.g. 2015, 1995, etc)
'%m' #Month of the year, zero-padded (01..12)
'%B' #The full month name (e.g. January)
'%b' #The abbreviated month name (e.g. Jan)
'%d' #Day of the month, zero-padded (01..31)
'%j' #Day of the year (001..366)
@kadru
kadru / safe_nav_op_enum_dig.rb
Created November 11, 2018 03:11
safe navigation operator & and Enumerable #dig (ruby 2.3 and up)
#Safe navigation operator
if account && account.owner && account.owner.address
#your code here
end
#could be rewritten like
account&.owner&.address
#more examples
account = Account.new(owner: nil) # account without an owner
#
@kadru
kadru / commands_related_to_processes.sh
Last active January 30, 2019 21:27
Command related to processes