Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / devise_security_fix.rb
Last active August 29, 2015 14:19
rails: devise security
# /config/initializers/devise.rb
config.password_length = 8..128
# user model validates
validate :password_complexity
def password_complexity
if password.present? and not password.match(/\A(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+\z/)
errors.add :password, "must include at least one lowercase letter, one uppercase letter, and one digit"
end
end
@oojikoo-gist
oojikoo-gist / cors_whitelist.md
Created April 12, 2015 12:55
rails: cors whitelist

Cross Origin Resource Sharing

Whitelist in Rails:

Gemfile

gem 'rack-cors', :require => 'rack/cors'

config/application.rb