Carrier Wave and How to Test Uploading a New Image to a ActiveRecord::Base Model

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • forem

    For empowering community 🌱

  • RSpec.describe Organization do context "when changing organization's profile image" do it "updates the cached profile image for each associated article" do # What is going on with this spec? Follow the comments. # # tl;dr - verifying fix for https://github.com/forem/forem/issues/17041 # Create an organization and article, verify the article has cached the initial profile # image of the organization. original_org = create(:organization, profile_image: File.open(Rails.root.join("app/assets/images/1.png"))) article = create(:article, organization: original_org) expect(article.cached_organization.profile_image_url).to eq(original_org.profile_image_url) # For good measure let's have two of these articles create(:article, organization: original_org) # A foible of CarrierWave is that I can't re-upload for the same model in memory. I need to # refind the record. #reload does not work either. (I lost sleep on this portion of the # test) organization = described_class.find(original_org.id) # Verify that the organization's image changes. See the above CarrierWave foible expect do organization.profile_image = File.open(Rails.root.join("app/assets/images/2.png")) organization.save! end.to change { organization.reload.profile_image_url } # I want to collect reloaded versions of the organization's articles so I can see their # cached organization profile image articles_profile_image_urls = organization.articles .map { |art| art.reload.cached_organization.profile_image_url }.uniq # Because of the change `{ organization.reload... }` the organization's profile_image_url is # the most recent change. expect(articles_profile_image_urls).to eq([organization.profile_image_url]) end end end

  • CarrierWave

    Classier solution for file uploads for Rails, Sinatra and other Ruby web frameworks

  • We’re a Rails shop at Forem. In the Forem code base we use the CarrierWave gem to help with our file uploads.

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts