makandra dev
makandracards.com

Rails applications and ruby gems should have a README that gives the reader a quick overview of the project. Its size will vary as projects differ in complexity, but there...

...be written atop the class file. Structure I suggest the following outline for a Rails project's README, using the very readable Markdown language: # App title Describe the whole project...

...to be released when the block ends. Example: RSpec::Mocks.with_temporary_scope do allow(Rails).to receive(:env).and_return('production'.inquiry) puts Rails.env # prints "production" end puts Rails.env # prints...

Accept: application/json This means the client will only understand JSON responses. When a Rails action is done, it will try to render a template for a format that the...

...pages/foo, application/foo with {:locale=>[:de], :formats=>[:json], :handlers=>[:erb, :builder, :haml]} This is because Rails tried to find a template like foo.js.erb but all it found was foo.html.haml (which the...

makandra dev

...ALLOW_REMOTE_DATABASE_URL: 'true' DATABASE_URL: postgres://postgres:postgres@localhost:5432/test PGTZ: 'Europe/Berlin' RAILS_ENV: test TZ: 'Europe/Berlin' strategy: matrix: partition: [ 0, 1, 2, 3 ] # Keep in sync with...

...uses: actions/checkout@v4 - uses: ./.github/actions/setup-node - uses: ./.github/actions/setup-ruby - name: Setup database schema run: bundle exec rails db:create db:schema:load - name: Precompile assets run: bundle exec rails assets:precompile

Rails has always included a scaffold script that generates a default controller implementation for you. Unfortunately that generated controller is unnecessarily verbose. When we take over Rails projects from other...

...We prefer a different approach. We believe that among all the classes in a Rails project, controllers are some of the hardest to test and understand and should receive special...

content_type_1.match(/\Aimage\/(jpeg|png)\z/) # => nil content_type_2.match(/\Aimage\/(jpeg|png)\z/) # => Rails Newer Rails explicitly warns you, when you use ^ and $ in validations with a regular expression...

To allow HTTP 304 responses, Rails offers the fresh_when method for controllers. The most common way is to pass an ActiveRecord instance or scope, and fresh_when will set...

The problem It might seem simple enough to just say: updated_at = Rails.cache.fetch('updated_at_of_expensive_scope') fresh_when last_modified: updated_at The example above calls...

...at the same time have a .where on an included table, two things happen: Rails tries to load all involved records in a huge single query spanning multiple database tables...

...activity.reload.users.ids # => [1, 2, 3, 4] Or you can reset the association cache: activity.users.reset # newer Rails activity.users(true) # old Rails In newer Rails versions you should prefer to use joins and...

github.com

gem 'super_diff' Require it in your spec_helper.rb require 'super_diff/rspec' # For Rails applications you can replace this with 'super_diff/rspec-rails' Customize colors in spec/support/super_diff.rb SuperDiff.configure do |config...

...contain exactly "XXX" ActiveRecord::Base is monkey patched by super_diff/rspec-rails (includes rspec and rails) super_diff/rails (includes active_record and active_support) super_diff/active_record This means that you have...

...you thought it would be, you don't understand how XSS protection works in Rails. Calling html_safe on the joined array will incorrectly bless the complete string as safe...

...string].join(' ').html_safe # will incorrectly render as ' foo bar ' with unescaped tags Good Rails >=3 safe_join([unsafe_string, safe_string], ' ') # will correctly render as '<span>foo...

...for storing time of day in the format hh:mm:ss, neither Ruby nor Rails themselves offer an elegant way to deal with day times. Time and DateTime both handle...

...the threads terminate. This only affects threads that use ActiveRecord. You can rely on Rails' various clean-up mechanisms to release connections, as outlined below. This may cause your application...

...will allow in total. 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...

...field that is handled by Carrierwave uploaders (or maybe any other attachment solution for Rails) in tests allows different approaches. Here is a short summary of the most common methods...

...RSpec looks for fixture files: RSpec.configure do |config| config.file_fixture_path = "spec/custom_directory" end Alternatives: Rails.root.join('spec/fixtures/files/avatar.jpg').open('r') Rails.root.join('spec/fixtures/files/avatar.jpg').read File.open('spec/fixtures/files/avatar.jpg') (might only work if you run the...

...ordered by ID because they need to be able to iterate in batches. Modern Rails will raise an error if you try order yourself. If you are on Rails...

...aware that find calls inside the block are implicitly scoped. This is fixed in Rails...

makandra dev
github.com

...note that some of the functions edge_rider provides have native implementations in newer rails versions. Useful in applications Relation#traverse_association(*names) Edge Rider gives your relations a method...

...These utilities are mostly useful in libraries that need to support multiple versions of Rails. They offer a unified interface across Rails versions. Relation#collect_ids You should not use...

...only list versions that are allowed by your Gemfile requirements (e.g. does not show rails update to 6 if your Gemfile has the line gem 'rails', '~>5.2'). I also experienced...

...Using unguessable URLs. This is fast (because Apache can deliver assets without going through Rails), but less secure. When going with the "unguessable URL" approach, it is possible to somewhat...

...with SecureRandom.hex(32), and also put it as url_signature_secret into your secrets.yml. Rails helper To generate an expiring URL, use the following helper: def sign_public_path(path...

...characters that have a particular meaning in URLs, like & or =. If you are using Rails URL helpers like movies_path(:query => ARBITRARY_STRING_HERE), Rails will take care of the...

When using Rails credentials, you will edit the encrypted credentials for staging or production environments from time to time. To do that you need the secret key which should only...

...to live in :shared_path/config/credentials/:stage.key. If you have a single master.key (e.g. on Rails < 7.2), edit the Capistrano task to find the key at :shared_path/config/master.key instead. Usage

...worldwide by developers looking for help and tips on web development with Ruby on Rails and DevOps. 15 years ago – in 2009 – we wrote our first card. Since then, over...

...in makandra cards We gain the most experience in web development and Ruby on Rails in our day-to-day work, as web development is our largest team and we...

axonflux.com

/^([\w!#$%&'*+-/=?^`{|}~]+.)*[\w!#$%&'*+-/=?^`{|}~]+@((((([a-z0-9]{1}[a-z0-9-]{0,62}[a-z0-9]{1})|[a-z]).)+[a-z]{2...

makandra dev

$ ruby -e "RubyVM::YJIT.enable; puts RubyVM::YJIT.enabled?" true The last variant is what Rails 7.2+ is doing after the boot process with its new default configuration. So if you...

...are on a current Rails version and didn't explicitly turn off YJIT, you are probably already using it. Keep in mind that Rails turns on YJIT after the boot...

Rails offers several methods to manage three types of different cookies along with a session storage for cookies. These are normal, signed and encrypted cookies. By following the happy...

...sparse and only focuses on controller specs, which recommended usage have been limited since Rails 5+ (see "Rails: Support for Rails 5"), this card will summarize some guidance on how...

makandra dev

Getting CSS (and JS) live reloading to work in a esbuild / Rails project is a bit of a hassle, but the following seems to work decently well. We assume that...

...you already use a standard "esbuild in Rails" setup, and have an esbuild watcher running that picks up your source code in app/assets and compiles to public/assets; if not change...