This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
| // for the ST3 users who don't have the Default.sublime-theme file (which is actually the default configuration), | |
| // the simplest procedure is: | |
| // 1- Navigate to Sublime Text -> Preferences -> Browse Packages | |
| // 2- Open the User directory | |
| // 3- Create a file named Default.sublime-theme with the following content (modify font.size as required): | |
| [ | |
| { | |
| "class": "sidebar_label", | |
| "color": [0, 0, 0], | |
| "font.size": 16 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Throttle ensures fn runs at most once per delay period (default 1 second). | |
| On the first call, it immediately invokes fn and sets a timeout. | |
| Any subsequent calls during the delay window are ignored. | |
| Once the timeout expires, timeoutId resets to null, allowing the next call through. | |
| This is a "leading-edge" throttle — the function fires right away, then locks out for delay ms. | |
| Note that calls made during the cooldown are silently dropped (not queued), so the last trailing call can be lost. | |
| */ | |
| export function throttle(fn, delay = 1000) { | |
| let timeoutId = null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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...'} %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app/types/br_money_type.rb | |
| class BrMoneyType < ActiveRecord::Type::Decimal | |
| def cast(value) | |
| value.is_a?(String) ? value.clean_money : value | |
| end | |
| def deserialize(value) | |
| value ? value.to_f : value | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
NewerOlder