Skip to content

Instantly share code, notes, and snippets.

@eprothro
eprothro / gist:6243211
Last active June 8, 2018 18:57
Foundation Custom Forms Select Capybara Helper

Helper Module

# spec/support/custom_forms_helper.rb
module CustomFormsHelper
  def foundation_select(option, opts={})
    # Zurb Foundation adds custom markup after (and then hides)
    # the originating select. Here we simulate the user's interaction
    # with the custom form instead of just setting the hidden originating select's value
    originating_select_name = opts[:from]
@eprothro
eprothro / spec_boilerplate.rb
Created October 1, 2013 21:28
rspec boilerplate organization
require 'spec_helper'
describe Resource do
let(:resource) { create :resource }
#-------------------------------------#
# ActiveRecord Relationship Behaviors #
#-------------------------------------#
context "with/when some context/event" do
@eprothro
eprothro / Compass Retina Spriting.md
Last active December 27, 2015 10:09
Sass sprite generation with compass for retina and non-retina images (example code for a Rails 3 application).

Overview

The below Sass+Compass/Ruby can be used to:

  • Generate retina and non-retina sprite maps
  • Allow a single style class to provide retina and non-retina support, per image
  • Automatically generate those sprite style classes

Example markup for a retina supported menu button:

%a.icon-hamburger
def select2_select(option, opts={})
# same as foundation_select, but for select2
# boxes used throughout the staff app.
originating_select_name = opts[:from]
originating_select_id = find("select[name='#{originating_select_name}']")['id']
custom_select = find(".select2-container[id$='#{originating_select_id}']")
# click dropdown
custom_select.find("a.select2-choice").click
# click option with correct text
@eprothro
eprothro / gist:7833280
Created December 6, 2013 22:33
image uploader OO JS for use with s3_direct_upload gem

/assets/javascripts/elements/ImageUpload.js

function ImageUpload($el) {
  var self = this;

  self.$el = $el
  self.$image = $(this.$el.attr('data-image-selector'));
  self.$uploadForm = this.$el.find('.s3-uploader')
  self.$uploadTriggers = this.$el.find('.js-upload-image-trigger');
  self.$imageUploader = this.$el.find('#file');
@eprothro
eprothro / gist:ca56e21b7db6842f8b2d
Last active May 12, 2017 14:31
Quick and dirty postgresql query time analysis rake task

Usage

$ rake pg:query_time
total time (min) ▾ | average execution (ms) |            query
--------------------+------------------------+------------------------------
       2.0379       |       1153.5163        | SELECT COUNT(*) FROM "users"  WHERE ("users"."username" ILIKE ?)
       1.3497       |        809.8202        | INSERT INTO users (email, username, auth_token) VALUES (?, ?, ?), ...
       0.0056       |        111.416         | SELECT COUNT(*) FROM "users"  WHERE ("users"."avatar_file_name" ILIKE ?)
(3 rows)
@eprothro
eprothro / heroku_postgresql_config_settings.md
Last active August 29, 2015 14:10
View Heroku Postgres configuration settings

Run PG:PSQL

With pg installed, you can run the pg:psql Heroku command to connect to your database and execute commands using the psql interactive terminal:

$ heroku pg:psql
psql (9.3.5, server 9.3.3)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.
database-name=> |
@eprothro
eprothro / gist:2b2a89fece090d0a1c43
Created February 24, 2015 19:28
Simple ruby memory benchmark
module Benchmark
def memory_usage
`ps -o rss= -p #{Process.pid}`.to_i
end
def memory
m0 = memory_usage
yield
memory_usage - m0
end
def kb
@eprothro
eprothro / gist:214410555f9b198d296b
Last active May 15, 2018 16:44
Enumerators in Ruby | Primer

Enumerators yield for each value in the enumeration

calling next returns the next value, and then raises when there's no values left
e = Enumerator.new do |yielder|
  a = 1
  yielder.yield a
end

e.next
=> 1
@eprothro
eprothro / gist:dc022de04c58291d5430
Last active August 29, 2015 14:16
QueryObject as Decorator for relation
class TestQuery < SimpleDelegator
  def initialize
    super(Bomb.all.extending(Scopes))
  end
  module Scopes
    def processed
      where(Bomb.arel_table[:asset_remote_url].eq(nil))
    end
 end