Skip to content

Instantly share code, notes, and snippets.

View eliotsykes's full-sized avatar

Eliot Sykes eliotsykes

View GitHub Profile
@eliotsykes
eliotsykes / sidekiq_rails_apps.md
Created April 28, 2017 19:23
Open Source Rails apps using Sidekiq
@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 / extract_custom_matcher.md
Last active March 31, 2017 13:05
Extract Custom RSpec Matcher

Original

expect(page).to have_css("h3", text: "Expected text here...")
click_button "Submit"
expect(page).to(
  have_css("h3", text: "Expected text here..."),
  "stay on page on invalid form submit"
)
@eliotsykes
eliotsykes / class_variables.rb
Last active March 16, 2017 11:23
Class Variables shared by all instances
# 1. Create a file class_variables.rb with the contents below
# 2. Run the file with `ruby class_variables.rb` in Terminal
# 3. Review the output and the code below and explore why:
#
# - the class variable @@total_results_count correctly counts the number
# of all Result instances.
#
# - the class variable @@errors does not track the errors for
# a single instance, instead it wrongly tracks *all* errors
# for all Result instances.
@eliotsykes
eliotsykes / results_controller.rb
Last active March 14, 2017 17:39
API Controller method override to use HTTP Basic
# Further defences to consider adding:
# - Restrict access to permitted IP address(es)
# - Use rack-attack to throttle requests and prevent brute force
# attack attempts to determine correct username and password
# - Favor long, random username and password
class Api::V1::ResultsController < Api::ApiController
def update
...
end
@eliotsykes
eliotsykes / thing.rb
Last active March 14, 2017 10:15
ThingsController - Typical Controller Structure
# For database-backed models:
class Thing < ApplicationRecord
validates :name, presence: true
end
# For Plain Old Ruby Object models:
class Thing
include ActiveModel::Model
@eliotsykes
eliotsykes / logger.js
Last active March 13, 2017 10:53
Logging JavaScript client-side to floating div
// Requires jQuery
var logger = {
setup: function() {
$('<div style="position:fixed; top: 0; left: 0; width: 100%; background-color: white;"><pre id="log-output" style="max-height: 70px; font-size: 10px; line-height: 12px;"></pre></div>')
.appendTo('body');
this.logOutput = $('#log-output');
},
info: function(msg) { this.logOutput.prepend(msg + "\n"); },
hide: function() { this.logOutput.parent().hide(); },
show: function() { this.logOutput.parent().show(); }
@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
@eliotsykes
eliotsykes / date_time_formats.rb
Created February 9, 2017 18:39
Real World Rails DATE_FORMAT samples
~/dev/real-world-rails (master)
$ ag -G 'config/initializers/' ::DATE_FORMATS
apps/accelerated_claims/config/initializers/date_formats.rb
1:Date::DATE_FORMATS[:printed] = '%d %B %Y'
apps/advocate-defence-payments/config/initializers/date_time.rb
2:Date::DATE_FORMATS[:default] = Settings.date_format
5:DateTime::DATE_FORMATS[:default] = Settings.date_time_format
8:Time::DATE_FORMATS[:default] = Settings.date_time_format
@eliotsykes
eliotsykes / api_controller.rb
Last active May 10, 2022 03:59
Token Authentication in Rails API Controller and Request Spec
# File: app/controllers/api/api_controller.rb
class Api::ApiController < ActionController::Base
# Consider subclassing ActionController::API instead of Base, see
# http://api.rubyonrails.org/classes/ActionController/API.html
protect_from_forgery with: :null_session
before_action :authenticate
def self.disable_turbolinks_cookies
skip_before_action :set_request_method_cookie