Skip to content

Instantly share code, notes, and snippets.

@dv
dv / .rubocop.yml
Created March 7, 2021 15:58
standard.rb with customisations
# from: https://news.ycombinator.com/item?id=26371997
inherit_from:
- https://raw.githubusercontent.com/testdouble/standard/master/config/base.yml
Style/TrailingCommaInArrayLiteral:
Enabled: true
EnforcedStyleForMultiline: consistent_comma
Style/TrailingCommaInHashLiteral:
Enabled: true
@dv
dv / better_travel_to.rb
Created October 20, 2018 15:02
This circumvents the no-nesting rule of the new `travel_to` in Rails TimeHelpers
# The new `travel_to` does not allow nested calls, and instead throws the following error
# in your face whenever you try to use it:
#
# Calling `travel_to` with a block, when we have previously already made a call to `travel_to`, can lead to confusing time stubbing.
#
# Using this module to override `travel_to` will solve that issue.
#
# Note: not guaranteed to work perfectly
#
module BetterTravelTo
@dv
dv / expose_method.rb
Last active January 23, 2017 12:11
When you really want to test a private method, you can use this simple helper to temporarily expose it for easier access during specs.
require "spec_helper"
RSpec.describe MyClass do
describe "#some_private_method" do
expose_method MyClass, :some_private_method
it "works" do
MyClass.new.some_private_method
end
end
@dv
dv / semi_strong_params.rb
Created December 10, 2015 16:34
For when you're in between mass-assignment and strong-params
module ActiveModel
module MassAssignmentSecurity
# Original source: https://github.com/rails/protected_attributes/blob/0421e825911b05a77c6521171b43070c9e6c4b35/lib/active_model/mass_assignment_security.rb#L350
def sanitize_for_mass_assignment(attributes, role = nil) #:nodoc:
if attributes.respond_to?(:permitted?) && _uses_mass_assignment_security
_mass_assignment_sanitizer.sanitize(self.class, attributes, mass_assignment_authorizer(role))
else
sanitize_forbidden_attributes(attributes)
end
@dv
dv / semi_strong_params.rb
Last active November 25, 2015 10:20
If you're using `protected_attributes` gem and would like to smoothly switch over to `strong_params`, this is a possible useful monkey patch to only use the mass assignment protection when handling controller params.
# Not fully tested, use at own risk!
module ActiveModel
module MassAssignmentSecurity
# Original source: https://github.com/rails/protected_attributes/blob/0421e825911b05a77c6521171b43070c9e6c4b35/lib/active_model/mass_assignment_security.rb#L350
def sanitize_for_mass_assignment(attributes, role = nil) #:nodoc:
if attributes.respond_to?(:permitted?) && _uses_mass_assignment_security
_mass_assignment_sanitizer.sanitize(self.class, attributes, mass_assignment_authorizer(role))
else
@dv
dv / circular_class.rb
Created November 7, 2015 11:55
Expanded Rails 5 Test Case for ActiveSupport::Concern::UnreachableIncludedBlock
class Fixtures::Concern::CircularClass
A = "a"
include Fixtures::Concern::CircularConcern
end
@dv
dv / fizz_buzz_without_if.rb
Last active August 29, 2015 14:21
FizzBuzz without conditionals, answer to a nerdsnipe
# Changes ["Fizz", "Buzz"] into ["FizzBuzz"]
def collapse(hash)
hash.each do |key, value|
hash[key] = [value.join()]
end
end
100.times.each do |i|
result = Hash.new{[]}
@dv
dv / rails_g_and_open_in_sublime.rb
Last active August 29, 2015 14:17
Run rails generator and open generated files in Sublime
#!/usr/bin/ruby
# Alias as follows:
#
# alias rg="~/scripts/rails_g_and_open_in_sublime.rb"
#
# Then use like this:
#
# $ rg model Post
#
@dv
dv / run.rb
Created January 20, 2015 11:48
Forcing nginx into submission (aka running nginx using dynamically compiled config files)
require 'rubygems'
require 'erb'
# Nginx needs its config files to contain absolute paths, so
# we need to compile them to set the correct paths.
def expand(filename)
full_path = File.expand_path(File.dirname(__FILE__)) # "~/Users/david/poetry/sslqsquire/canary"
File.join(full_path, filename)
end
@dv
dv / turbo_turbo.js.coffee
Last active March 2, 2021 04:05
Fix setTimout, setInterval, and global ajax requests when using Turbolinks
# This library fixes common problems with turbolinks
# - Overwrite setTimeout and setInterval to intercept generated ID's
# - Keep track of Ajax requests
#
# When turbolinks' unload event is called, we:
# - Cancel all setTimeouts and setIntervals
# - Abort all still running Ajax requests
$.turboTurbo =