Skip to content

Instantly share code, notes, and snippets.

View tangentus's full-sized avatar

Trevor Broaddus tangentus

  • TangentialSolutions, Evidation
  • Tennessee
View GitHub Profile
@tangentus
tangentus / some_view.html.erb
Last active March 31, 2023 19:26
A PoC of a Stimulus ViewComponent
<%= render StimulusComponent.new controller: "test" do |component| %>
<button <%= component.action :click, :test, :test %>>Hello, Stimulus Component!</button>
<% end %>
@tangentus
tangentus / form-submission.js
Last active July 20, 2020 12:24
POST a Form and use Turbolinks to navigate.
const form = document.getElementById("form");
form.addEventListener("click", (e) => {
e.preventDefault();
const formData = new FormData(form);
const csrf = document.querySelector("meta[name='csrf-token']").getAttribute("content");
fetch("/", {
method: "POST",
body: JSON.stringify({order_form: formData}),
headers: {
@tangentus
tangentus / curry.rb
Last active August 8, 2020 00:06
A methodology for currying
anchor = proc do |a|
proc {|b| "#{a} #{b}" }
end
append = proc {|p| p.call("b")}
(anchor >> append).call("a")
anchor.call("a").call("b")
@tangentus
tangentus / fun_with_bindings.rb
Created September 11, 2020 14:15
Fun with Bindings
class Blah
def initialize
@hello = 'world'
end
end
blah = Blah.new
blah_binding = blah.instance_eval { binding }
ERB.new("<%= hello %>").result(blah_binding)
# Result:
# NameError: undefined local variable or method `hello' for #<Blah:0x00007fcb999d2990 @hello="world">
@tangentus
tangentus / array_of_embedded_docs.rb
Created February 4, 2021 01:56
Mongo Ruby Driver bulk updates of embedded documents
## Resources used to create this query
# https://docs.mongodb.com/ruby-driver/current/tutorials/ruby-driver-crud-operations/#updating
# https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/
ProductCollection.with(client: :background) do |klass|
klass.collection.update_many(
{"product_references" => {"$exists" => true}}, # ensure that the `product_references` field exists
{ "$set" => { "product_references.$[elem].listed" => true } }, # says "update all documents matching the array filters below"
{
# this `array_filters` allows you to set conditions on a field that is an array. The "elem" name is arbitrary and only needs to be the same everywhere it is used
@tangentus
tangentus / embedded_document_count.rb
Created February 4, 2021 18:59
Count the number of documents in an embedded array for a collection
SomeCollection.collection.aggregate([
{
"$match": { # This ensures that we only calculate a count where the attribute exists
"some_attribute": {
"$exists" => true
}
}
},
{
"$project": {
@tangentus
tangentus / benchmarkable.rb
Created February 8, 2021 17:16
A Concern to ease benchmarking methods and allow for file output
module Benchmarkable
extend ActiveSupport::Concern
class_methods do
attr_reader :benchmarkable_file_path
def benchmark_to(file_path)
@benchmarkable_file_path = file_path
end
end
@tangentus
tangentus / palindrome.rb
Created April 1, 2021 01:54
Fun with Palindromes
KNOWN_PLANINDROMES = %w(racecar hannah).freeze
def unique_palindrome(books)
palindromes_per_book = {}
books.each do |book_name, book_words|
palindromes_per_book[book_name] = {}
# filter out any unknown palindromes
book_words.split(" ").each do |word|
next unless KNOWN_PLANINDROMES.include?(word)