Skip to content

Instantly share code, notes, and snippets.

@yamitake
yamitake / track_events.rb
Created August 15, 2025 06:53
Rails concern for queueing analytics events in flash and rendering them once per tab as dataLayer.push(...) calls guarded by sessionStorage to prevent duplicates.
# app/controllers/concerns/track_events.rb
module TrackEvents
extend ActiveSupport::Concern
included do
helper_method :render_data_layer_events
delegate :tag, to: :view_context, private: true
end
def data_layer_events = flash[:data_layer_events] ||= []
@yamitake
yamitake / html_tag_helper.rb
Created July 11, 2025 07:07
Lightweight HTML tag DSL for Rails views, including support for both standard and self-closing tags.
module HtmlTagHelper
NORMAL_TAGS = %i[
a abbr address article aside audio b bdi bdo blockquote body button canvas caption
cite code colgroup data datalist dd del details dfn dialog div dl dt em fieldset figcaption figure
footer form h1 h2 h3 h4 h5 h6 header hgroup html i iframe ins kbd label legend li main map mark menu
meter nav noscript object ol optgroup option output p picture pre progress q rp rt ruby s samp script
section select small span strong style sub summary sup table tbody td template textarea tfoot th thead
time title tr u ul var video
]
@yamitake
yamitake / launch.json
Created June 10, 2025 00:58
VS Code Debug Configuration for Zenn Preview This launch.json configuration allows you to easily preview your Zenn articles locally within VS Code. It leverages npx zenn preview to start a local development server and automatically opens the preview in your browser when the server is ready. This streamlines the writing and reviewing process for …
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Zenn Preview",
"runtimeExecutable": "npx",
"runtimeArgs": ["zenn", "preview"],
"cwd": "${workspaceFolder}",
@yamitake
yamitake / breadcrumbs_helper.rb
Last active April 14, 2025 05:52
breadcrumbs_key for rails view helper
module BreadcrumbsHelper
def breadcrumb_key = @_breadcrumb_key ||= :"#{controller_path.gsub('/', '_')}_#{ACTION_MAP[action_name] || action_name}"
private
ACTION_MAP = {
"create" => "new",
"update" => "show"
}.freeze
end
@yamitake
yamitake / gtm_helper.rb
Last active April 12, 2025 03:23
Rails用Google Tag Manager ViewHelper
module GtmHelper
GTM_SCRIPT = "(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':" \
"new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0]," \
"j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=" \
"'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);" \
"})(window,document,'script','dataLayer','%s');".freeze
GTM_IFRAME_URL = "https://www.googletagmanager.com/ns.html?id=%s".freeze
def gtm_key = @gtm_key ||= Rails.application.config_for(:gtm)["key"] || "SET_GTM_KEY"
def render_gtm_head = tag.script(GTM_SCRIPT % gtm_key, escape: false)
@yamitake
yamitake / random_open_issue.sh
Created May 21, 2024 13:36
指定したリポジトリからランダムでIssueを開く
#!/bin/bash
REPO="yapr/taskhub"
temp_file=$(mktemp)
gh issue list --repo "$REPO" --state open > "$temp_file"
random_issue=$(shuf -n 1 "$temp_file")
rm "$temp_file"
issue_number=$(echo "$random_issue" | awk '{print $1}')
gh issue view "$issue_number" --repo "$REPO" --web
@yamitake
yamitake / render_async.coffee
Created February 3, 2020 10:13
ex) = render_async related_products_path(@Product)
$(document).one 'turbolinks:load', ->
if document.documentElement.hasAttribute('data-turbolinks-preview')
return
headers = {}
csrfTokenElement = document.querySelector('meta[name="csrf-token"]')
headers['X-CSRF-Token'] = csrfTokenElement.content if csrfTokenElement
$('div[data-render-async-path]').each (_i, div) ->
$.ajax(
url: $(this).data('render-async-path')
method: 'GET'
@yamitake
yamitake / gretel.rb
Last active February 18, 2020 01:45
# frozen_string_literal: true
module Gretel
module ViewHelpers
delegate :breadcrumbs_json_ld, to: :gretel_renderer
end
class Renderer
# rubocop:disable Rails/OutputSafety
def breadcrumbs_json_ld
@yamitake
yamitake / youtube_monky.svg
Last active October 17, 2018 02:06
youtube 500画面ででた猿のSVG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@yamitake
yamitake / git-wip
Last active May 25, 2020 01:31
git wipコマンド
#!/bin/sh
if [ $# -eq 0 ]; then
echo "Usage:"
echo "git wip (branch-name) (issue-number)"
exit 2
fi
issue_number=''
issue_title=''
branch_name=$1
if [ $# -eq 2 ]; then