Skip to content

Instantly share code, notes, and snippets.

@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 / 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

@oojikoo-gist
oojikoo-gist / block_ie.md
Last active August 29, 2015 14:19
rails: block ie

reference: ruby-journal.com

How to Block Old IE Version With Rails

Install useragent gem by appending to Gemfile:

gem 'useragent'
@oojikoo-gist
oojikoo-gist / association.rb
Created April 15, 2015 16:48
rails: association
# With this declaration, Rails will keep the cache value up to date, and then return that value in response to the size method.
# without contuer_cache option
# asking for the value of @customer.orders.size requires making a call to the database to perform a COUNT(*) query
# To avoid this call, you can add a counter cache to the belonging model:
belongs_to :customer, counter_cache: true
# throught association
class Physician < ActiveRecord::Base
has_many :appointments