This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module GroupHelper | |
def admin(group) | |
true if group.memberships.admin.where(user_id: current_user.id).present? | |
end | |
def regular(group) | |
true if group.memberships.member.where(user_id: current_user.id).present? | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# | |
# Table name: friendships | |
# | |
# id :integer not null, primary key | |
# user_id :integer | |
# friend_id :integer | |
# created_at :datetime not null | |
# updated_at :datetime not null | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
class RoutesTest < ActionDispatch::IntegrationTest | |
test 'should create new post' do | |
assert_routing({ path: 'posts', method: :post }, { controller: 'posts', | |
action: 'create' }) | |
end | |
test 'should get post' do | |
assert_routing '/posts/1', { controller: 'posts', action: 'show', id: '1' } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######### | |
# PROXY # | |
######### | |
def fahrenheit_to_kelvin(value) | |
(value + 459.67) * 5 / 9 | |
end | |
def kelvin_to_celcius(value) | |
value -= 273.15 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UnitConversion | |
def initialize(measurement, unit) | |
@measurement = measurement | |
@unit = unit | |
end | |
# Proxy with Kelvin | |
def to_kelvin | |
case @unit | |
when 'celcius' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function each(collection, predicate){ | |
if (Array.isArray(collection) || typeof collection === 'string'){ | |
for (var i = 0; i < collection.length; i++){ | |
predicate(collection[i]); | |
} | |
} else if(typeof collection === 'object') { | |
for (var j in collection){ | |
predicate(collection[j]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var transform = function(collection, predicate){ | |
if (Array.isArray(collection)){ | |
return collection.map(predicate); | |
} else if (typeof collection === 'string'){ | |
collection = collection.split(''); | |
return collection.map(predicate).join(''); | |
} else if (typeof collection === 'object'){ | |
return Object.keys(collection).reduce(function(results, property){ | |
results[property] = predicate(collection[property]); | |
return results; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sort = function(arr){ | |
var newArray = []; | |
var copyOfArr = arr; | |
var count = arr.length; | |
while(copyOfArr.length > 0){ | |
var minIndex = copyOfArr.indexOf(Math.min.apply(null, arr)); | |
newArray.push(Number(copyOfArr.splice(minIndex, 1))); | |
} | |
return newArray; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_.memoize = function(func) { | |
var history = {}; | |
/* PSEUDO CODE | |
check if func & arguments in history | |
if they are: look up the stored return value | |
if not: run it with 'once' and store it in 'history' as an object with 3 properties: | |
history = { | |
{ | |
function: ____, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_.memoize = function(func) { | |
var history = {}; | |
var result; | |
return function() { | |
var args = Array.prototype.slice.call(arguments); // to set as object keys | |
var setResult = function(func){ | |
result = func.apply(this, args); | |
}; |