Rails: How to stub the env in Rails 7+
Rails 7.1
added
Show archive.org snapshot
a new method Rails.env.local?
. If you want to stub the Rails env correctly, use ActiveSupport::EnvironmentInquirer
like this:
# check if the value of stubbed_env is valid
allow(Rails).to receive(:env).and_return(ActiveSupport::EnvironmentInquirer.new(stubbed_env.to_s))
Related cards:
Rails: Example on how to extract domain independent code from the `app/models` folder to the `lib/` folder
This cards describes an example with a Github Client on how to keep your Rails application more maintainable by extracting domain independent code from the app/models
folder to the lib/
folder. The approach is applicable to arbitrary scenarios...
How to add esbuild to the rails asset pipeline
This are the steps I needed to do to add esbuild to an application that used the vanilla rails asset pipeline with sprockets before.
Preparations
- update Sprockets to version 4
- add a
.nvmrc
with your preferred node version (and instal...
Rails: How to get the ordered list of used middlewares
Rails middlewares are small code pieces that wrap requests to the application. The first middleware gets passed the request, invokes the next, and so on. Finally, the application is invoked, builds a response and passes it back to the last middlew...
How to change the hostname in Cucumber features
Capybara uses www.example.com
as the default hostname when making requests.
If your application does something specific on certain hostnames and you want to test this in a feature, you need to tell Capybara to assume a different host.
Given...
RSpec: how to prevent the Rails debug page if you want to actually test for 404s
Within development and test environments, Rails is usually configured to show a detailed debug page instead of 404s. However, there might be some cases where you expect a 404 and want to test for it.
An example would be request-specs that ch...
How to get the hostname of the current machine in Rails or a Ruby script
Use Socket.gethostname
. So for a machine whose hostname is "happycat", it will look like this:
>> Socket.gethostname
=> "happycat"
That should work right away for your Rails application. For plain Ruby, you first need to do:
requi...
3 ways to run Spring (the Rails app preloader) and how to disable it
spring ...
The most obvious way to use spring is to call it explicitly:
spring rails console
spring rake db:migrate
Binstubs
Binstubs are wrapper scripts around executables. In Rails they live inside bin/
. If you...
Rails: How to test the parsed response body
Testing your responses in Rails allows to parse the body depending on the response MIME type with parsed_body
.
get '/posts.json'
response.parsed_bo...
Rails: How to restore a postgres dump from the past
It sometimes happen that a database dump, that would want to insert into your development database, does not match the current schema of the database. This often happens when you have an old dump, but your current setup is up to date with the the ...
How to remove/disable the automatic XSS protection helper html escaping for Rails 3
How to remove/disable the automatic XSS protection helper html escaping for Rails 3.
This is probably a horrible idea.