Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@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 / rails_api_file_upload.rb
Last active October 26, 2022 05:48
rails: api_file_upload
class ImageUploader < CarrierWave::Uploader::Base
class FilelessIO < StringIO
attr_accessor :original_filename
attr_accessor :content_type
end
before :cache, :convert_base64
def convert_base64(file)
@oojikoo-gist
oojikoo-gist / base_controller.rb
Last active June 25, 2022 23:02
rails: rails_cors
class API::V1::BaseController < ApplicationController
skip_before_filter :verify_authenticity_token
before_filter :cors_preflight_check
after_filter :cors_set_access_control_headers
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
@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 / rspec3_config.md
Created April 17, 2015 20:25
rails: rspec3

Rails 4 with Rspec 3

Configure your Gemfile

# Gemfile
group :development, :test do
  gem 'rspec-rails', '~> 3.0.0'
  gem 'factory_girl_rails'
  gem 'capybara'
@oojikoo-gist
oojikoo-gist / random_location_circle.rb
Created October 14, 2015 19:26
ruby: random_location_circle
def random_location_from(center_lat, center_lng, distance)
radius = distance
radiusInDegrees=radius/111000.to_f
r = radiusInDegrees
origin_x = center_lat
origin_y = center_lng
u = rand(0.00..1.00)
v = rand(0.00..1.00)
@oojikoo-gist
oojikoo-gist / example.webpack.config.js
Created June 22, 2016 09:35
example.webpack.config.js
var path = require('path');
var webpack = require('webpack');
// var fs = require('fs');
var GlobalizePlugin = require('globalize-webpack-plugin');
module.exports = {
name: 'vets4pets',
target: 'web',
version: '0.0.1',
author: 'Chipotle Software (c) 2016',
# Hey Everyone -
# I've been doing some googling here and wanted to ask if anyone's done this before - streamed flash video (*.flv) via Ruby?
# Basically, I'm doing some R&D on a possible future project to use RoR for the site/db access, and Flash to stream video. Thing is, the user needs to be authorized for the video, and it needs to be streamed so it isn't cached on the client-end (for copyright protection purposes - yeah I hate it too, but gotta do it).
# I've seen one tutorial using Ming to do that, but I'd prefer something that has a separate server-side application, and separate client-side application built in Flash, so I have more control over the look and feel of the application, as well as can embed other interesting things in there if I want.
# I've taken a look at Red5 (http://osflash.org/red5), but seeing as it's in Java, I'm not too keen on working with it.
# Part of the reason I want to do this in Ruby is obviously so I can work with it through Rails, -and- so I can use Ruby to fetch videos f
@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: