tl;dr: Always have your attachment path start with :rails_root/storage/#{Rails.env}#{ENV['RAILS_TEST_NUMBER']}/. The directory where you save your Paperclip attachments should not look like this: storage/photos/1...

...storage/test/photos/1/... storage/test/photos/2/... storage/test/attachments/1/... storage/test/attachments/2/... storage/test2/photos/1/... storage/test2/photos/2/... storage/test2/attachments/1/... storage/test2/attachments/2/... In order to implement this, make Rails.env and ENV['RAILS_TEST_NUMBER'] part of your path template: has_attached_file :attachment, :path...

The ActionDispatch module of Rails gives you the helper method flash to access the flash messages in a response. describe PostsController, type: :request do describe 'update' do it 'shows a...

...helps to modify the Accept-Language in tests. Here is an example for a Rails application with Cucumber integration tests: class ApplicationController OVERRIDE_HTTP_ACCEPT_LANGUAGE = nil def accept_language...

github.com

Test one codebase against multiple sets of gem dependency sets (e.g. Rails 4.2, Rails 5.0). Test one codebase against multiple Ruby versions (e.g. Ruby 2.1.8, Ruby...

...possible dependency permutations (Ruby, gem set, database type). Manually exclude incompatible dependency permutations (e.g. Rails 5.0 does not work with Ruby 2.1). Let developers enter their local credentials for MySQL...

...tokens or CSP nonces. We have deprecated Rack::SteadyETag. We instead recommend reconfiguring your Rails app so two requests to the same resource produce the same HTML for a given...

makandra dev

...them, this is fortunately made very easy if you use compass. Simple example for Rails 3+ Let's assume you have two images app/assets/images/layout/blue-button.png, and .../blue-button-hover.png and the following sass...

...not there already, add this to the :assets group in your Gemfile: gem 'compass-rails' gem 'oily_png' Run bundle install. Add a config/compass.rb with the following content:

If your rails application is unable to send mails, it might be useful to debug your settings using the rails console. Here is a snippet that shows the current settings...

...byebug 11.1.3 (the most recent version at time of writing) when using it with Rails 6 and it's new autoloading component, zeitwerk. There already is a issue for that...

...see below) -- but can be a problem, for example for the capture method of Rails. Consider these methods: def stuff puts 'yielding...' yield puts 'yielded.' true end

...have probably seen it before, e.g. a return in an each block. Impact on Rails applications Since this is something that you would have to do yourself, you could just...

Rails includes milliseconds in Time / DateTime objects when rendering them as JSON: JSON.parse(User.last.to_json)['created_at'] #=> "2001-01-01T00:00:00.000+00:00" In RSpec you might want...

github.com

...t like an ActiveSupport::HasWithIndifferentAccess. There are also some issues if you are on Rails < 4.1 and want it to replace #to_json (but you can always just call Oj.dump...

What I did test successfully was the workaround below. Workaround In Rails 4 you can wrap the output of Oj.dump(...) in an escape_json tag to escape...

...OVER() AS full_count FROM (/* some complicated subquery */) posts LIMIT 20 OFFSET 100; In Rails with will_paginate you might use it like this: posts = scope_complicated_scope.select('posts.*, COUNT(*) OVER() AS...

...not doing this. Use form models or vanilla methods instead. The :conditions option for Rails associations cannot take a lambda. This makes it hard to define conditions that must be...

...a typo. It's to prevent Ruby from interpolating the string at compile time. Rails is aware of this hack and will perform interpolation at runtime. See this article for...

.../../../../../../../../../etc/passwd%%0000.html) Someone tries to exploit CVE-2019-5418. If you use the latest Rails (or latest Rails LTS) you're safe. The exact versions that fix this issue are...

...Rails 6.0.0.beta3, 5.2.2.1, 5.1.6.2, 5.0.7.2, 4.2.11.1, Rails LTS 3.2.22.13 and Rails LTS...

...have to run a separate process. # You'll also benefit from code reloading. if Rails.env.development? require 'sidekiq/testing' Sidekiq::Testing.inline!

...groups are a useful RSpec feature. Unfortunately the default directory structure generated by rspec-rails has no obvious place to put them. I recommend storing them like this: spec/models/shared_examples/foo.rb spec/models/shared_examples/bar.rb...

...those shared examples available to all specs, put the following into your spec_helper.rb (for rails 4 in rails_helper.rb), above the RSpec.configure block: Dir[Rails.root.join("spec/models/shared_examples/**/*.rb")].each {|f| require f...

You will need to upgrade to RSpec >= 2 and rspec-rails >= 2 for Rails 3. Here are some hints to get started: In RSpec 2 the executable is rspec, not...

RSpec and rspec-rails have been completely refactored internally. All RSpec classes have been renamed from Spec::Something to RSpec::Something. This also means that every require 'spec/something' must...

makandra Curriculum

Why is it often fine to memoize instance methods? Resources Speeding up Rails with Memoization 4 Simple Memoization Patterns in Ruby (And One Gem)

website_sizer = WebsiteSizer.new website_sizer.size_of('https://makandra.com') # => 27448 website_sizer.size_of('https://railslts.com') # => 2145364 You can use any library to perform the actual HTTP request. The class should...

makandra dev

...fixing errors as they occur: Update gems as listed below, and bundle Boot a Rails console - see below for a list of changes you will probably need Run Specs with...

...constraints as possible. Boot the application in different environements to spot further issues, e.g. rails console staging Gem updates Replace ruby-debug with byebug or pry Replace mysql with mysql2...

makandra dev

Spring is a Rails application preloader. When debugging e.g. the rails gem, you'll be wondering why your raise, puts or debugger debugging statements have no effect. That's because...

...you can only write environment variables that subprocesses will see. For tests with a Rails application (i.e., that call rails or rake or other binstubbed commands), this method may help...

...following project: ruby -v ruby 1.8.7 bundler -v Bundler version 1.13.7 gem -v 1.8.30 rails -v Rails 3.2.22.1 Running specs or features resulted in: uninitialized constant Gem::LOADED_SPECS_MUTEX...

...previous settings described in Maximum version of Rubygems and Bundler for Ruby 1.8.7 and Rails 2.3 (even the rails version was rails 3.2 and not 2.3) seems not to work...

Expiration of Rails sessions By default Rails sessions expire when the user closes her browser window. To change this edit your config/initializers/session_store.rb like this: ActionController::Base.session = { :key => '...', :secret => '...' :expire_after...

} In older Railses the initializer is not available. Set the option in the environment.rb instead: config.action_controller.session = { :key => '...', :secret => '...' :expire_after => 10.years } Expiration of Rails cookies In addition to the...

...want something similar, so we define our own helper object: let :helper do Spec::Rails::Example::HelperExampleGroup::HelperObject.new.tap do |helper| helper.send :extend, LayoutHelper end end it 'should work' helper.title('Hello...

helper.instance_variable_get('@content_for_title').should == 'Hello World' end This applies to Rails 2. Maybe RSpec 2 / Rails 3 are smarter...

...Note.all.preload(:attachments).ids.size # => 8 Note I created a bug report for this in the Rails project: https://github.com/rails/rails/issues/46455