Skip to content

Instantly share code, notes, and snippets.

View eliotsykes's full-sized avatar

Eliot Sykes eliotsykes

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / git-aware-bash-prompt.md
Last active May 30, 2025 08:07
Git Aware Bash Prompt
@eliotsykes
eliotsykes / api_controller.rb
Last active October 30, 2015 18:40
Avoid cookies in Rails 4.2 API Controller
class Api::ApiController < ActionController::Base
protect_from_forgery with: :null_session
def self.disable_turbolinks_cookies
skip_before_action :set_request_method_cookie
end
disable_turbolinks_cookies
end
@eliotsykes
eliotsykes / ranges_and_arrays.rb
Created September 27, 2015 15:21
Ranges & Arrays
fruits = ['apples', 'bananas', 'coconuts', 'dates', 'elderberry']
# Array[ ] accepts an integer to lookup by index, this is what we
# do most of the time when we work with arrays:
fruits[0] #=> 'apples'
fruits[2] #=> 'coconuts'
fruits[4] #=> 'elderberry'
# Array[ ] accepts negative integers too. These get special treatment. They
# count backwards from the end of the array:
@eliotsykes
eliotsykes / current-route-query-params-ember-component.js
Last active August 15, 2024 15:15
How to get the current route, queryParams, etc. in an Ember component
// Examples
// Yes, "router.router" twice - this assumes that the router is being injected
// into the component. Otherwise lookup 'router:main'
// One of these will be of interest, figure out which one you want:
this.get('router.router.state');
this.get('router.router.state.params');
this.get('container').lookup('controller:application').currentPath;