...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...
...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 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...
...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...
...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...
...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...
...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...
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...
...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...
class SecureCookies COOKIE_SEPARATOR = "\n".freeze def initialize(app) @app = app end def call(env) status, headers, body = @app.call(env) if headers['Set-Cookie'].present? && Rack::Request.new(env).ssl...
...note that when your invoice model is using a before_save or before_validation callback to sum up item totals and store that value in an attribute, you need to...
...totals which returns net, vat and gross totals in a single hash. Code that calls your invoice usually requires all these values together (e. g. to print an invoice), so...
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...
...as keyword arguments. Don't change the syntax, or you'll experience pain. Always call super inside of your overridden #initialize method. A lot of magic things happen in the...
...method stubbing and expectations. If you define initialize with no argument and don't call super, initialize might be hit with this error: ArgumentError: Wrong number of arguments (given...
...High-traffic forms with a high probability of value collision Wrap your entire save call in a Mutex, so the second form submission will report a violated uniqueness validations instead...
# 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...
When you use method_missing to have an object return something on a method call, always make sure you also redefine respond_to_missing?. If you don't do it...
...own) relies on respond_to? (for a good reason). For example #respond_to? will call #respond_to_missing? if the method is not defined within the receiver. When it has...
...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...
...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...
...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...
Rails migrations allow you to use a change method whose calls are automatically inverted for the down path. However, if you need to some path-specific logic (like SQL UPDATE...
...run change and ignore up and down. However, Rails 4+ features a helper method called reversible: class MyMigration < ActiveRecord::Migration def change rename_table :old_table, :new_table add_column...
...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...
...holds a .nvmrc file. The project's readme document offers a bash function which calls nvm use after each cd. In fact, it replaces cd in your bash.
...is also true on some browsers for DELETE, or in general for any AJAX call that is neither GET or POST. Some workarounds for this issue below.
...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...