Skip to content

Instantly share code, notes, and snippets.

@peterberkenbosch
Created December 17, 2021 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterberkenbosch/081dcfacde0fab59ac56ab0fab5417c5 to your computer and use it in GitHub Desktop.
Save peterberkenbosch/081dcfacde0fab59ac56ab0fab5417c5 to your computer and use it in GitHub Desktop.

Rails 7 Solidus demo

Rails Starter Template

Use this template

(optional) Rebase upstream template repo

Make note of upstream sha when templated: 2add9da (https://cln.sh/ETfqYT) and the first commit sha from templated new repo: 85f49ba (https://cln.sh/dfknP7)

Add upstream git remote:

git remote add upstream git@github.com:peterberkenbosch/rails-starter-template.git

Fetch the upstream commits:

git fetch upstream

And rebase the upstream commits onto the current branch:

git rebase --onto 2add9da 85f49ba main

Then force push to have the full correct history in place.

git push --force-with-lease 

Update from the upstream template

When there are changes made in the rails-starter-template and the above is setup correctly. You can now get the upstream changes into your application like so:

git fetch upstream
git rebase upstream/main

Please note that the more changes you made in your application, the higher the risk to have merge conflicts. Another approach is to cherry pick individual commits from the upstream repository like so:

git fetch upstream
git cherry-pick 632eb36

Gems

Add the following gems to your Gemfile, some dependencies are not yet released with Rails 7 support.

# use forked gems that have not yet been released with rails 7 support:
gem "ransack", github: "activerecord-hackery/ransack"
gem "awesome_nested_set", github: "peterberkenbosch/awesome_nested_set", branch: "rails-7-support"
gem "font-awesome-rails", github: "cseelus/font-awesome-rails",  branch: "rails-7-compatibility"

gem "solidus", github: "peterberkenbosch/solidus", branch: "rails7"

The template comes with a default setup script that you can run to install all gems: (at this point you can also just run bundle install)

bin/setup

Install Solidus

We want to install Solidus without running the migrations since we are using the strong_migrations gem that is blocking the migrations right now. We will fix that right after the installation. Furthermore we will skip the sample and seed data on this step too since those steps are needing a migrated database.

bin/rails g solidus:install --migrate=false --sample=false --seed=false

When the installer asks you if you want to use the solidus_auth_devise extension, just press enter to install this. We definitly want to use this extension:

Solidus has a default authentication extension that uses Devise.
You can find more info at https://github.com/solidusio/solidus_auth_devise.
  Would you like to install it? (Y/n) Y

The installer also prompt you for installing a payment method extension, unfortunatly the default here is not set to none but to paypal. We do not want to use paypal for this demo so type none when asked:

You can select a payment method to be included in the installation process.
  Please select a payment method name: [paypal, none] (paypal) none 

Strong Migrations configuration and migrate.

Before we can run the migrations and neccesary seed files we need to update config/initializers/strong_migrations.rb with the timestamp from the last installed migration from Solidus.

The installed migrations will all have timestamps based on the installation time. To get the correct timestamp grab the last migration file from db/migrate/ and copy the timestamp from the file name.

In this case this is 20211217112861_add_unconfirmed_email_to_spree_users.solidus_auth.rb. So we will have to copy 20211217112861 and add that in our strong_migrations.rb initializer like so:

# Mark existing migrations as safe
StrongMigrations.start_after = 20211217112861

We can now run bin/rails db:migrate to run all the migrations we just installed. If you have committed the migrations in git before running db:migrate there is one caveat. Since we only dump db/schema.rb when there are changes in git on the migrations you might have to dump the schema manually with:

bin/rails db:schema:dump

Setup admin and default seeds

Solidus comes with default seeds that are needed to run the store, and solidus_auth_devise seeds the admin user. To setup the new store you only need to run bin/setup. This will reset the database (among other things) and will prompt you for setting a username and password for the initial store admin user:

Create the admin user (press enter for defaults).
Email [admin@example.com]: 
Password [test123]: 
Done!

Tweaks needed for Rails 7

By default Rails 7 is using :vips for image processing and scaling. Solidus image variants setup is using imagemagick style resize strings. Unfortunatly these are not compatible with each-other.

The error output will be something like this:

web     | Completed 500 Internal Server Error in 1716ms (ActiveRecord: 33.1ms | Allocations: 114755)
web     | 
web     | 
web     |   
web     | ActionView::Template::Error (no implicit conversion of String into Integer):
web     |     1: <% size ||= :mini %>
web     |     2: 
web     |     3: <% if image_url = image.try(:url, size) %>
web     |     4:   <%= image_tag image_url %>
web     |     5: <% else %>
web     |     6:   <span class="image-placeholder <%= size %>"></span>
web     |   
web     | ruby-vips (2.1.4) lib/vips/gvalue.rb:100:in `g_value_set_int'

We will solve this in our app by configuring active_storage to use mini_magick in our application.rb

config.active_storage.variant_processor = :mini_magick

Running all the things.

The rails-starter-application comes with a bin/dev script that will run Overmind by default and starts all the processes from the Procfile.dev file:

web: bin/rails server -p 3000
js: yarn build --watch
css: yarn build:css --watch
sidekiq: bundle exec sidekiq -C config/sidekiq.yml

you can now visit http://localhost:3000

Sample data.

To load the sample data that is provided by Solidus we can run:

bin/rails spree_sample:load

If you want to start with a clean slate again, a simple bin/setup will do just that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment