Skip to content

Instantly share code, notes, and snippets.

class HashSchema
def initialize(schema)
@schema = schema.map { |key, type|
[key, HashSchema.lookup(type)]
}.to_h
end
def cast(params)
params.map { |key, value|
[key, @schema[key] ? @schema[key].cast(value) : value]
@ayamomiji
ayamomiji / list.html
Last active September 18, 2023 16:13
Stimulus example: smart scroll
<div data-controller="smart-scroll"
data-action="smart-scroll:added->smart-scroll#handleAdded
resize->smart-scroll#handleAdded
scroll->smart-scroll#handleScroll">
<div data-controller="smart-scroll-item">
aya: an an
</div>
<div data-controller="smart-scroll-item">
hatate: ni hao
</div>
@ayamomiji
ayamomiji / deploy.rb
Last active October 19, 2021 18:12
capistrano 3: precompile assets on local machine then upload
namespace :deploy do
namespace :assets do
Rake::Task['deploy:assets:precompile'].clear_actions
desc "Precompile assets on local machine and upload them to the server."
task :precompile do
run_locally do
execute 'RAILS_ENV=production bundle exec rake assets:precompile'
end
@ayamomiji
ayamomiji / deploy.rb
Created April 4, 2012 16:52
My capistrano + rainbows configure
# Check if remote file exists
def remote_file_exists?(full_path)
'true' == capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip
end
# Check if process is running
def remote_process_exists?(pid_file)
capture("ps -p $(cat #{pid_file}) ; true").strip.split("\n").size == 2
end
@ayamomiji
ayamomiji / example.html
Created October 16, 2020 02:42
componentWillUnmount/beforeDestroy in alpine.js
<div x-data @remove='console.log($el, "removed")'></div>
@ayamomiji
ayamomiji / listable.rb
Created October 1, 2020 01:14
include module with parameter, a prettier version imo
# adds `acts_as_list` and a `ordered` scope with given scope
# usage:
# include Listable
# include Listable[scope: :user]
module Listable
extend ActiveSupport::Concern
included do
include ListableModule.new
end
@ayamomiji
ayamomiji / gist:4736614
Last active November 10, 2017 11:16
turbolinks + angularjs
bootstrapAngular = ->
$('[ng-app]').each ->
module = $(this).attr('ng-app')
angular.bootstrap(this, [module])
$(document).on('page:load', bootstrapAngular)
@ayamomiji
ayamomiji / config_database.yml
Last active February 13, 2017 09:21
Add emoji support for mysql
default: &default
adapter: mysql2
encoding: utf8mb4
charset: utf8mb4
collation: utf8mb4_unicode_ci
def whatever
blabla
render nothing: true
end
require 'rack/rewrite'
Rails.application.config.middleware.insert(0, Rack::Rewrite) do
rewrite %r{^/(?!assets|auth|admin)}, '/'
end