Skip to content

Instantly share code, notes, and snippets.

View songjiz's full-sized avatar
🧑‍💻

songji zeng songjiz

🧑‍💻
View GitHub Profile
import Foundation
enum Environment: String {
case development, staging, production
}
extension Environment {
static var current: Environment {
if isAppStore {
return .production
import * as React from "react";
import { useMousePosition } from "~/hooks/useMousePosition";
/** Component to cover the area between the mouse cursor and the sub-menu, to allow moving cursor to lower parts of sub-menu without the sub-menu disappearing. */
export function MouseSafeArea(props: { parentRef: React.RefObject<HTMLDivElement> }) {
const { x = 0, y = 0, height: h = 0, width: w = 0 } = props.parentRef.current?.getBoundingClientRect() || {};
const [mouseX, mouseY] = useMousePosition();
const positions = { x, y, h, w, mouseX, mouseY };
return (
<div
import { Controller } from "stimulus";
import { get } from "@rails/request.js";
import { PageSnapshot } from "@hotwired/turbo";
export default class extends Controller {
static values = { hoverTime: Number };
connect() {
this.element.addEventListener("mouseover", this.prefetch.bind(this));
this.element.addEventListener("touchstart", this.prefetch.bind(this));
}
@songjiz
songjiz / ticket.rb
Created July 19, 2021 07:41 — forked from dhh/ticket.rb
class Ticket < ActiveRecord::Base
belongs_to :grouper
belongs_to :user
validate :user_cant_be_blacklisted, on: :confirmation
validate :user_cant_double_book, on: :confirmation
validate :grouper_cant_be_full, on: :confirmation
validate :grouper_cant_have_occurred, on: :confirmation
@songjiz
songjiz / pagination_controller.js
Created June 29, 2021 08:26 — forked from dhh/pagination_controller.js
HEY's Stimulus Pagination Controller
/*
ERB template chunk from The Feed's display of emails:
<section class="postings postings--feed-style" id="postings"
data-controller="pagination" data-pagination-root-margin-value="40px">
<%= render partial: "postings/snippet", collection: @page.records, as: :posting, cached: true %>
<%= link_to(spinner_tag, url_for(page: @page.next_param),
class: "pagination-link", data: { pagination_target: "nextPageLink", preload: @page.first? }) unless @page.last? %>
</section>
export default class extends ApplicationController {
print() {
window.print()
}
scrollToTop() {
window.scrollTo({ top: 0, behavior: "smooth" })
}
confirm(event) {
@songjiz
songjiz / puma.service
Created January 16, 2021 12:41
systemd puma service
[Unit]
Description=Puma HTTP Server
After=network.target
# Uncomment for socket activation (see below)
# Requires=puma.socket
[Service]
Type=simple
@songjiz
songjiz / _form.html.erb
Last active August 8, 2023 10:05
Toggle password visibility with stimulus controller
<div class="field" data-controller="toggle-password-visibility" data-toggle-password-visibility-visible-icon-class="mdi-eye-off">
<%= form.label :password, class: 'label'%>
<div class="control">
<div class="field has-addons has-addons-right">
<div class="control is-clearfix has-icons-left has-icons-right">
<span class="icon is-left">
<i class="mdi mdi-onepassword mdi-24px"></i>
</span>
<%= form.password_field :password, class: ['input', { "is-danger": sign_in.errors.key?(:password) }], data: { 'toggle-password-visibility-target': 'password' }, required: true %>
<% if sign_in.errors.key?(:password) %>
<% if object.errors.any? %>
<div id="error_explanation" class="alert alert-danger alert-dismissible animate__animated animate__pulse" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<h4 class="alert-heading">
<%= t "activerecord.errors.template.header", count: object.errors.count, model: object.model_name.human %>
</h4>
<div>
<p><%= t "activerecord.errors.template.body" %></p>
<% heading ||= '' %>
<% dismissible ||= false %>
<% flash.each do |type, msg| %>
<div class="alert alert-<%= type %> alert-dismissible" role="alert">
<% if heading.present? %>
<h4 class="alert-heading"><%= heading %></h4>
<% end %>
<div><%= msg %></div>
<% if dismissible %>