Skip to content

Instantly share code, notes, and snippets.

@Cambero
Cambero / __readme.md
Created August 21, 2019 10:58 — forked from maxivak/__readme.md
Load code in libraries in Rails 5

Load lib files in production (Rails 5)

If you have your code defined in classes in lib/ folder you may have problems to load that code in production.

Autoloading is disabled in the production environment by default because of thread safety.

Change config/application.rb:

    config.autoload_paths << Rails.root.join("lib")
 config.eager_load_paths &lt;&lt; Rails.root.join("lib")
@Cambero
Cambero / rails_load_path_tips.md
Created August 21, 2019 10:57 — forked from maxim/rails_load_path_tips.md
How to use rails load paths, app, and lib directories.

In Rails 3

NOTE: This post now lives (and kept up to date) on my blog: http://hakunin.com/rails3-load-paths

If you add a dir directly under app/

Do nothing. All files in this dir are eager loaded in production and lazy loaded in development by default.

If you add a dir under app/something/

@Cambero
Cambero / user_tracking.rb
Created August 20, 2019 10:46 — forked from kule/user_tracking.rb
Simple User Tracking For Rails
# (concern) e.g. for Post model
module UserTrackable
extend ActiveSupport::Concern
included do
before_create :set_created_by
before_save :set_updated_by
belongs_to :created_by, class_name: 'User', foreign_key: 'created_by_id'
@Cambero
Cambero / submit.md
Created August 1, 2019 11:40 — forked from tanaikech/submit.md
Upload Files to Google Drive using Javascript

Upload Files to Google Drive using Javascript

This is a sample script for uploading files to Google Drive using Javascript. The files are uploaded by Drive API v3. gapi.client.drive.files.create() can create an empty file on Google Drive. But it cannot directly upload files including contents. I think that this might not be able to upload files and metadata with the multipart/related, although this might be resolved by the future update. So now, as one of workarounds, I use using XMLHttpRequest.

  • This sample uses gapi.
    • Before you use this, please enable Drive API at API console and carry out the installation of gapi.
  • When this script is run, a text file including "sample text" is created to Google Drive.
  • When you use this script, please set fileContent and metadata.

In this sample script, a text file including contents is created under a folder.

@Cambero
Cambero / arrayToCSV.js
Created June 25, 2019 12:03 — forked from yangshun/arrayToCSV.js
Converts a 2D array into a CSV file
function arrayToCSV (twoDiArray) {
// Modified from: http://stackoverflow.com/questions/17836273/
// export-javascript-data-to-csv-file-without-server-interaction
var csvRows = [];
for (var i = 0; i < twoDiArray.length; ++i) {
for (var j = 0; j < twoDiArray[i].length; ++j) {
twoDiArray[i][j] = '\"' + twoDiArray[i][j] + '\"'; // Handle elements that contain commas
}
csvRows.push(twoDiArray[i].join(','));
}

Pry Cheat Sheet

Command Line

  • pry -r ./config/app_init_file.rb - load your app into a pry session (look at the file loaded by config.ru)
  • pry -r ./config/environment.rb - load your rails into a pry session

Debugger

@Cambero
Cambero / Jwt_authentication_rails.markdown
Created March 20, 2019 15:28 — forked from NicholasJacques/Jwt_authentication_rails.markdown
JWT authentication in rails using jwt gem and bcrypt

Handroll JWT Authentication for your Rails API because no on likes using Devise

The Problem:

If you’re new to building API’s with Rails you’ve probably wondered how to authenticate requests made to the API to ensure that they are coming from the correct source with correct permissions. Since API’s are stateless applications they do not have the ability to create sessions for users. (Read about how Rails Sessions work here) This creates some challenges when trying to handle authentication because the app isn’t able to remember a user’s session data from one request to the next.

The Solution:

Enter Json Web Tokens. Json Web Tokens (JWT) are a self contained authentication method designed for stateless authentication. A self contained authentication method is one that does not require any storage on the back-end to verify the authenticity of the request. All of the data necessary to authenticate the request is contained right inside the token!

What does a JWT look like?

@Cambero
Cambero / ecommerce_scrap.rb
Created November 29, 2017 12:42 — forked from mpantel/ecommerce_scrap.rb
Using headless, capybara and selenium to automate file downloads
require 'headless'
require 'capybara'
require 'capybara/dsl'
require 'selenium-webdriver'
class EcommerceScrap
include Capybara::DSL
def initialize
@Cambero
Cambero / tmux-cheats.md
Created October 26, 2017 20:40 — forked from Starefossen/tmux-cheats.md
My personal tmux cheat sheet for working with sessions, windows, and panes. `NB` I have remapped the command prefix to `ctrl` + `a`.

Sessions

New Session

  • tmux new [-s name] [cmd] (:new) - new session

Switch Session

  • tmux ls (:ls) - list sessions
  • tmux switch [-t name] (:switch) - switches to an existing session
@Cambero
Cambero / docker-cleanup-resources.md
Created September 28, 2017 14:04 — forked from bastman/docker-cleanup-resources.md
docker cleanup guide: containers, images, volumes, networks

Docker - How to cleanup (unused) resources

Once in a while, you may need to cleanup resources (containers, volumes, images, networks) ...

delete volumes

// see: https://github.com/chadoe/docker-cleanup-volumes

$ docker volume rm $(docker volume ls -qf dangling=true)

$ docker volume ls -qf dangling=true | xargs -r docker volume rm