Skip to content

Instantly share code, notes, and snippets.

View eliotsykes's full-sized avatar

Eliot Sykes eliotsykes

View GitHub Profile
@eliotsykes
eliotsykes / rails_new_help_output.md
Last active March 31, 2024 17:09
"rails new" options explained

Run rails new --help to view all of the options you can pass to rails new:

$ bin/rails new --help
Usage:
  rails new APP_PATH [options]

Options:
  -r, [--ruby=PATH]                                      # Path to the Ruby binary of your choice
                                                         # Default: /Users/eliot/.rbenv/versions/2.2.0/bin/ruby
require 'rails_helper'
RSpec.describe TodosController, :type => :controller do
describe "GET #index" do
#describe "POST #create" do
#describe "GET #show" do
#describe "PATCH #update" do (or PUT #update)
#describe "DELETE #destroy" do
#describe "GET #new" do
@eliotsykes
eliotsykes / multiline_expressions_in_ruby.md
Last active September 28, 2023 08:03
Multiline expressions in Ruby

How to break long lines up in Ruby

This page lists the options for breaking single-line expressions into multiple lines in Ruby.

Developers and teams need to come to their own decisions about which guideline(s) they prefer (preferences below are just my personal choices and I encourage you to disregard them).

# With trailing parens
x = [1, 2, 3].join(
 '-'
@eliotsykes
eliotsykes / index.html.erb
Last active July 28, 2023 16:29
Testing Rails: defining temporary controller, view and route in feature spec
<% # This file lives at spec/features/views/index.html.erb %>
<h1>My Temporary Page</h1>
@eliotsykes
eliotsykes / git-aware-bash-prompt.md
Last active April 28, 2023 12:18
Git Aware Bash Prompt
@eliotsykes
eliotsykes / DataNukerService.groovy
Created May 10, 2012 08:01
Grails GORM Truncate Tables in H2
package com.jetbootlabs.services
/*
* Truncates H2 tables mapped by the domainClasses variable. Useful for cleaning up test data.
*
* Temporarily disables referential integrity to avoid constraint violation errors when
* deleting records.
*
* Inspired by Luke Daley's blog post on how to do this in MySQL:
* http://ldaley.com/post/398082618/brute-force-fixture-cleanup-in-grails
@eliotsykes
eliotsykes / ngFocusAndBlur.js
Created April 16, 2013 09:27
AngularJS ngFocus and ngBlur directives - one way to get them before they get released. Before using, consider re-naming ngFocus and ngBlur to something that doesn't invade the ng namespace, e.g. replace all 'ngFocus' and 'ngBlur' strings with 'ngcFocus' and 'ngcBlur' (where c = cats/custom).
app.directive('ngFocus', ['$parse', function($parse) {
return function(scope, element, attr) {
var fn = $parse(attr['ngFocus']);
element.bind('focus', function(event) {
scope.$apply(function() {
fn(scope, {$event:event});
});
});
}
}]);
@eliotsykes
eliotsykes / DebugFilters.groovy
Created May 16, 2012 15:20
Grails Debug Filter - log requests
// Put me in grails-app/conf/DebugFilters.groovy
class DebugFilters {
def filters = {
all(controller:'*', action:'*') {
before = {
log.error "${request.method}: $request.forwardURI $request.params"
}
after = { Map model ->
@eliotsykes
eliotsykes / active_job_deserializer_extension.rb
Created September 21, 2022 07:59
ActiveJob compatibility for Rails 5.2 -> 6.0 upgrade
# File: config/initializers/active_job_deserializer_extension.rb
return unless (VERSION::MAJOR == 5 && VERSION::MINOR == 2)
require "active_job/arguments"
module ActiveJobDeserializerExtension
# This copies the hash deserialization from activejob 6.0 so activejob 5.2 is forwards compatible, it can run jobs
# with hash args that were serialized by activejob 6.0. This allows Rails 5.2 and 6.0 to run within the same app
# during the upgrade period.
def deserialize_hash(serialized_hash)
@eliotsykes
eliotsykes / README.md
Last active August 30, 2022 05:46
Techniques for Removing ActiveRecord Callbacks

I certainly have plenty of ar callbacks, wondering what patterns you use to avoid controller bloat or factories for every model?

Sometimes option 1) Extract the callback to its own method and call it:

# File app/models/user.rb
class User < ApplicationRecord

-  after_commit { |user| Mailer.registration_confirmation(user).deliver_later }, on: :create
+  def send_registration_confirmation