Skip to content

Instantly share code, notes, and snippets.

View cheeyeo's full-sized avatar
💭
Researching on use of transformers in computer vision

Chee Yeo cheeyeo

💭
Researching on use of transformers in computer vision
View GitHub Profile
@cheeyeo
cheeyeo / csp.rb
Last active September 19, 2015 20:21
CSP
module GitHub
module CSP
# Public: Constants for CSP keywords
NONE = "'none'".freeze
SELF = "'self'".freeze
UNSAFE_INLINE = "'unsafe-inline'".freeze
UNSAFE_EVAL = "'unsafe-eval'".freeze
# Public: Constants for CSP directive names
BASE_URI = "base-uri".freeze
@cheeyeo
cheeyeo / application.rb
Last active September 16, 2015 23:38
CORS Rails 4+
config.middleware.insert_before 0, "Rack::Cors", debug: true, logger: ->{Rails.logger} do
allow do
origins 'localhost:3000'
resource %r{/users\/?\d*},
:headers => ['Origin', 'Accept', 'Content-Type','Token'],
:methods => [:get, :put, :delete],
:max_age => 0
end
end
@cheeyeo
cheeyeo / Enhance.js
Last active September 15, 2015 11:56 — forked from sebmarkbage/Enhance.js
Higher-order Components
import { Component } from "React";
export var Enhance = ComposedComponent => class extends Component {
constructor() {
this.state = { data: null };
}
componentDidMount() {
this.setState({ data: 'Hello' });
}
render() {
@cheeyeo
cheeyeo / importer.rb
Last active August 29, 2015 14:27 — forked from yellow5/importer.rb
Ruby spec stubbing a block using mocha
class Importer
def parse_csv_file(csv_file)
File.open(csv_file) do |file|
ActiveRecordModel.transaction do
import_csv_stream(file)
end
end
end
end
@cheeyeo
cheeyeo / example.rb
Created August 20, 2015 20:59
ActiveRecord Transactions
ActiveRecord::Base.transaction do
@order.destroy!
@user.save!
end
# calling the bang methods will raise an exception and cause the transaction not to occur if errors exist. calling save and non-bang will not raise
# any errors at all
* Each transaction opens up a new database connection
@cheeyeo
cheeyeo / transactions.markdown
Last active August 29, 2015 14:27 — forked from jcasimir/transactions.markdown
Transactions

Transactions

As your business logic gets complex you may need to implement transactions. The classic example is a bank funds transfer from account A to account B. If the withdrawal from account A fails then the deposit to account B should either never take place or be rolled back.

Basics

All the complexity is handled by ActiveRecord::Transactions. Any model class or instance has a method named .transaction. When called and passed a block, that block will be executed inside a database transaction. If there's an exception raised, the transaction will automatically be rolled back.

Example

@cheeyeo
cheeyeo / error.rb
Created August 19, 2015 22:28
error handling
begin
update_purchase_captured_at!(purchase, captured_at)
rescue Exception => e
Honeybadger.notify(e)
Rails.logger.error(%Q{\
[#{Time.zone.now}][rake purchases:update_captured_at] Error updating \
captured_at for purchase #{purchase.id}: #{e.class}: #{e.message}
This means the funds got captured, but the order will *not* be fulfilled!
@cheeyeo
cheeyeo / gist:7aa4a5cf0db050d6216d
Last active September 13, 2021 21:29 — forked from beanieboi/gist:ad526faf063181f336a2
Codeship Nginx Config for Heroku
daemon off;
# Heroku dynos have at least 4 cores.
worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>;
events {
use epoll;
accept_mutex on;
worker_connections 1024;
}
class User
attr_accessor :name, :pet_name
end
class Post
end
class Factory < BasicObject
def initialize
@attributes = {}