Skip to content

Instantly share code, notes, and snippets.

View marksiemers's full-sized avatar

Mark marksiemers

  • Measurabl
  • Remote
View GitHub Profile
@marksiemers
marksiemers / request_spec.rb
Created March 24, 2018 01:23
Sample request spec for 404s in RSpec/Rails
describe 'API Request', type: :request do
let!(:request) do
-> { rails_respond_without_detailed_exceptions { get '/bad/path' } }
end
context 'first context, perhaps when a user is signed in' do
before { request.call }
it 'returns http not found (404)' do
expect(response).to have_http_status(:not_found)
require 'benchmark/ips'
class PangramArraySubtraction
def self.pangram?(phrase)
([*'a'..'z'] - phrase.downcase.chars).empty?
end
end
class PangramArrayAllInclude
def self.pangram?(phrase)
@marksiemers
marksiemers / routes.rb
Created September 20, 2017 01:14
Example of nesting routes in rails
Rails.application.routes.draw do
concern :reviewable do
resources :reviews, only: [:new, :create, :index]
end
resources :reviews, only: [:show, :delete, :edit, :update]
resources :artists, :albums, :songs, :lyrics, only: [], concerns: :reviewable
resources :artists do
@marksiemers
marksiemers / js-sort-by-score-with-lodash.js
Last active March 1, 2017 05:59
Scoring objects of arrays based on a "criteria" object
function isArray(obj) {
return (typeof obj !== "undefined" && obj !== null && obj.constructor === Array)
}
function areArrays() {
if (arguments.length === 0){ return null }
for (i = 0; i < arguments.length; i++) {
if (!isArray(arguments[i])) { return false }
}
return true
@marksiemers
marksiemers / js-are-arrays.js
Created March 1, 2017 04:26
Function to check if objects are arrays
function isArray(obj) {
return (typeof obj !== "undefined"
&& obj !== null
&& obj.constructor === Array)
}
function areArrays() {
if (arguments.length === 0){ return null }
for (i = 0; i < arguments.length; i++) {
if (!isArray(arguments[i])) { return false }
@marksiemers
marksiemers / horses_controller.rb
Last active March 1, 2017 04:28
Sinatra create action with ajax and corresponding JS
# Split a horses create action into separate methods for success and failure.
def post_horses_success
if request.xhr?
content_type :json
as_html = erb :'/horses/_horse_list_item', layout: false, locals: { horse: @horse }
{ horse: @horse, as_html: as_html }.to_json
else
redirect "/horses/#{@horse.id}"
end
Tiffany - There is a Sinatra skeleton in the cohort repos, don't use JQuery CDN version 3.
Matt - Helpers directory needs to be directly under app
David - Status code of 200 will always send back string
Jordan - ES2015 has an arrow function
Erik - In JS you can pass an object literal as an argument to a function
Ali - You can store a partial as a variable convert to json and send it with json data
Jack - You can use modules instead of helper methods
Ian - Weirdness with params in authentication?
Brendan - .off in JQuery will remove event binding, not all JS versions support default arguments
Lisa - JQuery script needs to be added before your other JS
@marksiemers
marksiemers / SassMeister-input-HTML.html
Created September 14, 2015 20:33
Generated by SassMeister.com.
<div class="mobile">
<div class="dates mobile">
May 17th
</div>
</div>
<div class="desktop">
<div class="dates">
May 17th
</div>