Skip to content

Instantly share code, notes, and snippets.

View eliotsykes's full-sized avatar

Eliot Sykes eliotsykes

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / sidekiq_rails_apps.md
Created April 28, 2017 19:23
Open Source Rails apps using Sidekiq
@eliotsykes
eliotsykes / original_bracket_validator.rb
Last active July 12, 2017 08:59
Ruby Bracket Validator
require 'set'
def is_valid(code)
openers_to_closers = {
'(' => ')',
'{' => '}',
'[' => ']'
}
openers = Set.new(openers_to_closers.keys.to_set)
@eliotsykes
eliotsykes / angularjs-rails-apps.md
Last active August 1, 2017 18:17
AngularJS Rails apps

Open Source AngularJS Rails Apps

Expect some false positives here, but hopefully most of these are open source Rails apps that use (or once used) AngularJS.

If you'd like to help update the list, please comment below with any of these apps you discover do not use AngularJS and include my username (@eliotsykes) in your message.

Confirmed using AngularJS

@eliotsykes
eliotsykes / check_rollbar.rake
Last active July 27, 2017 16:09
Check Rollbar is reporting errors raised in rake tasks
# Create file at lib/tasks/check_rollbar.rake
require 'securerandom'
# Run this with `heroku run rake check_rollbar`
desc "Check Rollbar reports errors raised in Rake tasks"
task check_rollbar: :environment do
unique_msg = "Hello from rake check_rollbar (unique identifier: #{SecureRandom.hex})"
begin
raise ActiveRecord::RecordNotFound, unique_msg
@eliotsykes
eliotsykes / os_rails_apps_with_skylight_gem.md
Created January 22, 2018 19:55
Some open source Rails apps that use/used Skylight gem