Moving rails apps to coolify
Freitag, 03. Januar 2025, 08:46 Uhr | roberto@vasquez-angel.de |Use Dockerfiles as Nixpacks bug out after install ruby (2025-01-03).
My assets don’t get served
Add the env var RAILS_SERVE_STATIC_FILES=true
Use Dockerfiles as Nixpacks bug out after install ruby (2025-01-03).
Add the env var RAILS_SERVE_STATIC_FILES=true
Seems like ubuntu recently updated openssl to v3. This seems to clash with installing older ruby versions.
Fix it by having rvm use its own openssl version:
#> rvm pkg install openssl
#> rvm reinstall ruby-2.7.8 --with-openssl-dir=$HOME/.rvm/usr
See https://github.com/rvm/rvm/issues/5209 for more details.
Resque | ActiveJob | |
Object | PORO | Inherits from ActiveJob::Base |
Entry method | def self.perform | def perform |
Invocation | MyJob.perform | MyJob.perform_now |
Queueing | @queue = :critical | queue_as :critical |
ActiveJob uses the instance method #perform as entry point for the job. So when you are migrating from resque and your job reference other methods in the job, you have to change them from def self.foo to def foo. You now have the advantage that you are working on an instance. So you can have instance methods, variables, etc.
Assume you have built a bunch of services or any other activemodel/activerecord descendant classes. You decide to add translations to your app. Then you realize that you will have to add the translations to you yaml files. First thing that comes into your mind: I’ll make a list of all attributes of those services. The idea is good, but not perfect. You might validate getter methods and not only attributes. So simply listing the attributes of all services will not cut it. What you want is to list all validated attributes/methods in all of your services. In our special case we narrowed it down to presence validations.
Following admittedly long line will list all of your Rao::Service::Base descendants that are in the namespace Blorgh” with all presences validated attributes as yaml, sorted, ready to cut and paste into your locale yaml files:
irb> y Rao::Service::Base.descendants.collect { |d| d.name.start_with?("Blorgh") ? d : nil }.compact.sort { |c, o| c.name <=> o.name }.each_with_object({}) { |k,m| m[k.name.underscore] = k.validators.collect { |v| v.class.name.include?("Presence") ? v.attributes : nil }.flatten.compact.sort.each_with_object({}) { |a,m| m[a] = nil } }
Do not forget to enable eager loading in your development env before doing this or your list might be incomplete.
When you have a gem with an nested namespace like “rao-api-resources_controller” (-> Rao::Api::ResourcesController) you cannot use the for_gem method of zeitwerk. Instead you can do as follows:
# lib/rao/api/resources_controller.rb
loader = Zeitwerk::Loader.new.tap do |loader|
root = Pathname.new(File.expand_path("../../..", __dir__))
loader.push_dir(File.absolute_path(root.join("lib")))
loader.inflector = Class.new(Zeitwerk::Inflector) do
def camelize(basename, abspath)
if abspath.end_with?("rao/api/resources_controller/version.rb")
"VERSION"
else
super
end
end
end.new
loader.setup
loader
end
sudo apt-get update
# Install rvm
sudo apt install curl -y
sudo apt install gnupg2 -y
gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
\curl -sSL https://get.rvm.io | bash -s stable
# Install ruby
rvm install ruby-3.0.0
# Install zsh/oh-my-zsh
Install zsh from this tutorial: https://kifarunix.com/install-and-setup-zsh-and-oh-my-zsh-on-ubuntu-20-04/
# Install vscode
Install vscode on windows from here: https://code.visualstudio.com/Download
# Install the wsl extension when vscode prompts you
# Restart to be able to run code from any ubuntu terminal
# Install postgres
sudo apt install postgresql -y
sudo apt-get install libpq-dev -y
sudo service postgresql start
sudo -u postgres createuser -s $USER
# Install redis
sudo apt install redis-server -y
# Install node
sudo apt install nodejs -y
# Install npm
sudo apt install npm -y
# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
# Install yarn
sudo npm install --global yarn
# Install heroku cli
curl https://cli-assets.heroku.com/install.sh | sh
Rails security guide: https://guides.rubyonrails.org/security.html
Zen rails security checklist: https://github.com/brunofacca/zen-rails-security-checklist
OWASP ruby on rails cheat sheet: https://cheatsheetseries.owasp.org/cheatsheets/Ruby_on_Rails_Cheat_Sheet.html
Bundle audit: https://github.com/rubysec/bundler-audit
The hard way (remove all gems):
$ rvm all-gemsets do gem cleanup
Remove stale source files and archives and other obsolete stuff:
$ rvm cleanup all
To avoid duplicate gems enable the gem cache:
$ rvm gemset globalcache enable
require 'aws-sdk-s3'
s3 = Aws::S3::Client.new(
access_key_id: ENV['ACCESS_KEY_ID'],
secret_access_key: ENV['SECRET_ACCESS_KEY'],
region: 'eu-central-1'
)
s3.list_objects(bucket: 'my-bucket')
class SchemaMigration < ActiveRecord::Base; self.primary_key = :version; end
SchemaMigration.where(version: "20200514202233").first
SchemaMigration.where(version: "20200514202233").update_all(version: "20200514142149")
When you are trying to run rake dummy:app from the rails-dummy gem and you get following message:
Could not find "README.md" in any of your source paths. Your current source paths are:
/home/vagrant/.rvm/gems/ruby-2.4.0@cmor/gems/railties-5.2.0/lib/rails/generators/rails/app/templates
You may be using rails 5.2.0. Upgrading to 5.2.3 or later solves the problem.
If you get following message when deploying to heroku (i.e. after upgraping rails):
command webpacker not found
You may need to run following command locally and commit the changes:
rails webpacker:install
When you run into following error:
'find_spec_for_exe': can't find gem bundler (>= 0.a) with executable bundle (Gem::GemNotFoundException)
It might be that your gem version is too old and does not fit bundler ~> 2.
Try updating gem:
gem update --system '2.7.9'
Yank specific gem version from rubygems in all subfolders:
for d in ./cmor_*/ ; do (cd "$d" && gem yank ${PWD##*/} -v 0.0.16.pre); done
I keep forgetting how to test exceptions with rspec2. This is an example:
describe "something" do describe "exceptions" do it "should raise an exception" do expect { # Code that throws an exception }.to raise_error end