- Ruby
- Git
- Standard
Create a git pre commit hook and give it permission to execute:
touch .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit| import { Controller } from "@hotwired/stimulus" | |
| /* | |
| Example usage: | |
| <div data-controller="form-submit-disabler" data-form-submit-disabler-disable-with-value="Creating..." | |
| <form data-form-submit-disabler-target="form"> | |
| <button data-form-submit-disabler-target="submitButton">Create</button> | |
| </form> |
| module QueryTrackable | |
| extend ActiveSupport::Concern | |
| # | |
| # Prints the number of queries and the time spent in milliseconds | |
| # Useful to track the performance of isolated classes | |
| # | |
| # Usage: | |
| # | |
| # class MyClass |
| import { Controller } from '@hotwired/stimulus' | |
| /* | |
| Disable a button when it is clicked | |
| Ps: Rails 7.0 has dropped UJS, so `data-disable-with` is no longer available | |
| Example usage: | |
| <%= button_to 'Confirm', foo_path, method: :post, class: "btn btn-warning", data: {controller: 'disable-with', 'disable-with-content-value' => 'Confirming...'} %> |
| # frozen_string_literal: true | |
| # put this file at .rubocop/cop/naming/short_variable_name.rb | |
| # then ensure you have the following in .rubocop.yml: | |
| # | |
| # require: | |
| # - .rubocop/cop/naming/short_variable_name.rb | |
| # | |
| # Naming/ShortVariableName: | |
| # Enabled: true |
| # frozen_string_literal: true | |
| # put this file at .rubocop/cop/rspec/redundant_require_rails_helper.rb | |
| # then ensure you have the following in .rubocop.yml: | |
| # | |
| # require: | |
| # - .rubocop/cop/rspec/redundant_require_rails_helper.rb | |
| # | |
| # RSpec/RedundantRequireRailsHelper: | |
| # Enabled: true |
| # app/jobs/job_debouncer.rb | |
| module JobDebouncer | |
| extend ActiveSupport::Concern | |
| class_methods do | |
| def perform_later_once(*args, wait_time: 15.seconds) | |
| job_key = "job_debouncer_#{self}_#{args}" | |
| return if Rails.cache.exist?(job_key) |
Create a git pre commit hook and give it permission to execute:
touch .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit| # For more: https://github.com/ruby-concurrency/concurrent-ruby | |
| num_threads = Concurrent.processor_count # or whatever you prefer like 4 | |
| thread_pool = Concurrent::FixedThreadPool.new(num_threads) | |
| products = Product.all | |
| executors = products.map { |product| | |
| Concurrent::Future.execute({executor: thread_pool}) do | |
| p "processing #{product.id}" | |
| end |
| class Array | |
| # Few types of normalizations | |
| # https://developers.google.com/machine-learning/data-prep/transform/normalization | |
| def normalize | |
| return if self.empty? | |
| x_min, x_max = self.minmax | |
| dx = (x_max-x_min).to_f |
| # You just need to set inactivity_timeout = 0.1 (or any other low number except zero). | |
| # It will trigger the request and wait only 0.1 second for the response. You don't need to define the callbacks as well. | |
| # Example code below: | |
| require 'bundler/inline' | |
| gemfile do | |
| source 'https://rubygems.org' | |
| gem "em-http-request" | |
| end |