Skip to content

Instantly share code, notes, and snippets.

@ilake
ilake / wallet.js
Last active September 28, 2017 08:33
var wallet = artifacts.require("./MyWallet.sol");
var receivedProposal = function (walletInstance, from, to) {
return new Promise((resolve, reject) => {
var event = walletInstance.proposalReceived({from: from, to: to}, {fromBlock: 0, toBlock: "latest"});
event.watch(function(error, response) {
event.stopWatching();
if (error) {

Please note this method is not atomic, it runs first a SELECT, and if there are no results an INSERT is attempted. If there are other threads or processes there is a race condition between both calls and it could be the case that you end up with two similar records.

Whether that is a problem or not depends on the logic of the application, but in the particular case in which rows have a UNIQUE constraint an exception may be raised, just retry:

begin
  CreditAccount.find_or_create_by(user_id: user.id)
rescue ActiveRecord::RecordNotUnique
  retry
end
@ilake
ilake / validaation.rb
Last active May 14, 2017 22:16
Mastering Rails Validations: Contexts
# http://blog.arkency.com/2014/04/mastering-rails-validations-contexts/
# http://blog.arkency.com/2014/05/mastering-rails-validations-objectify/
# The idea was, that it should not be possible to delete user who already took part of some important business activity.
class User < ActiveRecord::Base
has_many :invoices
validate :does_not_have_any_invoice, on: :destroy
def destroy
transaction do
@ilake
ilake / gist:1549611
Created January 2, 2012 06:39
ActionController::Metal and Airbrake
class ApiController < ActionController::Metal
include AbstractController::Logger
include Rails.application.routes.url_helpers
include ActionController::Rendering
include ActionController::Renderers::All
include ActionController::MimeResponds
include AbstractController::Callbacks
include Airbrake::Rails::ControllerMethods
around_filter :rescue_error
# find node name is gd:email and attribute rel value contains work wording
node = doc.xpath("//*[name()='gd:email'][contains(@rel, 'work')]").first
# change email address attribute value
node.attributes["address"].value = value[:address]
# change node content <node>{content is here}</node>
node.content = value
# remove all nodes that name is gd:phoneNumber
> Rails.application.config_for(:database)
=> {"adapter"=>"mysql2", "encoding"=>"utf8", "reconnect"=>false, "database"=>"listia_development", "pool"=>5, "username"=>"listia", "socket"=>"/tmp/mysql.sock"}
> h = Hashie::Mash.new(Rails.application.config_for(:database))
=> #<Hashie::Mash adapter="mysql2" database="listia_development" encoding="utf8" pool=5 reconnect=false socket="/tmp/mysql.sock" username="listia">
> h.adapter
=> "mysql2"
> h.database
=> "listia_development"
# When you use select * for update, other process only could touch the query when it be `committed`
# Query 1
User.transaction do
User.find(1).groups.joins(:owner).lock.each(&:destroy)
end
User.transaction do
# If the groups has same owner, it will wait the query 1
# If not, it will continue
# The user order I want
ids = [1, 3, 5, 9, 6, 2]
indexed_people = Person.find(ids).index_by(&:id)
people_in_order = indexed_people.values_at(*ids)
@ilake
ilake / Money.md
Last active December 25, 2015 08:19
require "i18n" rescue LoadError
module Money::Currency::Loader
  extend self

  DATA_PATH = File.expand_path("../../../../config", __FILE__)

ujs 會利用到data-type 這個keyword data attributes, 所以當你overwriate 掉時, 你會發現送出的request, header 什麼都不吃accept /, 所以無論你return 什麼他都會看不懂, 當然你預期中的的event (ajax:success) 也不會發生, 會跑到ajax:error 去

<%= form_for @task, :remote => true, :data => {:type => 'casue_problem'} %>