Skip to content

Instantly share code, notes, and snippets.

View paulsturgess's full-sized avatar

Paul Sturgess paulsturgess

View GitHub Profile
@paulsturgess
paulsturgess / service.js
Last active February 2, 2024 17:24
An example Service class wrapper for Axios
import axios from 'axios';
class Service {
constructor() {
let service = axios.create({
headers: {csrf: 'token'}
});
service.interceptors.response.use(this.handleSuccess, this.handleError);
this.service = service;
}
yum install -y libpng
yum install -y libjpeg
yum install -y openssl
yum install -y icu
yum install -y libX11
yum install -y libXext
yum install -y libXrender
yum install -y xorg-x11-fonts-Type1
yum install -y xorg-x11-fonts-75dpi
@paulsturgess
paulsturgess / service_spec.js
Created February 8, 2017 20:51
Test Axios promise with jasmine-ajax
import Service from 'path/to/service';
import 'jasmine-ajax'
describe('Service', () => {
let request, promise;
let instance = Service;
let payload = {foo:'bar'};
let path = '/path';
let callback = jasmine.createSpy('callback');
@paulsturgess
paulsturgess / reset_primary_key.md
Created February 27, 2013 19:49
Reset postgres primary key index using Rails
$ ActiveRecord::Base.connection.reset_pk_sequence!('table_name')

If you need the table names:

$ ActiveRecord::Base.connection.tables

=> ["accounts", "assets", ...]

@paulsturgess
paulsturgess / Todo.test.js
Last active October 8, 2018 11:22
Testing a stateless component
import React from 'react';
import TestUtils from 'react/lib/ReactTestUtils';
import Todo from './Todo';
describe('Todo', () => {
let instance, li;
let Wrapper = React.createClass({
render: function() {
return this.props.children;
}
@paulsturgess
paulsturgess / attr_accessor_dates.md
Created May 2, 2013 08:11
How to set an attr_accessor date via Rails date_select and have Rails handle the multi-attributes automatically.

Your Class:

class YourClass < ActiveRecord::Base
  attr_accessor :some_date
  columns_hash["some_date"] = ActiveRecord::ConnectionAdapters::Column.new("some_date", nil, "date")
end

Your View:

<%= form_for :your_class do |f| %>

@paulsturgess
paulsturgess / actions_spec.js
Last active November 20, 2017 22:30
Test a redux-thunk action
describe('saveResource', () => {
let resource = { id: 5, name: 'foo' };
describe('when the response is a 200', () => {
let dispatch = jasmine.createSpy('dispatch');
beforeEach(() => {
spyOn(Service, 'post').and.callFake(function(url, payload, callback) {
callback(200, {foo: 'bar'})
})
});
@paulsturgess
paulsturgess / Todo.js
Last active June 17, 2017 11:46
Example stateless React Component
import React, { PropTypes } from 'react'
const Todo = ({ onClick, completed, text }) => (
<li
onClick={onClick}
style={{
textDecoration: completed ? 'line-through' : 'none'
}}
>
{text}
@paulsturgess
paulsturgess / test-redux-container-component.js
Last active April 6, 2017 20:03
Test redux container component
import React from 'react';
import TestUtils from 'react/lib/ReactTestUtils';
import actions from '../actions'
import ConnectedAddTodo from './AddTodo.js'
import { Provider } from 'react-redux';
import configureMockStore from 'redux-mock-store';
const store = configureMockStore()({});
describe('AddTodo', () => {
let instance, container;
@paulsturgess
paulsturgess / actions.js
Last active February 8, 2017 20:40
Example redux actions using redux-thunk middleware
import Constants from '../path/to/constants';
import Service from '../path/to/service';
const Actions = {
toggleSomething: (id) => ({
type: Constants.TOGGLE_SOMETHING,
id
}),
resourceSuccessfullySaved: (data) => ({
type: Constants.RESOURCE_SUCCESSFULLY_SAVED,