Posts

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

Ruby < 3.1 installs failing on Ubuntu 22.04 with an openssl error

Montag, 03. April 2023, 11:07 Uhr | roberto@vasquez-angel.de |

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.

Moving from Resque to ActiveJob

Donnerstag, 18. August 2022, 08:14 Uhr | roberto@vasquez-angel.de |

  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.

Getting a list of all presence validated attributes for all services in your application

Dienstag, 12. Juli 2022, 14:17 Uhr | roberto@vasquez-angel.de |

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.

Loading nested gems with zeitwerk

Montag, 12. April 2021, 14:40 Uhr | roberto@vasquez-angel.de |

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

Setting up a rails development environment on windows with wsl

Samstag, 20. März 2021, 19:25 Uhr | roberto@vasquez-angel.de |

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 resources

Dienstag, 01. September 2020, 16:11 Uhr | roberto@vasquez-angel.de |

Cleanup RVM

Freitag, 24. Juli 2020, 11:25 Uhr | roberto@vasquez-angel.de |

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

Making AWS S3 requests in ruby

Donnerstag, 11. Juni 2020, 23:15 Uhr | roberto@vasquez-angel.de |

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')

Manipulating the schema migrations table with active record

Dienstag, 19. Mai 2020, 10:57 Uhr | roberto@vasquez-angel.de |

class SchemaMigration < ActiveRecord::Base; self.primary_key = :version; end
SchemaMigration.where(version: "20200514202233").first
SchemaMigration.where(version: "20200514202233").update_all(version: "20200514142149")

"Could not find "README.md" in any of your source paths" when running rake dummy:app

Dienstag, 04. Februar 2020, 10:39 Uhr | roberto@vasquez-angel.de |

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.

Fix command: webpack not found on heroku

Mittwoch, 22. Januar 2020, 22:43 Uhr | roberto@vasquez-angel.de |

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

Fixing 'find_spec_for_exe': can't find gem bundler

Montag, 11. November 2019, 22:21 Uhr | roberto@vasquez-angel.de |

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'

Useful commands when developing gems

Montag, 08. Juli 2019, 09:51 Uhr | roberto@vasquez-angel.de |

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

Expect exceptions in rspec2 test

Donnerstag, 07. April 2011, 13:31 Uhr | roberto@vasquez-angel.de |

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