Skip to content

Instantly share code, notes, and snippets.

View hungmi's full-sized avatar
🏠
Working from home

Hungmi hungmi

🏠
Working from home
View GitHub Profile
@hungmi
hungmi / demo.js
Last active December 18, 2019 08:08
Rails 4 with jquery-ujs: event listeners for remote link js success and error events
//// For Rails projects with rails-ujs Check official guide: https://guides.rubyonrails.org/working_with_javascript_in_rails.html#rails-ujs-event-handlers
// In xxx.html.erb, we can write
// <%= link_to 'xxx', url, remote: true, data: { type: :json, disalbe_with: "...", confirm: "ok?" }, class: 'js-remote-link' %>
$(document).on("ajax:success", ".js-remote-link", function(e) {
var data = event.detail[0]
var status = event.detail[1]
var xhr = event.detail[2]
console.log(data) // {message: "SUCCESS &check;"}
console.log(status) // OK
@hungmi
hungmi / action_text_parser.rb
Last active June 16, 2021 17:39
Rails ActionText: support prism syntax highlight code blocks. And transform <action-text-attachment/> to <img/> to work with any frontend.
# lib/action_text_parser.rb
# require 'action_text_parser' in application.rb
class ActionTextParser
def self.run(action_text_record)
html = action_text_record.body.to_html
nodes = Nokogiri::HTML::DocumentFragment.parse html
nodes = self.parse_attachments(nodes)
nodes = self.parse_code(nodes)
nodes.to_html
end
import TwCitySelector from 'tw-city-selector/dist/tw-city-selector.min.js'
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
connect() {
let controller = this
new TwCitySelector({
el: '.city-selector-set',
elCounty: '.county',
elDistrict: '.district',
@hungmi
hungmi / galleries.html.erb
Last active January 21, 2022 17:17
Asynchronously load images with Stimulus.js. Also supports media queries.
<% # I have a slide contains 6 images. %>
<% # And also I have different versions of them based on media queries. %>
<% # We can load the 2nd to 6th images asynchronously. %>
<% (2..6).each do |num| %>
<div data-controller="preload-img" data-preload-img-src-value='<%= asset_path("slides/pc/#{num}.jpg") %>' data-preload-img-media-value="(min-width: 992px)"></div>
<div data-controller="preload-img" data-preload-img-src-value='<%= asset_path("slides/mobile/#{num}.jpg") %>' data-preload-img-media-value="(max-width: 991px)"></div>
<% end %>
module WebI18nLocale
extend ActiveSupport::Concern
included do
prepend_before_action :digest_locale_params
before_action :set_locale
helper_method :set_locale_path
end
private
@hungmi
hungmi / hello.html
Created May 4, 2024 09:13
Turbo broadcast_*_to via ActionCable
<!-- app/views/hello.html -->
<h1>Hello!</h1>