Skip to content

Instantly share code, notes, and snippets.

@alecguintu
alecguintu / bm-string-contains-any-in-array.rb
Created April 29, 2014 03:33
Check if string contains any substring in an array in Ruby
require 'benchmark'
iterations = 1_000_000
words = 'foo, bar, test, boo'.split(',').map(&:strip)
string = 'this is going foo be awesome~!'
Benchmark.bmbm do |bm|
bm.report do
iterations.times do
words.any? { |s| string.include?(s) }
end
$('ul.links').on 'ajax:success', '.link', (evt, data, status, xhr) ->
console.log 'link clicked'
console.log $(this)
$('#this-form').bind 'ajax:success', (evt, data, status, xhr) ->
console.log 'form submitted'
# # When .link is clicked
# link clicked
# [a.link ...]
@alecguintu
alecguintu / pick_store_remove.js.coffee
Created September 2, 2014 10:40
Filepicker pick -> store -> remove
filepicker.pick picker_options, (pick_fpfile) ->
sanitized_filename = sanitize_filename(pick_fpfile.filename)
filepicker.store pick_fpfile, store_options, (store_fpfile) ->
# Remove files from both fp.io and our root s3 path
filepicker.remove pick_fpfile, {policy: security.policy, signature: security.signature}
@alecguintu
alecguintu / pickMultiple_store_remove.js.coffee
Last active August 29, 2015 14:05
Filepicker pickMultiple -> store -> remove
filepicker.pickMultiple picker_options, (pick_fpfiles) ->
for pick_fpfile, i in pick_fpfiles
sanitized_filename = sanitize_filename(pick_fpfile.filename)
filepicker.store pick_fpfile, store_options, (store_fpfile) ->
# Remove files from both fp.io and our root s3 path
# Here's where the problem happens. pick_fpfile is always the last from the list of pick_fpfiles since filepicker.store is asynchronous and pick_fpfile has already changed by the time the filepicker.remove's success callback has returned
filepicker.remove pick_fpfile, {policy: security.policy, signature: security.signature}
@alecguintu
alecguintu / pickMultiple_store_remove.js.coffee
Last active August 29, 2015 14:05
Filepicker pickMultiple -> store -> remove
filepicker.pickMultiple picker_options, (pick_fpfiles) ->
for pick_fpfile, i in pick_fpfiles
sanitized_filename = sanitize_filename(pick_fpfile.filename)
console.log 'Sending file...'
console.log pick_fpfile
filepicker.store pick_fpfile, store_options, (store_fpfile) ->
console.log 'Callback of file...'
console.log pick_fpfile
@alecguintu
alecguintu / snippet_areYouSure.js
Created September 4, 2014 10:18
Adding page:before-change for turbolinks
if (!settings.silent && !window.aysUnloadSet) {
window.aysUnloadSet = true;
$(window).bind('beforeunload', function() {
$dirtyForms = $("form").filter('.' + settings.dirtyClass);
if ($dirtyForms.length == 0) {
return;
}
// Prevent multiple prompts - seen on Chrome and IE
if (navigator.userAgent.toLowerCase().match(/msie|chrome/)) {
if (window.aysHasPrompted) {
class Shelf < ActiveRecord::Base
has_many :comics
has_many :books#, validate: -> {self.shelf_of == 'book'}
accepts_nested_attributes_for :comics
accepts_nested_attributes_for :books
# has a dropdown on view for shelf_of that could either be 'comic' or 'book
validates :shelf_of, inclusion: { within: %w(comic book) }
end
@alecguintu
alecguintu / gist:fdb8445294f0dada8c50
Last active August 29, 2015 14:20
Showing and hiding child components
Sample 1
_click: function() {
if ($('#add-here').is(':empty'))
React.render(<Child />, $('#add-here')[0]);
else
React.unmountComponentAtNode($('#add-here')[0]);
},
render: function() {
return(
<div>
<%
adyen = Adyen::Client.new
adyen.env = :test
# for basic-auth based implementations
adyen.ws_user = ENV['ADYEN_MERCHANT_LOGIN']
adyen.ws_password = ENV['ADYEN_MERCHANT_PASSWORD']
response = adyen.checkout.payment_methods({
amount: {
payload = {
"Request": {
"Product": {
"PrimaryCategory": "6614",
"Attributes": {
"name": "api create product test sample",
"short_description": "This is a nice product",
"brand": "Remark",
"model": "asdf",
"kid_years": "Kids (6-10yrs)"