Skip to content

Instantly share code, notes, and snippets.

@oojikoo-gist
oojikoo-gist / secure_headers.md
Last active September 14, 2015 14:37
rails: SecureHeaders
  • Content Security Policy (CSP) - Helps detect/prevent XSS, mixed-content, and other classes of attack. CSP 1.1 Specification
  • HTTP Strict Transport Security (HSTS) - Ensures the browser never visits the http version of a website. Protects from SSLStrip/Firesheep attacks. HSTS Specification
  • X-Frame-Options (XFO) - Prevents your content from being framed and potentially clickjacked. X-Frame-Options draft
  • X-XSS-Protection - Cross site scripting heuristic filter for IE/Chrome
  • X-Content-Type-Options - Prevent content type sniffing
  • X-Download-Options - Prevent file downloads opening
  • X-Permitted-Cross-Domain-Policies - Restrict Adobe Flash Player's access to data
@oojikoo-gist
oojikoo-gist / restful_api.md
Created April 12, 2015 19:12
rails: restful api basecontroller

The guide will assume that we are dealing with a pre-existing application that has two models: Album and Artist. An album belongs to an artist and an artist has many albums.

Requirements

This guide is for Rails 4.0.0+ only.

These gems can always be replaced with alternatives, but they will be good for demonstration. Add the following gems to your Gemfile:

@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
@oojikoo-gist
oojikoo-gist / association_callbacks_md
Created April 15, 2015 17:15
rails: association callbacks
# Association Callbacks
Normal callbacks hook into the life cycle of Active Record objects, allowing you to work with those objects at various points. For example, you can use a :before_save callback to cause something to happen just before an object is saved.
Association callbacks are similar to normal callbacks, but they are triggered by events in the life cycle of a collection. There are four available association callbacks:
## avaliable callbacks
- before_add
- after_add
@oojikoo-gist
oojikoo-gist / association_extensions.md
Created April 15, 2015 17:17
rails: association extensions

Association Extensions

You're not limited to the functionality that Rails automatically builds into association proxy objects. You can also extend these objects through anonymous modules, adding new finders, creators, or other methods. For example:

class Customer < ActiveRecord::Base
  has_many :orders do
    def find_by_order_prefix(order_number)
      find_by(region_id: order_number[0..2])
    end
 end
@oojikoo-gist
oojikoo-gist / column_reader.md
Created April 16, 2015 06:32
rails: column_reader

reference: tomafro.net

Read ActiveRecord columns directly from the class

Sometimes you want to read just a single column from a collection of records, without the overhead of instantiating each and every one. You could just execute raw SQL, but it’s a shame to do away with the nice type conversion ActiveRecord provides. It’d also be a pity to get rid of find scoping, amongst other goodness.

module Tomafro::ColumnReader
  def column_reader(column_name, options = {})
    name = options.delete(:as) || column_name.to_s.pluralize
 column = columns_hash[column_name.to_s]
@oojikoo-gist
oojikoo-gist / i18n_language.md
Created April 16, 2015 15:15
rails: i18n language

i18n local language

Gemfile:

gem 'http_accept_language'

config/initializers/locale.rb:

@oojikoo-gist
oojikoo-gist / STI.md
Created April 17, 2015 03:20
rails: STI(Single Table Inheritance)

Single Table inheritance

reference: blog.thirst.co

In a nutshell, STI allows you to create subclasses of a particular database table. Using a single table, you can cast rows to specific objects that extend the base model.

how to create STI relationships in Rails

Lets say we have a model Computer

@oojikoo-gist
oojikoo-gist / Polymorphic_devise.md
Last active August 29, 2015 14:19
rails: Polymorphic Associations in Rails4

Polymorphic Associations in Rails 4

Adding a Polymorphic Association to Devise User Accounts

reference:

Part1: astockwell.com

Part2: astockwell.com

PROBLEM