Skip to content

Instantly share code, notes, and snippets.

View zmajstor's full-sized avatar

Zoran Majstorovic zmajstor

View GitHub Profile
@macek
macek / 20101004063749_create_photos.rb
Created October 4, 2010 22:38
How to save uploaded files to your database in Rails
# db/migrate/20101004063749_create_photos.rb
class CreatePhotos < ActiveRecord::Migration
def self.up
create_table :photos do |t|
t.string :name, :null => false
t.binary :data, :null => false
t.string :filename
t.string :mime_type
t.timestamps
@jphpsf
jphpsf / application.html.erb
Created July 16, 2011 05:48
Reuse of the Rails Admin layout for the whole application
# app/views/layouts/application.html.erb
<% head_style 'styles' %>
<% head_javascript 'application' %>
<%= render :file => 'layouts/rails_admin/main' %>
@crosebrugh
crosebrugh / gist:1128448
Created August 5, 2011 20:34
Using nested_form_for with rails_admin
class Product < ActiveRecord::Base
has_many :material_product_mappings, :dependent => :destroy, :inverse_of => :product
has_many :materials, :through => :material_product_mappings, :autosave => true
#...
rails_admin do
list do
field :material_product_mappings do
@brcosm
brcosm / apns.rb
Created September 13, 2011 02:41
Ruby Apple Push Notification Example
require 'openssl'
require 'socket'
require 'rubygems'
require 'json'
# Takes json as an input and sends data to APNS over SSL
APNS_HOST = 'gateway.sandbox.push.apple.com'
APNS_PORT = 2195
APNS_CERT_FILE_PATH = 'MyCert.pem'
@mdaguete
mdaguete / Delegate.m
Created December 20, 2011 22:23
APNS Util
/**
* This is what you need to add to your applicationDidFinishLaunching
*/
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// Add registration for remote notifications
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
// Clear application badge when app launches
@mitfik
mitfik / ruby_csr_example.rb
Created February 27, 2012 10:13
Ruby example of CSR with openssl
require 'openssl'
def gen_key(name)
key = OpenSSL::PKey::RSA.new 1048
file = File.new(name, "w")
file.write(key)
file.close
end
def get_key(name)
@javan
javan / application_controller.rb
Created November 30, 2013 22:06
Prevent cross-origin js requests
class ApplicationController < ActionController::Base
before_filter :ensure_xhr
private
def ensure_xhr
if request.get? && request.format && (request.format.js? || request.format.json?)
head :forbidden unless request.xhr?
end
end
end
@janko
janko / ldap.rb
Last active August 29, 2015 14:02
Example Faraday integration with the LDAP protocol, with caching to the database.
require "faraday"
module Faraday
class Adapter
class NetLdap < Faraday::Adapter
def call(env)
# LDAP request, and call `save_response(env, status, body, headers)`
@app.call(env)
end
end
@janko
janko / application.rb
Created June 7, 2014 16:57
Custom error pages in Rails
module MyApp
class Application < Rails::Application
# ...
config.exceptions_app = self.routes
config.action_dispatch.rescue_responses.merge!(
"RDS::ResourceNotFound" => :not_found,
)
# ...
end
@bruno-
bruno- / gist:eddd6298deaa4940c52c
Created August 13, 2014 19:05
signing git commits - cheat sheet

Article: http://mikegerwitz.com/papers/git-horror-story

Theory

  • faking other user's commits is easy with --author flag $ git commit --author='Foo Bar <foo@bar.com>' -m 'some commit'

  • signing commits ensures:

    • someone else can't commit as myself
  • I really commited all the commits I sign