Skip to content

Instantly share code, notes, and snippets.

View chamnap's full-sized avatar

Chamnap Chhorn chamnap

View GitHub Profile
@njakobsen
njakobsen / live_database_dump.rb
Last active November 5, 2021 02:28
Live stream a database dump (or any other STDOUT) using Rails 4. Why would you want this? If you have a large database dump and want to avoid storing it in memory as Rails streams it. This allows pipe the dump directly into the http response instead of storing it as a file, sending it, and then deleting it. Let me know what you think! I've teste…
class DatabaseController < ApplicationController
def database_dump
database = Rails.configuration.database_configuration[Rails.env]["database"]
send_file_headers!(:type => 'application/octet-stream', :filename => "#{database}_#{Time.now.to_s(:human)}.backup")
pipe = IO.popen("pg_dump '#{database}' -F c")
stream = response.stream
while (line = pipe.gets)
stream.write line
sleep 0.0001 # HACK: Prevent server instance from sleeping forever if client disconnects during download
@addyosmani
addyosmani / headless.md
Last active May 1, 2024 03:05
So, you want to run Chrome headless.

Update May 2017

Eric Bidelman has documented some of the common workflows possible with headless Chrome over in https://developers.google.com/web/updates/2017/04/headless-chrome.

Update

If you're looking at this in 2016 and beyond, I strongly recommend investigating real headless Chrome: https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md

Windows and Mac users might find using Justin Ribeiro's Docker setup useful here while full support for these platforms is being worked out.

@Mic92
Mic92 / index.html
Last active December 15, 2015 08:39
Zeroclipboard css styling for twitter bootstrap
<button id="copy" class="btn">Copy</button>
@ryanb
ryanb / episode_search.rb
Created February 18, 2013 00:44
Class that handles the PostgreSQL full text search for railscasts.com
class EpisodeSearch
attr_reader :ability, :options
delegate :sanitize, to: Episode
def initialize(ability, options = {})
@ability = ability
@options = options.dup
end
def tag
@ryanb
ryanb / issues_with_modules.md
Created November 29, 2012 22:38
Points on how modules can make code difficult to read.

My issues with Modules

In researching topics for RailsCasts I often read code in Rails and other gems. This is a great exercise to do. Not only will you pick up some coding tips, but it can help you better understand what makes code readable.

A common practice to organize code in gems is to divide it into modules. When this is done extensively I find it becomes very difficult to read. Before I explain further, a quick detour on instance_eval.

You can find instance_eval used in many DSLs: from routes to state machines. Here's an example from Thinking Sphinx.

class Article &lt; ActiveRecord::Base
@marcusmalmberg
marcusmalmberg / CarrierWave.md
Last active June 14, 2021 23:22
Guide to setup CarrierWave which will upload a file to Amazon S3 in production environment and use local storage in development and test

CarrierWave home

https://github.com/jnicklas/carrierwave

This example will create an uploader that will upload a file stored in a model Model. The file will be stored locally in development and test environment and will use Amazon S3 in production.

CarrierWave installation

First add the gems.

@ryanb
ryanb / tasks_controller_refactoring.rb
Created November 9, 2012 23:07
Variation of RubyTapas episode "021 Domain Model Events" without using callbacks
class TasksController < ApplicationController
def update
tracker = TaskTracker.new(@task)
if @task.update_attributes(params[:task])
TaskPusher.new(tracker, socket_id).push_changes
TaskMailSender.new(tracker, current_user).deliver_email
# success response
else
# failure respond
end
@ryanb
ryanb / abilities.rb
Created September 15, 2012 19:23
How you can break up large Ability class in CanCan
module Abilities
def self.ability_for(user)
if user.admin?
AdminAbility.new(user)
else user
MemberAbility.new(user)
else
GuestAbility.new
end
end
@greypants
greypants / README.markdown
Last active October 17, 2023 05:49 — forked from reagent/nav_link.rb
RAILS 3: nav_link helper for adding 'selected' class to navigation elements
@jshaw
jshaw / FacebookPageTabResizeiFrame.html
Created July 26, 2012 18:28
Resize the Facebook iFrame Tab Page with resizing content and different page heights on page loads.
<!DOCTYPE html>
<html lang="en" style="overflow: hidden">
<body style="overflow:hidden">
<head>
<!-- You need to include jquery & FBSDK-->
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {