...What happens when an error occurs? How do we get notified? Understand that each call of a cron job will boot up your Rails application and produce 100% load on...

Sidekiq jobs should never be enqueued in an after_create or after_save callback. Can you find out why? (Hint: You can find a spoiler here)

...provides a database-based model level mutex for MySQL. You use it by simply calling Lock.acquire('string to synchronize on') do # non-threadsafe code end You must make sure the...

...Only one process can enter the block using the same string, so a concurrent call to another Lock.acquire('string to synchronize on') will block, whereas Lock.acquire('some other string') will...

github.com

...before continuing - the event could prevent the default behavior. Luckily there is a concept called passive event listeners which is supported by all modern browsers. Below are the key parts...

...analysis indicates that the majority of touch event handlers on the web never actually call preventDefault(), so browsers often block scrolling unnecessarily. By marking a touch or wheel listener as...

...it_should_act_like from our spec_candy.rb (note the /behave/act/). It allows you to call a standard shared example group with parameters. Some stupid examples follow to show how it...

it_should_act_like 'string equaling another string', :string => 'foo' end If you call it_should_act_like with a block, it will be made available as block:

makandra dev
github.com

...by "pivoting" around a named association. You can traverse multiple associations in a single call. E.g. to turn a relation of posts into a relation of all posts of their...

Relation#collect_ids You should not use relation.collect(&:id) for this because a call like that will instantiate a potentially large number of ActiveRecord objects only to collect its...

...list, 'li', text: 'item 1') up.element.affix(list, 'li', text: 'item 2') Note how every call to up.element.affix() returns a reference to the newly created element. I do not need to...

stackoverflow.com

...it, e.g. with the splat operator: args = [1,2,3] some_method(*args) # Ruby calls args.to_a here Implicit conversion Implicit conversion happens when Ruby tries to convert an object...

...when it thinks it should: a, b, c = args # Ruby calls args.to_ary here # Fictional example; Ruby decomposes the block argument by calling args.to_ary [args].each do |(x, y...

...default route for the DirectUploadsController in this case. This one is automatically loaded when calling require 'active_storage/engine' in your application config. If you leave it there, an attacker could...

...update_rails_disk_service_url which you will need. Another solution would be to call a power that returns false for this default route. Rate limiting Now that the public...

...You can configure the maximum number of connections for each Rails process. This is called the size of your connection pool. The default pool size is 5. You can configure...

...mechanisms above, you can explicitely release the connection from your thread. To do so, call clear_active_connections! before your thread terminates: Thread.new do begin User.first # first database access makes...

api.rubyonrails.org

...evaluates the property the first time it is requested and persists it for subsequent calls. class User has_one_attached :pdf_resume def page_count return unless pdf_resume.attached? blob = pdf_resume.blob...

...It cannot ask Rails to render a view or access the database. Any HTTP calls must be mocked. Learn What do we test with Jasmine? Just as unit tests are...

...sent by the component. Use Jasmine spies to mock the window.fetch() method, inspect its calls and return a promise for a Response...

...updated_at_of_expensive_scope') fresh_when last_modified: updated_at The example above calls fresh_when without an object, so Rails will only set the Last-Modified header and...

# All interactivity (i.e. reading input) goes here end Place tests in lib/scripts/spec and call them my_script_spec.rb. This way they're close to their corresponding script, but not included in...

stackoverflow.com

...map { |x| x.to_i }. However, it is limited to argument-less method invocations. To call a method with an argument, you usually need to use the full block form. A...

...passing arguments to the short block form. (Ruby 2.7+ syntax) class Symbol def with(...) ->(caller, *rest) { caller.send(self, *rest, ...) } end end Using Array#dig and Hash#dig, we can now...

When you query the browser for DOM elements, there are some footguns you should know about. Some lists are synchronized...

...t send a request. The readyState will remain at XMLHttpRequest.UNSENT. } window.fetch = function() { // To prevent callbacks, return a promise that never settles. return new Promise(function() {}) } JS Capybara::Lockstep.synchronize end

...capybara-lockstep in the last line. Include this module in your test suite and call conclude_browser_requests at the end of each test. For example in a Cucumber suite...

...in the user export end end As rake task: spec/lib/tasks/gitlab_spec.rb: describe 'lib/scripts/gitlab.rb' do it 'calls the user export' do Rake::Task['gitlab:user_export'].invoke # Your code for testing e.g...

As plain ruby script: lib/scripts/gitlab_spec.rb: describe 'lib/scripts/gitlab.rb' do let(:script) { Rails.root.join(subject) } it 'calls the user export' do load(script) # Your code for testing e.g. that a file user_export.xlsx...

...several new classes created in one test you could also use returnValues for any call of the method Random.shuffle. callFake If you are testing the class in an integration level...

...to render and modify some html, it may fastly become boilerplate to define every call of the method. Here callFake comes in handy, because it allows you to define the...

When you find similar groups of expect calls in your tests, you can improve readability by extracting the group into its own matcher. RSpec makes this easy by allowing matchers...

...to call other matchers. Example The following test checks that two variables foo and bar (1) have no lowercase characters and (2) end with an exclamation mark: expect(foo).to...

...a Hash in Ruby. Array#to_h with a block (Ruby 2.6+) You can call an array with a block that is called with each element. The block must return...

...initializer will let you create a hash from an array in a single method call. Copy the attached initializer to config/initializers All enumerables (arrays, sets, scopes, etc.) now have an...

...logical NOR (NOT A AND NOT B), which is achieved by chaining multiple where.not calls. The difference in logic alters the scope of excluded records: NAND: Excludes records only if...

...instantiate 1000s of records. Use query_diet. Add database indexes. Don't descend into callback hell. Use form models. Optimize on demand: Don't optimize without knowing what's actually...

...Perhaps you can load that complicated table, or crazy visualization in a separate AJAX call? Move work from the server to the client. On the client The biggest issue on...

Event listeners are called in the order of their registration: button.addEventListener('click', () => console.log("I run first")) button.addEventListener('click', () => console.log("I run second")) Sometimes you want a listener to always run...

...to the document, it travels down from the document to the element. This is called the capturing phase. You can have a listener call in the capture phase by passing...

...Model.reindex(mode: :async, wait: true): /home/a_user/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/searchkick-5.3.1/lib/searchkick/relation_indexer.rb:142:in `block in batch_job': undefined method `call' for nil (NoMethodError) Searchkick.with_redis { |r| r.call("SADD", batches_key, [batch_id]) } ^^^^^ from /home/a_user/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/searchkick-5.3.1/lib/searchkick.rb...