RSSAmplifier

Blog

Ruby Language | btihen

Ruby Language

btihen.devRSS feed ↗45 posts

Latest posts

Bridgetown 2.1 - Ruby Static-Site Orientation

Overview Bridgetown allows static site generation with familiar ruby technology (ERB, Markdown, etc). Bridgetown is an active project and in the last 2 years it has gone from v1.0.0 to v2.1.2. Excellent resources and a deep dive can be found at https://edge.bridgetownrb.com/docs Bridgetown requires node and by default uses PostCSS , but you can switch to SASS if needed. No CSS framework is…

Rails: Table Selection Form (Article 2 of 2)

This article uses: https://btihen.dev/posts/ruby/rails_8_0_rails_tables_filtering_sorting as a starting point. Aticle 1 of 2: Modern Rails: Table Filtering & Sorting Article 2 of 2: Modern Rails: Table Selection Form The code for this article can be found at: https://github.com/btihen-dev/rails_table_selection_form Basic Rails App Setup Be sure you have a database (I assume postgresql), but feel…

Rails: Table Filtering & Sorting (Article 1 of 2)

I recently learned that Rails 8 (actually 7.1+) has delightful features that make it easy to render dynamic tables without (or minimal) JavaScript and efficient network traffic. This is an exploration of using these new features. This article is an extension of: https://www.colby.so/posts/turbo-8-refresh-sorting The cool thing is that morph updates without a full page reload - so its fast! and…

Rails with Postgres - Fuzzy Searches

Overview Recently I was working on a project at work that required finding the appropriate record with incomplete information (that might be either mispelled or within multiple columns - thus LIKE and ILIKE are insufficient). My co-worker Gernot Kogler , introduced me to the trigram scoring searches using similarity and word_similarity - this is a simple and very effective way to do fuzzy…

Ruby 3.x - Lazy Evaluation, prioritized algorithms

We had a problem to the ‘best’ fitting object within an array. In our case it was to find the object that would return the best metadata. But finding the ‘best’ person to talk with is a similar problem. Let’s say we have a list of employees and we want to find the best person to talk with about purchasing our product. class Person attr_reader :name , :rank , :role ,…

Creating the Rails Age Plugin

Creating a simple Rails Engine despite the variety of documentation was confusing, so here is what I learned. Source Code: https://github.com/marpori/rails_age RubyGem Link: https://rubygems.org/gems/rails_age AGE Demo App: https://github.com/marpori/rails_age_demo_app It’s important to set up a dummy app from the beginning (to simplify testing) So after several starts I found the following…

Rails 7.1.x - GraphDB App with AGE

Graph Databases are very interesting technologies for managing social networks, recommendation engines, etc. In this article, I want to explore using Apache AGE within a Rails Application. This article assumes a working knowledge of OpenCypher and Apache AGE. If you want to get started you can find an Intro and further resources at: https://btihen.dev/posts/tech/graphdb_getting_started_age_1_5_0/…

Rails 7.1.x - Graph AGE Integration Exploration

Graph Databases are very interesting technologies for managing social networks, recommendation engines, etc. In this article, I want to explore using Apache AGE within a Rails Application. This article assumes a working knowledge of OpenCypher and Apache AGE. If you want to get started you can find an Intro and further resources at: https://btihen.dev/posts/tech/graphdb_getting_started_age_1_5_0/…

Rails 7.1.x - Minimal Rails -- no Database

Overview I was recently asked how to create a rails site without a database (in order to run on a very small footprint). I thought that was an interesting question, so here is my exploration. In oder to make the site dynamic, this article creates a landing page and an email form. Create Rails # active storage will install even without active record - so we MUST not allow that either rails new nodb…

Rails 7.1.x - Dynamic Tables with Turbo Morph

I recently learned that Rails 7.1+ has some delightful features that make it easy to render dynamic tables without JavaScript. This is an exploration of using these new features. The cool thing is that morph updates without a full page reload - so its fast! and very easy to setup. This code can be found at: https://github.com/btihen-dev/rails_morph_tables Quick Summary By adding this config in our…

Rails 7.1.x Secured Namespaced Controllers

I have found in large code bases Modules and namespaces are critical. Here is a simple way to build a secured admin panel using namespaces. This code can be found at: https://github.com/btihen-dev/rails_secured_namespace Getting Started I will create a basic application using: rails new secured_namespace -d = postgresql -T cd secured_namespace bin/rails g scaffold Contact email first_name…

