Skip to content

Instantly share code, notes, and snippets.

@gomo
gomo / README.md
Last active April 16, 2020 04:57
Add change modify key event to document.

Add an event to document that detects when the Shift, Meta (Command), Alt (Option) and Control keys are pressed and released.

If you want to use it in IE, you need to use PollyFill of Object.assign.

I've only tested it with Chrome. Please comment if it doesn't work in other browsers.

@gomo
gomo / data.rb
Last active December 4, 2018 01:35
helper methods and before/after hook for rails task using class inherit
# frozen_string_literal: true
# lib/tasks/data.rb
require 'tasks'
module Tasks
class Data
include ::Tasks
@gomo
gomo / chain_checker.rb
Last active October 4, 2018 04:12
ChainChecker is used when you want to group multiple `expect`.
# frozen_string_literal: true
##
# {ChainChecker} is used when you want to group multiple `expect`.
# You use it including in `RSpec::Matchers.define` block.
#
# RSpec::Matchers.define :have_foo_bar do |foo, bar|
# include ChainChecker
# end
#
@gomo
gomo / _form.html.erb
Created June 12, 2018 02:38
Bootstrap4 form template for rails.
<%# app/view/shared/_form.html.erb %>
<%= form_with model: record, url: url, local: true, class: 'px-4' do |form|%>
<% elements.each do |elem|%>
<div class="form-group form-row mb-5">
<%= form.label elem.label, for: elem.id, class: 'col-md-3 col-lg-2 col-form-label'%>
<div class="col-md-9 col-lg-10">
<% if elem.type?(:collection_radio_buttons, :collection_check_boxes) %>
<div>
<%= form.public_send elem.type, elem.name, *elem.args, include_hidden: false do |check|%>
<%= tag.div class: 'form-check form-check-inline', style: elem.style do%>
@gomo
gomo / recruit.rb
Created June 12, 2018 01:14
Validator for an array with a whitelist.
# app/models/forms/recruit.rb
class Forms::Recruit
include ActiveModel::Model
attr_accessor :job_types
validates :job_types, presence: true, white_list: {list: JOB_TYPES, allow_blank: true}
end
@gomo
gomo / Usage.md
Last active March 14, 2018 08:48
The utility for grouped_collection_select of rails.

In your controller.

users = [
  [1, "Rollam Westerling"],
  [2, "Jeffory Mallister"],
  [3, "Rhaenys Targaryen"],
  [4, "Ulrick Dayne"],
@gomo
gomo / array_length_validator.rb
Last active February 5, 2018 03:41
Array length validator for rails
# app/validators/array_length_validator.rb
class ArrayLengthValidator < ActiveModel::Validations::LengthValidator
def initialize(options)
ActiveModel::Validations::LengthValidator::MESSAGES.each do |key, value|
options[value] = I18n.t("errors.messages.array_#{value}")
end
super(options)
end
end
@gomo
gomo / README.md
Last active July 5, 2017 07:01
How to check checkboxes by values using javascript selenium.

Both getAttribute and isSelected are asynchronous methods, so my solution has become such complicated. Does anyone know a simpler solution?

@gomo
gomo / plane_obj.js
Last active June 29, 2017 02:55
A function in a plane object is the named function in nodejs (v6.10.2).
const obj = {
foobar: function(){}
}
obj.foobar.toString() == (function(){}).toString() //false
obj.foobar.toString() == (function foobar(){}).toString() //true
obj.foobar.toString() //function foobar() {}
@gomo
gomo / test.js
Created June 21, 2017 04:47
Javascript selenium with headless chrome
import webdriver from 'selenium-webdriver'
import test from 'selenium-webdriver/testing'
test.describe('Test', () => {
test.it('with headless chrome.', () => {
const chromeCapabilities = webdriver.Capabilities.chrome();
chromeCapabilities.set('chromeOptions', {
'args': ['--headless', '--disable-gpu']
});
driver = new webdriver.Builder()