Skip to content

Instantly share code, notes, and snippets.

@zdennis
zdennis / class_inheritable_accessor_vs_class_attribute_differences.rb
Created May 22, 2019 14:02
Differences with Rails 2.3 class_inheritable_accessor and Rails 3.2+ class_attribute
class Task
class_inheritable_accessor :foo
self.foo = []
class_attribute :bar
self.bar = []
end
class Subtask < Task
end
@zdennis
zdennis / test-heroku-buildpack-versions.sh
Last active April 17, 2019 18:05
Script to find which buildpack version was good starting from a known bad one
#!/bin/bash
PREV=146 # 146 is the currently known bad version
START=147
STOP=149
rails new zachs-sample-rails-app -d postgresql &&
cd zachs-sample-rails-app &&
\rm .git/hooks/pre-commit &&
git add . &&
@zdennis
zdennis / order.service.ts
Created February 21, 2019 22:07
Angular: Service-layer caching based on routing / navigation events
import {
Component,
OnInit,
Input,
OnDestroy,
ViewChild,
} from '@angular/core';
import { Order } from '../../orders/order';
import { Subscription } from 'rxjs';
import { ActivatedRoute, Data } from '@angular/router';
@zdennis
zdennis / order-list-page.component.ts
Last active February 21, 2019 18:27
Ionic + Angular: Re-use resolvers to load route data on cached pages
import {
Component,
OnInit,
Input,
OnDestroy,
ViewChild,
} from '@angular/core';
import { Order } from '../../orders/order';
import { Subscription } from 'rxjs';
import { ActivatedRoute, Data } from '@angular/router';
@zdennis
zdennis / limiting-columns-selected.rb
Created November 28, 2018 18:35
ActiveRecord: Limiting columns selected by default
class SomeModel < ::ActiveRecord::Base
default_scope -> { select('id') } }
end
# only select id
SomeModel.where(name: 'Wibble').all
# override and select all columns
SomeModel.unscoped.where(name: 'Wibble').all
@zdennis
zdennis / rubocop-guard.sh
Created August 15, 2018 18:41
Poor man's rubocop guard/watcher.
compute_hash() {
git s | grep $1 | cut -d" " -f2 | xargs cat | md5
}
HSH=`compute_hash rb`
for i in `seq 1 100000` ; do
NHSH=`compute_hash rb`
if [ "$HSH" != "$NHSH" ] ; then
git s | grep rb | xargs rubocop
else
echo -n '.'
@zdennis
zdennis / 1.rb
Last active June 29, 2018 17:43
Mutually Human Blog Post: Rails 5 API + Angular 5/6 + Capybara Code Snippets Part 3
require 'capybara/rails'
require 'capybara_spa'
require 'selenium/webdriver'
@zdennis
zdennis / browser_logging.rb
Created June 26, 2018 19:11
Chromedriver browser logging for Capybara
# browser_logging.rb will print out the browser log from Chrome
# when running specs with "js: true". This is so we can easily debug
# issues where there are JS errors or other console warnings/error(s), etc.
#
# Output file is: Rails.root/log/browser.log
#
# Note: Nothing will be printed out for non-JS examples.
RSpec.configure do |config|
browser_log = File.new(Rails.root.join("log/browser.log").to_s, "w")
browser_log.sync = true
@zdennis
zdennis / 0_installing_latest_version_of_rails.sh
Last active June 29, 2018 17:43
Mutually Human Blog Post: Rails 5 API + Angular 5/6 + Capybara Code Snippets Part 2
> gem install -r rails -v 5.2.0
Successfully installed rails-5.2.0
1 gem installed
@zdennis
zdennis / find-node-at-position.js
Last active February 1, 2018 18:07
JavaScript spike into finding node at a position within another, and then returning a result that supplies information that can be used to construct a selection/range for moving the cursor
function findNodeAtPosition(node, position) {
const result = { node: undefined, offset: undefined, found: false, currentPosition: 0 };
return findNodeAtPositionWithResult(result, node, position);
}
function findNodeAtPositionWithResult(result, node, position, indent = ' ') {
console.log(indent + 'findNodeAtPositionWithResult: ', result, node, position);
let currentPosition = result.currentPosition;
let childNode;