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...

...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...

...object).to receive(:foo).with('arg1', 'arg2') This expectation would be met with this call: object.foo('arg1', 'arg2') But what if the argument is expected to be an object with...

end Looking at the hidden browser Option 1: Run without headless You can call your tests with NO_HEADLESS=1 bundle exec cucumber to see the Chrome window for...

...subsequent runs: Spec: # spec/models/ / _spec.rb describe ModelName::ExampleApi do describe ' ' do it ' ', :vcr do # calls to API # expectations end end end Generated Cassette: spec/vcr/ / _ /

...in your config e.g. Rails.application.config.fetch(:foobar). You might also want to chain the method calls for nested configs e.g. Rails.application.config.fetch(:foobar).fetch(:bar). This approach allows you to have '', true...

...To fine-tune its config, you may pass an options argument to the svgo.optimize call. Usually, you want to keep the default and specify only a few settings. Do this...