Skip to content

Instantly share code, notes, and snippets.

@oojikoo-gist
oojikoo-gist / session_store.md
Created April 12, 2015 12:43
rails: database based session

The best practice is to use a database based session, which thankfully is very easy with Rails:

Project::Application.config.session_store :active_record_store
@oojikoo-gist
oojikoo-gist / heroku_dev.md
Last active August 29, 2015 14:19
heroku: development test

create another heroku instance for git remote named heroku-dev

$ git push heroku-dev develop:master
@oojikoo-gist
oojikoo-gist / routes_concerns.rb
Last active August 29, 2015 14:19
rails: routes concerns
# examples of rails 4 routes concerns
concern :commentable do
resources :comments
end
concern :image_attachable do
resources :images, only: :index
end
resources :messages, concerns: :commentable
@oojikoo-gist
oojikoo-gist / bulletproof_404s_rails.md
Created April 1, 2015 16:16
rails: bulletproof 404s(exception handling)

A step by step guide to bulletproof 404s on Rails

origin: jerodsanto Blog

Step 1: Configure your router as the exceptions app

Since Rails 3 you’ve been able to configure an app to handle exceptions, which you want to point right at your router. To do this, add the following to config/application.rb:

@oojikoo-gist
oojikoo-gist / guard watch_list
Created April 1, 2015 08:11
rails: guard watch list
guard 'rails', :host => "127.0.0.1", :port => '35728' do
watch('Gemfile.lock')
# Rails
watch(%r{app/controllers/.+\.(rb)$})
watch(%r{app/helpers/.+\.rb})
watch(%r{app/mailers/.+\.(rb)$})
watch(%r{app/models/.+\.(rb)$})
watch(%r{app/serializers/.+\.(rb)$})
watch(%r{app/uploaders/.+\.rb})
watch(%r{app/views/.+\.(erb|haml|slim)$})
@oojikoo-gist
oojikoo-gist / Giant_queries.md
Created March 31, 2015 14:47
rails: Giant queries

Giant queries loading everything into memory

You need to fix some data so you’ll just iterate through it all and fix it, right?

User.has_purchased(true).each do |customer|
  customer.grant_role(:customer)
end
@oojikoo-gist
oojikoo-gist / catch_json_parse_errors.rb
Created March 30, 2015 10:56
rails: Catching Invalid JSON Parse Errors
class CatchJsonParseErrors
def initialize app
@app = app
end
def call env
begin
@app.call(env)
rescue ActionDispatch::ParamsParser::ParseError => exception
@oojikoo-gist
oojikoo-gist / api_acronym.md
Last active August 29, 2015 14:17
rails: API acronym

easy fix for api-> API

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'API'
end

now You can write as below

@oojikoo-gist
oojikoo-gist / Gemfile_pattern.md
Created March 25, 2015 16:05
rails: Gemfile pattern

PATTERNS IN A RAILS GEMFILE

The majority of the projects used Rails defaults of Coffeescript, Sass, jQuery, and ERB

Only 37% of the projects used Bootstrap. I thought this number would be higher. It feels like every MVP out there starts with bootstrap.

Only 33% used HAML. I thought this number would be higher as well. I’m not a huge fan of HAML, but it seems like it’s the default templating language for most Rails developers I’ve worked with.

Around 1/3 of the projects turned off Turbolinks. I’ve spent a little bit of time using Turbolinks and while I’m not opposed to it, there are a handful of common practices that I was used to that didn’t work with Turbolinks. I suspect this was the case for many teams and just chose to turn it off out of frustration or interest in making progress elsewhere. I’ve definitely done this in the past when I just wanted to get something out the door.

@oojikoo-gist
oojikoo-gist / application_controller.rb
Created March 25, 2015 15:59
rails: devise after_sign_in_path_for
def after_sign_in_path_for(resource)
current_user_path
end