Skip to content

Instantly share code, notes, and snippets.

View kieraneglin's full-sized avatar
😎

Kieran kieraneglin

😎
  • British Columbia, Canada
View GitHub Profile
<?php
class PostsController {
public function show($params) { // Note that you have to accept an argument here to get param information
$post = Post.find($params['id']); // This param comes from the identifier we set in the route. Note that the `Post.find` is a fictional method
include 'views/posts/show.php';
}
public function index($params) {
@kieraneglin
kieraneglin / router_syntax.php
Last active February 20, 2018 20:18
PHP Router Syntax Example
<?php
$router = new Router();
// Traditional routes
$router->route('POST', '/posts', 'posts#create'); // This looks for PostsController with a method named `create`
$router->route('GET', '/posts', 'posts#index');
$router->route('GET', '/posts/{:id}', 'posts#show'); // This will pass a param with a key of `id` and a value of whatever is in the URL
$router->route('PATCH', '/posts/{:id}', 'posts#update');
$router->route('GET', '/me', 'users#show'); // It's worth noting that the lack of a trailing slash is important. `/me/` will not match `/me` and vice-versa
@kieraneglin
kieraneglin / punchbox_sample.js
Created December 18, 2017 07:34
punchbox sample
class Post { // The name of this class doesn't matter, nor does the filepath
constructor() {
// Code related to this class' setup. Not required for Punchbox to run
}
controller() {
// Code within here will run on every action of the controller
}
index() {
// This code will run on the Posts#index action specifically
}
@kieraneglin
kieraneglin / assert_differences_code.rb
Created December 18, 2017 07:04
assert_differences code example
# Drop into test_helper.rb or similar
def assert_differences(expression_array, by:, message: nil, &block)
expressions = Array(expression_array)
change_by = Array(by).map(&:to_i)
if expressions.size != change_by.size
raise ArgumentError, 'Each expression must have a corresponding value to change by'
end
exps = expressions.map.with_index do |e, i|
@kieraneglin
kieraneglin / new_syntax_example.rb
Last active December 18, 2017 16:55
assert_differences new syntax example
assert_differences(['User.count', 'Post.count'], by: [-1, -5]) do
# Logic...
end
assert_differences('User.count', by: -1) do
# Logic... (although you should be using assert_difference in this case)
end
assert_differences(-> { User.count }, by: -1) do
# I can do lambdas too!
@kieraneglin
kieraneglin / old_syntax_example.rb
Last active December 18, 2017 06:57
assert_differences old syntax
assert_difference('User.count', -1) do
assert_difference('GoodbyeMailerJob.size', +1) do
assert_difference('Post.count', -5) do
# Logic...
end
end
end
@kieraneglin
kieraneglin / README.md
Created June 8, 2017 01:40
js-map-label for Node

Author

Original author is rcknr. I manually transpiled to use JS classes

Usage

See here

Otherwise, put this gist into a file called map-label.js and include with import MapLabel from './map-label'. From there, follow the instructions outlined above.

@kieraneglin
kieraneglin / assert_differences.rb
Created May 27, 2017 04:53
assert_differences
def assert_differences(expression_array, by:, message: nil, &block)
expressions = Array(expression_array)
change_by = Array(by).map(&:to_i)
if expressions.size != change_by.size
raise ArgumentError, 'Each expression must have a corresponding value to change by'
end
exps = expressions.map.with_index do |e, i|
callable = e.respond_to?(:call) ? e : lambda { eval(e, block.binding) }
@kieraneglin
kieraneglin / helper_proxy.rb
Created April 8, 2017 04:18
[Include all Rails helpers] If you need access to helpers, even private ones (for a decorator, say), try this. #tags: ruby, rails, helpers, decorators, liquid
module HelperProxy
HELPER_DIRECTORY = Dir[Rails.root.join('app', 'helpers', '**', '*.rb')]
HELPER_DIRECTORY.each do |file|
include file.split(/\/helpers\/(.*?)\.rb/)[1].classify.safe_constantize
end
end
@kieraneglin
kieraneglin / stop-puma.sh
Created April 6, 2017 21:47
[Stop Puma-dev] #tags: bash, rails, puma
pkill -USR1 puma-dev