Skip to content

Instantly share code, notes, and snippets.

View songjiz's full-sized avatar
🧑‍💻

songji zeng songjiz

🧑‍💻
View GitHub Profile
<% 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 %>
@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
require 'base64'
require 'json'
require 'net/https'
require 'openssl'
require 'securerandom'
require 'time'
require 'uri'
module Aliyun
class SMS
@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
@songjiz
songjiz / reset_password_support.rb
Last active April 13, 2020 14:35
stateless token base on ActiveSupport::MessageEncryptor
module ResetPasswordSupport
extend ActiveSupport::Concern
class ResetPasswordToken
include StatelessToken
def self.purpose
:reset_password
end
end
Trix.config.textAttributes.red = {
style: { backgroundColor: "red" }
}
element.editor.activateAttribute("red")
// See available attribute options in:
// https://github.com/basecamp/trix/blob/master/src/trix/config/text_attributes.coffee
// https://github.com/basecamp/trix/blob/master/src/trix/config/block_attributes.coffee

Implementing rich-text mentions with Action Text

  1. Mix ActionText::Attachable into your mentionable person model:

    class Person < ApplicationRecord
      include ActionText::Attachable
    
      # ...

end

@songjiz
songjiz / rails-jsonb-queries
Created March 7, 2019 05:32 — forked from mankind/rails-jsonb-queries
Rails-5 postgresql-9.6 jsonb queries
http://stackoverflow.com/questions/22667401/postgres-json-data-type-rails-query
http://stackoverflow.com/questions/40702813/query-on-postgres-json-array-field-in-rails
#payload: [{"kind"=>"person"}]
Segment.where("payload @> ?", [{kind: "person"}].to_json)
#data: {"interest"=>["music", "movies", "programming"]}
Segment.where("data @> ?", {"interest": ["music", "movies", "programming"]}.to_json)
Segment.where("data #>> '{interest, 1}' = 'movies' ")
Segment.where("jsonb_array_length(data->'interest') > 1")
class ActionDispatch::Routing::Mapper
def draw(routes_name)
instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
end
end
BCX::Application.routes.draw do
draw :api
draw :account
draw :session