Skip to content

Instantly share code, notes, and snippets.

View songjiz's full-sized avatar
🧑‍💻

songji zeng songjiz

🧑‍💻
View GitHub Profile
@songjiz
songjiz / has_one_time_password.rb
Last active May 26, 2020 08:43
One Time Password concern for ActiveRecord
module HasOneTimePassword
extend ActiveSupport::Concern
included do
class_attribute :otp_secret_salt,
instance_accessor: false,
default: 'otp'
end
module ClassMethods
require 'base64'
require 'json'
require 'net/https'
require 'openssl'
require 'securerandom'
require 'time'
require 'uri'
module Aliyun
class SMS
@songjiz
songjiz / soft_delete.rb
Last active March 31, 2021 03:53
soft delete record
module SoftDelete
extend ActiveSupport::Concern
included do
scope :trashed, -> { where.not(deleted_at: nil) }
scope :not_trashed, -> { where(deleted_at: nil) }
scope :kept, -> { not_trashed }
define_model_callbacks :trash, :untrash
end
<% 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 %>
<% 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>
@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) %>
@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
export default class extends ApplicationController {
print() {
window.print()
}
scrollToTop() {
window.scrollTo({ top: 0, behavior: "smooth" })
}
confirm(event) {
@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>
@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