Rails 7.1.x Base App

A basic Rails app structure that offers a variety of opportunities for various Rails experiments. This code can be found at: https://github.com/btihen-dev/rails_base_app Quick Summary without explanations nor step: # use other options as needed rails new base_app --database = postgresql -T # main is good for testing very new features, but leads to lots of update messages # rails new base_app -T…

Rails 7.1.x Dynamic Tables using Turbo Morph

NOTE : this article has been rewritten with more features in https://btihen.dev/posts/ruby/rails_8_0_rails_table_filtering_sorting/ I recently learned that Rails 7.1+ has some delightful features that make it easy to render dynamic tables without JavaScript. This is an exploration of using these new features. This article is an adaptation of the article:…

Rails 7.1.x JSON API Introduction with Nested data

I was recently asked about Rails API applications and wanted to explore the options (based on the Flintstones characters). The end goal is to return one or many records in the following format - without an N+1 query. { 'id' : 8 , 'first_name' : 'Wilma' , 'last_name' : 'Flintstone' , 'given_name' : 'Slaghoople' , 'nick_name' : null , 'gender' : 'female' , 'person_jobs' : [ { 'start_date' :…

Rails 7.1.x Exploring Bi-directional Relations

I am have been exploring Graph-DBs and wanted to try a graph structure in a relational DB. Not trivial! This code can be found at: https://github.com/btihen-dev/flintstones_api Getting Started bin/rails new flintstones_api --api cd flintstones_api bin/rails g scaffold Person first_name last_name given_last_name gender bin/rails g model PersonRelationships role_one role_two \ person_one:references…

Rails Design: Protected Modules with Injection

Rails Design - Modules with Boundaries and Injection Recently, a colleague Severin Räz suggested that we could be using module to create namespaces (to organize our code with high cohesion) and private_constant to enforce API boundaries (enforce low coupling). Based on that I ran some experiments and wrote about them in Rails with Protected Modules and Rails with Minimal Engines) Similarly,…

Rails with Mingines (Minimized Engines)

Intro This article was inspired by [Julián Pinzón] and his talk at Ruby Australia 2023 All you need is Rails (Engines): Compartmentalising your Monolith . Its widely known that Engines are a power way to create a full ‘sub-rails application’. However, I’ve struggled to enjoy engine usage, in particular with the front-end aspects. Thus, I liked the idea of low overhead…

Rails with Protected Modules

Rails with Strong Boundaries between the Modules This experiment was inspired from an internal Tech Talk at Garaio REM by a colleague Severin Räz . He suggested that we could be using module to create namespaces (to organize our code with high cohesion) and private_constant to enforce API boundaries (enforce low coupling). The example given was similar to the following: Typical Ruby Code is very…

Rails 7.1.0.alpha2 (main) Test-drive

Install Rails Gem To install rails you first need to install the rails gem with: gem install rails I thought it would be fun to test the new alpha version of rails - but I always forget how to do this without upgrading an existing projects. Discover the Pre-release versions gem list rails --remote --prerelease -e --remote - checks the rubygems site - not the locally installed versions --prerelease…

Ruby Ractors

Intro Ractors (Ruby Actors) provide object safety and allow full parallel computing (uses as many CPUs as desired). This is very helpful with heavy CPU tasks that can be split into sensible independent tasks. Ruby Core has threads, but fundamentally they offer only concurrency and are dangerous since they share objects (which requires a lot of care). Adding Mutex helps with safety, but you are…

Rails Packwerk Refactor

I have been interested in building Rails with much less accidental coupling. Until recently, Engines have been the best way to do that, but the setup effort is rather heavy (to integrate the migrations, tests, namespaces, …). In fact, heavy enough that most people do without modules. Packwerk however, makes it easy to use modules and critcially migrate toward modules overtime. Packwerk…

Modular Rails using Packwerk

I have been interested in building Rails with much less accidental coupling. Until recently, Engines have been the best way to do that, but the setup effort is rather heavy (to integrate the migrations, tests, namespaces, …). In fact, heavy enough that most people do without modules. Packwerk however, makes it easy to use modules and critcially migrate toward modules overtime. Packwerk…

Rails 7.0 Using Packages (Packwerk gem)

Organizing Code Gems and Engines have long been used to organize Ruby and Rails code into workable small units. Shopify has introduced a new system called ‘packages’ - they use the packwerk gem to help us. In fact, it is designed to make it easy to take large (and likely highly coupled) large codebase and move toward ‘packages’ self-contained (or have explicit…

Rails 7.0 - Decoupling with Events (or Commands / Service Objects)

NOTE: the concepts work (we use them at work) - but this particular code hasn’t yet been tested. (just recording these ideas for me, but feel free to let me know of any suggestions) Organizing Code In Rails its all too easy to accidentally tightly couple controller activities with follow-up actions - resulting in bloated controllers and / or tight coupling with models. A relatively simple…

Rails 5.2 - Command Objects and PORO Attributes

Overview Sometimes within an work flow different validations and business logic is needed. It can be very complex if each modification uses the same controller and model logic! I’ve tried and ist crazy difficult. The best solution I have found is to use Commands and separate Controllers. It became practical to treat a command object as a Rails Model as of Rails 5.2 when the Attribute API…

Rails 7.0 Base Setup

update GEMS library gem update –system be sure bundler is installed gem install bundler update Bundler library bundle update –bundler update Ruby rbenv install 3.1.1 rbenv global 3.1.1 or asdf install ruby 3.1.1 asdf global ruby 3.1.1 be sure you have rails for your ruby version gem install rails # or ensure version 7.0 using gem install rails --version 7.0.0 Rails with esbuild & css…

Bridgetown 1.0 - Ruby Static-Site Orientation

Overview I have often wanted to build websites using as much of my Rails knowledge as possible. Now I can! Enter Bridgetown - https://edge.bridgetownrb.com/docs A ruby based (erb, components, etc), author-friendly (markdown pages). The newest version 1.0 beta uses esbuild by default (or webpacker) and has several pre-build deploy configurations and a quick and easy way to install TailwindCSS!…

Passwordless Authentication with Devise

I wrote a little app for a small non-profit group. Some of them had severe problems with password management - so this article is how I solved that. The easiest way to approach this is to use Rails built-in Secure Global IDs, in this way no database migrations are needed. Overview User enters their email-address in a simple form If account is found - a link with a token is generated and email is…

Rails 6.x Auth with MagicLink using SecureRandom Token

Passwordless Authentication is very convenient for users and generally as secure as passwords (according to many articles as long as the email access-links are short-lived - as email is not very secure). Therefore, after some reading, it seems like a good approach is to make a short-lived link, and then transfer the security to a session. I found that there seems to be three simple approaches: Do…

Rails 6.x+ Auth with MagicLinks using Rails Signed GlobalIDs

Passwordless Authentication is very convenient for users and generally as secure as passwords (a good security authentication discussion can be found at: ). A good approach is to make a short-lived link, and then transfer the security to a session. I found that there seems to be three simple approaches: Do it yourself: with a Stored-Token (with an expiration date-time) Do it yourself: with a…

Rails 7.0alpha2 Test-drive

I thought it would be fun to test the new alpha version of rails - but I always forget how to do this without upgrading an existing projects. Discover the Rails Pre-release versions gem list rails --remote --prerelease -e –remote - checks the rubygems site - not the locally installed versions –prerelease - find pre-release versions -e - use an exact match (many packages have rails in…

Strategy Pattern and Lambdas

Motivation Often it is nice to track metadata associated with a model - that is expensive and or complex to lookup - so ideally, we generate this when we save and it is available with each document read. With a lot of complex data this gets very tricky – especially when this should be configurable (perhaps with a DSL). I have found the strategy pattern is very helpful, but can have a lot of…

Squished String Type (Rails 5/6)

https://discuss.rubyonrails.org/t/database-fields-are-polluted-with-both-nil-and-empty-values-when-submitting-forms/74877/3 https://bitbucket.org/tiu/rails-application-template/src/master/rails/lib/types/type/string.rb https://stackoverflow.com/questions/3879680/how-can-i-make-rails-3-localize-my-date-formats/45743846#45743846 Lets start by making a sample project (without tests -T): 1 2 rails new…

Rails 6.x - Framework Agnostic Associations - part 2

Purpose In the interest of coding Rails in a way to work well with other code bases, I looking at ways to do complex database relations in a framework agnostic way. In particular, this article will primarily explore Polymorphic Relationships. This is the second article in the series. This article builds on (part 1)[post_ruby_rails/rails_6_x_agnostic_associations_1/] Overview In this case, I want…

Rails 6.x - Framework Agnostic Associations - part 2

Purpose In the interest of coding Rails in a way to work well with other code bases, I looking at ways to do complex database relations in a framework agnostic way. In particular, this article will primarily explore Polymorphic Relationships. This is the second article in the series. This article builds on (part 1)[post_ruby_rails/rails_6_x_agnostic_associations_1/] Overview In this case, I want…

Rails 6.x - Framework Agnostic Associations - part 1

Purpose In the interest of coding Rails in a way to work well with other code bases, I looking at ways to do complex database relations in a framework agnostic way. In particular, this article will primarily explore Polymorphic Relationships. This is the second article in the series. This article is followed up with (part 2)[post_ruby_rails/rails_6_x_agnostic_associations_2/] Overview In this…

Rails 6.1 - Hotwire with Lazy Loading

Overview As was seen in Using Hotwire with Flash Messages Hotwire can easily load data - let’s do this in a lazy loaded way (after the html is loaded we add data). Basic Setup Start with the code at the end of: Using Hotwire in Rails Prepare our code Let’s remove the extra Tweet.new load in the controller’s index method: # app/controllers/tweets_controller.rb def index @tweets =…

Rails 6.1 - Hotwire with Modals

Overview Now that you have the basics of using Hotwire in Rails Using Hotwire in Rails - its interesting to try using it in other contexts, inparticular modals are very useful for inputs in Single Page Apps. So in this Blog we will make the new input form a modal and leave the edit as an in-place form. Basic Setup Start with the code at the end of: Using Hotwire in Rails Add jQuery to Bootstrap…

Rails 6.1 - Hotwire with StimulusJS

Overview Hotwire only updates dom_ids (usually only within a partial) - so other Frontend needs still need to be met with Javascript. Rails uses StimulusJS to augment Hotwire. I Using Hotwire with Flash Messages we created a new instance of Tweet in the turbo_template and sent that to the form. (Pretty non-standard) - we can do this even more simply by using JS to clear the form without…

Rails 6.1 - Hotwire with Flash Messages

Overview Now that you have the basics of using Hotwire in Rails Using Hotwire in Rails - its interesting to try using it in other contexts, inparticular modals are very useful for inputs in Single Page Apps. So in this Blog we will make the new input form a modal and leave the edit as an in-place form. Basic Setup Start with the code at the end of: Using Hotwire in Rails Flash Messages in Partial…

Rails 6.1 - Hotwire (Simple Realtime SPA)

Overview Hotwire is planned to be integrated into Rails 7.x and is already included in the soon to be published book: Modern Front-End Development for Rails: Hotwire, Stimulus, Turbo, and React Hotwire allows us to build Single Page Apps using Ruby and Rails with nearly NO configuration! It allow very responsive web-applications as it reloads only the parts of the page that change via a socket.…

Tweets - A Single Page Rails App (using reloads)

Overview This article is just to have a base app that stays on the index page. We will use this as a starting point for a more efficient single page app by updating this project with Hotwire in the next article Tweets - A Rails based Single Page App using Hotwire Hotwire allows us to build Single Page Apps using Ruby and Rails with nearly NO configuration! It allow very responsive web-applications…

Rails 6.1 with TailwindCSS 2.0 and AlpineJS

Intro TailwindCSS is a very flexible CSS framework and makes it easy to customize unique web pages and animations. Unfortunately, with Rails its a bit tricky to install and configure with Rails Standards. TailwindCSS 2.0 expects PostCSS 8 and Rails Webpacker uses PostCSS 7 (for now) TailwindCSS 2.0 expects AlpineJS, React or Vue – by default Rails uses StimulusJS (although you can…

Install and Configure Rails

Intro To document is mostly for me – at least until I automate my setup defaults. However, I am glad to share and get ideas from others too. I will build a little calendar app I use with friends (it’s focused on being mobile friendly and easy to use – not a full featured calendar). Rails Setup Taken from: https://gist.github.com/alxndr/7569551…

Rails Devise User Model with Roles

Configure devise (for multiple types of accounts) install the devise engine: bin/rails generate devise:install now follow the basic setup config &ndash; add to config/environments/development.rb config . action_mailer . default_url_options = { host : 'localhost' , port : 3000 } add notifications to the layout for devise in app/views/layouts/application.html.erb just above <%= yeild %> < p class =…