Skip to content

Instantly share code, notes, and snippets.

View marcusshepp's full-sized avatar
🎯
Focusing

Marcus Shepherd marcusshepp

🎯
Focusing
View GitHub Profile
@marcusshepp
marcusshepp / chown.sh
Last active November 9, 2017 15:32
changing the permissions level of a folder
sudo chown -R $USER ~/projects/kaa-api/log
@marcusshepp
marcusshepp / rails_queries.rb
Last active September 14, 2017 15:23
Rails Queries
# rails 2.3.5
p = Promotion.find(:first, :conditions => ["subdomain = ?", "foobar"])
# to find based on multiple conditions based on multiple associations
User.find(:all, :include => [:contact, :promotion], :conditions => ['contacts.email = ? and promotions.id = ?', 'marcuss@hesonline.com', 22])
# to find last record in old rails apps, rails 2
p = Promotion.find(:all, :order => "id desc", :limit => 1) # newest first
# and in ascending order
p = Promotion.find(:all, :order => "id asc", :limit => 1) # oldest first
@marcusshepp
marcusshepp / promise.js
Last active October 28, 2016 14:01
js ajax promise
function ajax(type, url){
return new Promise(function(resolve, reject){
var http_request = new XMLHttpRequest();
http_request.open(type, "http://127.0.0.1:8000/"+ url);
http_request.onload = function() {
if (http_request.status === 200){
resolve(JSON.parse(http_request.response));
} else {
reject(Error(http_request.statusText));
}
@marcusshepp
marcusshepp / is_empty.js
Last active October 26, 2016 13:23
is object empty
function is_empty(obj){
return Object.keys(obj).length === 0;
}
// alternative
function isEmpty(object) {
for(var i in object) {
return true;
}
return false;
@marcusshepp
marcusshepp / redo.sh
Created October 25, 2016 13:14
How to reset an app with an existing migration in production on postgres. Had a problem where I deployed a project with a new app with an initial migration. Then changed the migration in dev. Then redeployed with a different initial migration.
psql -h 999.999.99.99 -U web_forms web_forms
web_forms=> SELECT * from django_migrations;
web_forms=> DELETE FROM django_migrations WHERE id=28;
web_forms=> DROP TABLE room_change_request_type;
# drop all tables from that app
web_forms=> \q
./manage.py migrate
@marcusshepp
marcusshepp / csrf
Created September 30, 2016 17:13
react django csrf middleware token component + jquery cookie function
django-csrf-token.js
import React from 'react';
export default class DjangoCSRFToken extends React.Component {
render(){
var csrf_token = getCookie('csrftoken');
return (<input type="hidden" name="csrfmiddlewaretoken" value={ csrf_token }/>);
}
@marcusshepp
marcusshepp / webpack-dev-server.md
Last active September 28, 2016 14:09
setting up react and webpack dev server with hot module replacement

package.json

{
  "name": "todo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "./node_modules/.bin/webpack-dev-server --inline --hot",
    "test": "echo \"Error: no test specified\" && exit 1"
@marcusshepp
marcusshepp / width_height.js
Last active September 27, 2016 01:24
vanilla js get width height
var element = document.querySelector("#foo");
var element_details = element.getBoundingClientRect();
var element_width = element_details.width;
var element_height = element_details.height;
@marcusshepp
marcusshepp / append.js
Created September 22, 2016 14:52
vanilla js append to element
appendChild
@marcusshepp
marcusshepp / get selected.js
Created September 22, 2016 14:29
Get selected values from a select element with multiple = 'multiple'
function get_selected_values(select_element){
/*
Vanilla JS way of returning an array of selected elements from a
select element with multiple='mulitple'.
Used to allow for multiple Evaluation Categories to be
selected.
*/
var result = [];
var options = select_element && select_element.options;
var opt;