Skip to content

Instantly share code, notes, and snippets.

View eliotsykes's full-sized avatar

Eliot Sykes eliotsykes

View GitHub Profile
@eliotsykes
eliotsykes / base_scopes_sample.diff
Last active December 15, 2015 10:24
Rails BaseScopes and subclasses for tidying up excessive SQL scopes in models
+++ b/app/scopes/base_scopes.rb
+class BaseScopes < SimpleDelegator
+
+ def initialize
+ super self.class.relation
+ end
+
+ def self.method_missing(method_name, *args)
+ method = self.new.method(method_name)
+ method.to_proc
@eliotsykes
eliotsykes / naive_hash.rb
Last active January 6, 2016 16:46
Evolving NaiveHash into RubyHash
# Copy NaiveHash (end of this file) and rename to RubyHash.
#
# Try using RubyHash in irb/pry and store some data in it
# and notice it has problems with remembering all of your data due
# to RubyHash not handling hash collisions:
#
# require_relative 'ruby_hash'
# h = RubyHash.new
# ('a'..'z').each do |char|
# h[char] = char.upcase
@eliotsykes
eliotsykes / audit.rb
Created December 30, 2015 07:00
Add Custom Scopes to Audited Gem
# app/models/audit.rb
Audit = Audited.audit_class
class Audit
scope :foo, -> { where("created_at > 0") }
scope :bar, -> { where("updated_at > 0") }
end
# Using scopes example:
# Audit.foo.bar
@eliotsykes
eliotsykes / deploy
Last active January 4, 2016 20:50
bin/deploy for rails+heroku app
#!/bin/sh
# Create this file in the bin/ directory of your
# Rails app.
# Run this script to deploy the app to Heroku.
set -e
# Push changes to Heroku
@eliotsykes
eliotsykes / cloud9-postgresql-rails-howto.md
Last active December 20, 2021 13:28
How to setup PostgreSQL & Rails on Cloud9

How to setup PostgreSQL & Rails on Cloud9

At time of writing, Cloud9 has PostgreSQL pre-installed, so you won't need to install it yourself. However, its not running by default, so you will need to start it with this command in the terminal:

sudo service postgresql start

Change the PostgreSQL password to 'password' (or choose a different password):

@eliotsykes
eliotsykes / sanitize_sql_array_examples.md
Last active December 16, 2016 09:11
sanitize_sql_array examples

Apps and engines searched can be found at https://github.com/eliotsykes/real-world-rails

$ ag 'sanitize_sql_array'
apps/canvas-lms/app/models/attachment.rb
1169:        clauses << sanitize_sql_array(["(attachments.content_type=?)", type])

apps/canvas-lms/app/models/enrollment/query_builder.rb
141:      ActiveRecord::Base.send :sanitize_sql_array, [sql, *args]
@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
@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 / 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 / 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(); }