Skip to content

Instantly share code, notes, and snippets.

@SofiaSousa
SofiaSousa / deep_compact.rb
Created September 25, 2024 14:02
Ruby Hash `deep_compact` and `deep_compact!`
class Hash
# Returns a new hash with the nil values/key pairs removed recursively.
#
# hash = { a: true, b: { c: [1, 2, 3], d: { e: nil, f: 4 } }, g: nil }
#
# hash.deep_compact # => { a: true, b: { c: [1, 2, 3], d: { f: 4 } } }
def deep_compact
dup.deep_compact!
end
import { Controller } from "@hotwired/stimulus"
export default class ResultsToggle extends Controller {
// ---
// Targets
static targets = ["elementToHide"]
// ---
// Values
static values = { visible: Boolean }
@SofiaSousa
SofiaSousa / index.html.erb
Last active August 9, 2022 08:30
stimulus html 1
<%= turbo_frame_tag "results" do %>
<section data-controller="results-toggle"
data-results-toggle-visible-value="true"
data-results-toggle-hidden-class="hidden">
<button data-action="click->results-toggle#toggle">Hide Results</button>
<div data-results-toggle-target="elementToHide">
<% @items.each do |item| %>
<%= render item %>
@SofiaSousa
SofiaSousa / destroy.turbo_stream.erb
Last active August 9, 2022 08:30
destroy.turbo_stream.erb
<%= turbo_stream.remove "item_1" %>
<%= turbo_stream.update "stats" do %>
<%= render "items/stats" %>
<% end %>
<%= turbo_stream.update "flash", partial: "layouts/flash" %>
@SofiaSousa
SofiaSousa / index.html.erb
Last active August 8, 2022 15:02
index.html.erb
<section>
<%= form_with(url: items_path, method: :get) do |f| %>
...
<%= f.submit "Search" %>
<% end %>
<%= turbo_frame_tag "results" do %>
<% @items.each do |item| %>
<%= render item %>
<% end %>