Skip to content

Instantly share code, notes, and snippets.

View zgid123's full-sized avatar
:electron:
Dead man walking

Dương Tấn Huỳnh Phong zgid123

:electron:
Dead man walking
View GitHub Profile
@zgid123
zgid123 / active_storage_model.rb
Created July 17, 2023 04:22
monkey-patching to apply scope
# frozen_string_literal: true
# rubocop:disable Style/DocumentDynamicEvalDefinition, Naming/PredicateName
# This monkey-patching is for applying scope for association
# example:
# has_many_attached images, -> { order(created_at: :desc) }
module ActiveStorage
class Attached
module Model
extend ActiveSupport::Concern
@zgid123
zgid123 / activestorage.rb
Created July 12, 2023 03:12
Attach existing s3 url for ActiveStorage
# frozen_string_literal: true
# This monkey-patching is for creating records using url
# instead of reupload the file using attach of activestorage
# this is useful for SPA
module ActiveStorage
module BlobParser
def parse_blob(attachable)
key, filename, content_type = parse_url(attachable)
io = URI.parse(attachable).open
  • display top 20 largest folders
  du -ah . | sort -rh | head -20
  • display total volume
 du -hs /path/to/directory
@zgid123
zgid123 / helper.rb
Last active June 27, 2022 04:08
Rspec request controller helper
def do_request(http_method: nil, action: nil, params: {}, **extra_params)
if http_method.blank? || action.blank?
http_method, action = self.class.metadata[:full_description].split[1..2]
action.delete!('#')
end
send(http_method.downcase.to_sym, action.to_sym, params: params.merge(extra_params))
end
=begin
@zgid123
zgid123 / regex-find-comments-for-ruby.md
Last active January 21, 2021 03:40
Regex find all comments for vscode

find all comments except for frozen_string_literal comment

/((([^\w\d:;\t\r\n]\s?)|^\s?)#\s?(?!.*(frozen_string_literal:|rubocop:(disable|enable))).+)|(^=begin(.|\n)+?(?==end)=end)/gm
@zgid123
zgid123 / regex-find-comments-for-js.md
Last active January 28, 2021 02:45
Regex find all comments for vscode

find all comments except for eslint comments

/((([^\w\d:;\t\r\n]\s?)|^\s?)\/\/\s?(?!.*(eslint-disable)).+)|(\/\*((.|\n)((?!(eslint-(disable|enable)))))+?(?=\*\/)\*\/)/gm