...as_trait block, it will be bound to the trait module, not the class including the trait. While this may seem unproblematic at first glance, it becomes a problem when...
...including traits in multiple classes, especially when using parameterized traits. Example (bad) Consider the following trait and class. module ExampleTrait as_trait do |audience| HELLO = "hello #{audience}" # don't do...
...do so by using Consul, but you may use any other mechanisms. class Power include Consul::Power def initialize(user) @user = user end def signed_in? user.present? end power :direct...
Now we define our custom controller using this power: class CustomDirectUploadsController < ActiveStorage::DirectUploadsController include Consul::Controller current_power { Power.new(Current.user) } require_power_check power :direct_uploads end
...Each platform-specific variant ships pre-built binaries of libxml2, e.g. x86_64-linux includes binaries for 64bit Linux on Intel/AMD. This significantly speeds up installation of the gem, as...
...force_ruby_platform option for the gem. This will download the ruby variant which includes no binaries, only the libxml2 extension source. gem 'nokogiri', force_ruby_platform: true
...small enough to stay fast even on slower mobile connections. Ideally the total size including assets should be < 300 KB on first load (YMMV). Favicon Most mobile devices can add...
...If your web application has shareable content, you can even improve its display by including dynamic meta information with each page. Twitter Cards Facebook's Open Graph. Add these tags...
...belongs_to :current_version, class_name: 'PageVersion' end Now, when you load pages and include their current versions' primary media... Page.includes(:current_version => :primary_medium).to_a ... Rails will produce...
...you add conditions on an association that owns polymorphic associations which you want to include. For example, the following raises an error. Page.includes(:current_version => :primary_medium).order('page_versions.updated_at...
...existing class from your application. A common use case ist testing modules which are included elsewhere. Your module may behave differently depending on the including class, be parameterized (like with...
While you could test that by using a domain-logic class that currently includes your module, said class may evolve at some point and could brake your tests.
SVG files often contain redundant information, like editor metadata or hidden elements. When esbuild handles your static assets, you can...
...is Function.prototype, not Object. Exercise: Multiple inheritance Ruby supports multiple inheritance through modules and include: module Loggable def log(message) let prefix = "[" + self.class.name + "] " puts prefix + message end end
puts "this is foo" end end class Bar < Foo include Loggable def bar puts "this is bar" end end bar = Bar.new bar.foo # prints "this is foo" bar.bar # prints...
...not_change(contract, :attr_2) end end RSpec::Matchers.define_negated_matcher :an_array_excluding, :include describe 'A negated matcher' do it 'can be used in a composed matcher expression' do...
...change { list }.to(an_array_excluding(5)) end end RSpec::Matchers.define_negated_matcher :exclude, :include describe 'A negated matcher' do it 'can be used for creating more expressive and meaningful...
...Let's say you have an Uploader like this: class MyUploader < CarrierWave::Uploader::Base include CarrierWave::MiniMagick # Create different versions of your uploaded files: version :small do process resize_to...
...check the dimensions of the several versions by writing RSpec.describe Movie, type: :model do include CarrierWave::Test::Matchers describe '#poster' do before do @movie = create(:movie) @movie.poster.store!(File.open('spec/fixtures/test_poster.jpg'))
...it needs to skip those children that are #marked_for_destruction?. Otherwise you will include children that have been ticked for deletion in a nested form. Wrong way
...and_store_amount # the critical code 2/2 private def calculate_and_store_amount # may include amounts of items you marked for destruction self.amount = invoice_items.collect(&:amount).sum end end
def sanitize_html self.html = Sanitize.clean(html) end end end # app/models/page.rb class Page < ApplicationRecord include DoesSanitizeHtml end # app/models/template.rb class Template < ApplicationRecord include DoesSanitizeHtml end Testing test trait usage with a...
...published for educational purposes on GitHub. The german bachelor's thesis has also been included for download at the end. This card is roughly structured as follows: 1. Research approach...
Combinations of methods For brevity and concise demonstration of the results I only include a introduction to the coverage based TCP. Coverage based TCP Coverage is a metric to...
...the ones tagged with speed: 'slow' You can use the tags for hooks and includes, too. RSpec.configure do |config| config.before(:each, slow: true) do # Do one thing here end
...the PostgreSQL server doesn't matter here. For example, the official Ubuntu 20.04 sources include only PostgreSQL 12, so your pg_restore version will also be v12. Ubuntu 22.04 includes...
sudo apt install postgresql-client-16 installs only the PG 16 client, including version 16 of pg_restore sudo apt install postgresql-16 installs PG 16 server and...
...your worker explicitly with a sidekiq_retries_exhausted-block, e.g. like this: class DownloadWorker include Sidekiq::Worker # Import jobs are retried a few times without sending exception notifications. # Only after...
...expected and the retries total duration fits. # Example: TestWorker.perform_async('foo', 'bar') class TestWorker include Sidekiq::Worker def perform(*args) raise 'This is a test error. All is well.'
expect(link_element).to have_selector('.nav-link') Info Even though require 'capybara/rspec' includes Capybara::RSpecMatchers by default, such that rendered is converted once one of Capybara's matchers...
...you will might not notice this on your view specs! View helpers are not included by defaulft Helpers used within your view are not included by default. Thus, you may...
...have an indefinitely high resolution). When ordering lists by timestamps, you should therefore always include a final order condition using the primary key of the table. class Photo < ActiveRecord::Base...
...scope :by_date, -> { order('created_at DESC, id DESC') } end Photo.by_date Remember to include the id field in the database index...
...wanted to also have access to Rails validations and reached for this: class Address include ActiveModel::API include ActiveModel::Attributes include ActiveModel::Validations REQUIRED_ATTRIBUTES = %i[street_and_number zip...
...option, you can provide a mapping between the attribute names on Address and the including model. However, using that option suddenly switches the way how Rails constructs your Address instance...
...you manually have to update and push them into your repository. The following card includes an option how you can keep them consistently up to date with no extra effort...
...issues. Even if don't set the options above as a default this section includes some useful practical ways on how to correctly record the logs without side effects.
...do use TypeScript): { "compilerOptions": { "baseUrl": "./app/assets", "paths": { "@/*": ["js/*"], "@images/*": ["images/*"], "@css/*": ["css/*"], "@spec/*": ["../../spec/js/*"] } }, "include": [] } (The include: [] only makes sense if you do not use Typescript. If you leave it...
ActiveRecord gives you the :include option to load records and their associations in a fixed number of queries. This is called preloading or eager loading associations. By preloading associations you...
...problem that slows down a many index view. You might have noticed that using :include randomly seems to do one of the following: Execute one query per involved table with...
RSpec examples can get quite long, especially in feature specs. This makes them hard to read & understand. Also, when executing...
...the filename "([^\"]*)"$/ do |filename| patiently do match = page.all('img').find do |img| img[:src].include?("/#{filename}") end expect(match).to be_present end end Note that the check needs to...
...helper since the DOM might change between page.all('img') and the check img[:src].include?, causing a Selenium::WebDriver::Error::StaleElementReferenceError. If you can express a check in a readable...