Skip to content

Instantly share code, notes, and snippets.

View johnnymo87's full-sized avatar

Jonathan Mohrbacher johnnymo87

View GitHub Profile
@johnnymo87
johnnymo87 / URI error
Last active December 21, 2015 08:18
I must have something wrong either with my erb or my route, because I'm getting this crazy suffix on my URI when I press my Merge button. From what I can tell, this suffix is an array with the date the article was created and it's permalink, and I have no idea how it got it there instead of the id.
/admin/content/merge_articles/[2013, "08", "18", "merge-into-me"]
@johnnymo87
johnnymo87 / articles_controller_spec.rb
Created October 6, 2013 17:17
recommended rspec tests if using scaffolding
require 'spec_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
@johnnymo87
johnnymo87 / httpServerPipe
Created November 26, 2013 04:42
I just found out streams can deliver a lot of different kinds of content over the server without much hassle. More info: http://ejohn.org/blog/node-js-stream-playground/
var fs = require('fs');
util = require('util');
/**
* Exercise 1
* Make an HTTP server that serves files. The file path is provided in the URL like this: http://localhost:4000/path/to/my
*/
require('http').createServer(function(req, res) {
var file = '.' + req.url;
@johnnymo87
johnnymo87 / charity_page.feature
Created December 5, 2013 18:38
The browser and cucumber interpret this HTML one way, and rspec interprets it another way. Browser & cucumber: The postcode and email labels still show despite the fact that the organization has nil for these fields. Rspec: The postcode and email don't show in the `rendered` page.
# Snippet from LocalSupport / features / charity_page.feature
Feature: Web page owned by each charity
As a charity worker
So that I can increase my charity's visibility
I want to have a web presence
Tracker story ID: https://www.pivotaltracker.com/story/show/45405153
Background: organizations have been added to database
Given the following organizations exist:
@johnnymo87
johnnymo87 / gist:7857581
Created December 8, 2013 13:35
Regex for validating URLs
From top comment on: http://flanders.co.nz/2009/11/08/a-good-url-regular-expression-repost/
// (?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
//
// $@ matches the entire url
// $1 matches scheme (http, ftp, mailto, ym, mshelp, etc)
// $2 matches authority (host, ftp user:pwd@host, etc)
// $3 matches path
// $4 matches query (http GET REST api, etc)
// $5 matches fragment (html anchor, etc)
@johnnymo87
johnnymo87 / gist:8760346
Last active August 29, 2015 13:55
Pagination: Why does length and count return different values?
# in spec/models/project_spec.rb
describe '#paginate' do
before(:all) { 9.times { FactoryGirl.create(:project) } }
after(:all) { User.delete_all }
it 'returns paginated values' do
Project.paginate(page: 1).should eq Project.first 5
Project.paginate(page: 1).length.should eq 5
Project.paginate(page: 1).count.should eq 9
@johnnymo87
johnnymo87 / user_reports_controller
Created May 7, 2014 17:40
Should I extract this private method into a service?
class UserReportsController < ApplicationController
layout 'full_width', :except => [:invited]
...
def invited
@resend_invitation = true
@invitations = serialize_invitations
render :template => 'user_reports/invited', :layout => 'invitation_table'
end
@johnnymo87
johnnymo87 / gist:eea8f9118af338ac46e6
Last active August 29, 2015 14:01
Deconstructing the framework

Deconstructing the framework, by Gary Bernhardt

Single Responsibility Principle - A class should have one, and only one, reason to change.

A Rails controller's reasons to change:

  • Authentication
  • Authorization
  • Wrap HTTP in both directions (in: params & headers; out: status codes & headers)
  • Manipulate your Active Record models
  • Manipulate your database (user.reload)
@johnnymo87
johnnymo87 / gist:711e1d38e2e5707fda06
Created May 17, 2014 17:38
Finding fixed points
import math.abs
object finding_fixed_points {
val tolerance = 0.0001 //> tolerance : Double = 1.0E-4
def isCloseEnough(x: Double, y: Double) =
abs((x - y) / x) / x < tolerance //> isCloseEnough: (x: Double, y: Double)Boolean
def fixedPoint(f: Double => Double)(firstGuess: Double) = {
def iterate(guess: Double): Double = {
val next = f(guess)
if (isCloseEnough(guess, next)) next
@johnnymo87
johnnymo87 / parens.rb
Last active August 29, 2015 14:01
Exercise 2: Parentheses Balancing
# https://class.coursera.org/progfun-004/assignment/view?assignment_id=4
# Exercise 2: Write a recursive function which verifies the balancing of parentheses in a string
def balance(ary, collected = [])
return collected.length.even? if ary.empty?
char = ary.shift
balance(ary, collected + char.scan(/\(|\)/))
end
require 'minitest/autorun'