...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...
...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...
...is about valid files, e.g. matching extension white- or blacklists. "Processing" is about process calls, e.g. for changing image resolution. "Download" is when CarrierWave loads a file from a remote...
CarrierWave provides CarrierWave::Uploader::Base.clean_cached_files! for that already, but you need to call regularly from your preferred scheduler. For example, when using whenever, you should have something like...
...results prematurely. To prevent this, get into a habit of explicitely loading scopes by calling to_a when you're done filtering in the database. Learn about the N...
To prevent this, add any other expression after the scope: scope = Movie.all; nil Call #to_sql on a scope to see the query it will produce. This does not...
...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...
...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...
...text on https://loremipsum.de/. Write a regular expression that matches a sentence, then call it multiple times. Parse Ruby classes Write a ClassScanner class that parses a .rb file...
...parser as a single giant regex. Instead write individual patterns for methods, accessors, etc. Call each patterns until there are no more matches left. You only need to parse the...
...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...
...a strong understanding of the following language features/concepts: Writing and reading variables Defining and calling functions Control flow: if, for, switch, ... Functions and function pointers Iterating over lists
...Coming from Ruby you should notice that parentheses (()) are not optional in JavaScript function calls. Understand the difference between function pointers (foo) and function invocations (foo()). Notice the parallels between...
...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...