Skip to content

Instantly share code, notes, and snippets.

View MaxLap's full-sized avatar

Maxime Lapointe MaxLap

View GitHub Profile
@MaxLap
MaxLap / config.rb
Last active May 20, 2024 12:39
Using RubyNext to show negated method calls, such as `foo.!exists?`
class NegatedMethodRewriter < Parser::TreeRewriter
def on_send(node)
receiver_node, method_name, *arg_nodes = *node
if receiver_node && method_name == :! && arg_nodes.size == 1
remove(node.location.selector)
insert_before(node.location.expression, "!(")
insert_after(node.location.expression, ")")
else
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
@MaxLap
MaxLap / gist:4b66607c23c8c00483a13f06acfb6018
Created December 1, 2020 13:34
ActiveRecord `associated` duplicate bug
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
@MaxLap
MaxLap / list_rails_assets_usage.rb
Created September 21, 2020 17:34
Rails 3.2 assets usage report
# This script will list asset files which are used and those which aren't for a Rails 3.2 application.
#
# It works by prefixing/suffixing all (js css less scss sass coffee) files. Then, after having run
# rake assets:precompile, you can run the script again and it will search in public/assets for
# those prefix to know if the file is definetly not used, or if it could be used.
root_dirs = %w(app/assets vendor)
file_endings = %w(js css less scss sass coffee)
file_paths_to_handle = root_dirs.flat_map {|d| file_endings.flat_map {|fe| Dir["#{d}/**/*.#{fe}"] } }.sort
require "benchmark"
require "colorize"
require "http"
require "spec/dsl"
class String
private PRIME_RK = 2097169u32
def index_old(search : String, offset = 0)
offset += size if offset < 0
@MaxLap
MaxLap / rubocop.rb
Last active March 12, 2024 17:15 — forked from skanev/rubocop.rb
A Rubocop wrapper that checks only added/modified code
#!/usr/bin/env ruby
# A sneaky wrapper around Rubocop that allows you to run it only against
# the recent changes, as opposed to the whole project. It lets you
# enforce the style guide for new/modified code only, as opposed to
# having to restyle everything or adding cops incrementally. It relies
# on git to figure out which files to check.
#
# Here are some options you can pass in addition to the ones in rubocop:
#
@MaxLap
MaxLap / haml_helper.rb
Last active December 25, 2015 20:08
Helper to allow passing locales to :coffeescript filters and javascript filters if you wrap your javascript in (function() { ... without having to use interpolation, saving you against slow rendering on Windows servers.
module HamlHelper
def coffee_with_locals locals={}, &block
# This helper will set locals as values inside of the :coffeescript filter that follows.
# Doing this instead of using regular interpolation allows haml to keep a compiled version of the CoffeeScript
# instead of having to compile on every render after the interpolation is applied. On Linux, the impact is
# negligible because V8 is fast, but Windows environments don't have this chance and page rendering can be slowed
# heavily from having to compile CoffeeScript.
block_content = capture_haml do