TILs - Fueling Curiosity, One Insight at a Time

At Codemancers, we believe every day is an opportunity to grow. This section is where our team shares bite-sized discoveries, technical breakthroughs and fascinating nuggets of wisdom we've stumbled upon in our work.

Published
Author
user-image
Sujay
In postgres, current_setting() function is used to get the value of a configuration parameter.

Code

Set rls.tenant_id = 1;
SET

select current_setting('rls.tenant_id');
 current_setting
-----------------
 1
(1 row)


#postgres
Published
Author
user-image
Syed Sibtain
System Analyst
Namespaces in Rails help organize our application by grouping related controllers, models, and views into separate directories. Using namespaces with scaffolding keeps our codebase structured and manageable, especially in larger applications.

Example.
rails generate scaffold Order::PurchaseOrder order_number:string business_unit:string ...

This will create files under the Order namespace, including controllers, models, and views. This approach keeps our codebase structured, with Order as a namespace, making it easier to manage related components and maintain clarity in larger applications.


Code

app/
├── controllers/
│   └── order/
│       └── purchase_orders_controller.rb
├── models/
│   └── order/
│       └── purchase_order.rb
├── views/
│   └── order/
│       └── purchase_orders/
│           ├── _form.html.erb
│           ├── edit.html.erb
│           ├── index.html.erb
│           ├── new.html.erb


#rails #namespace
Published
Author
user-image
Vaibhav Yadav
Senior System Analyst
Cross browser regular expression issue:

Recently I came across a regular expression that would cause the page to crash on iphone Safari browser, the regex was for obscuring email.

Problematic Regex:

Code

const obscuredEmail = email.replace(/(?<=.1}).(?=[^@]*@)/g, '*');


Fix:

Code

const obscuredEmail = email.replace(/(.)(?=.*@)/g, (match, p1, offset, string) => offset < string.indexOf('@') - 1 ? '*' : p1);


Lesson: Even a browser can cause browser compatibility issues.

#regex #browserCompatibility #safari #javascript
Published
Author
user-image
Sachin Kabadi
System Analyst
How to override models in parent application by reopening existing Rails engine classes. This can be done by organising overrides in a dedicated directory (e.g., app/overrides), ignoring this directory in the autoloader, and preloading the overrides in a to_prepare callback.

Code

# config/application.rb
module MyApp
  class Application < Rails::Application
    # ...

    overrides = "#{Rails.root}/app/overrides"
    Rails.autoloaders.main.ignore(overrides)

    config.to_prepare do
      Dir.glob("#{overrides}/**/*_override.rb").sort.each do |override|
        load override
      end
    end
  end
end


To override an engine model, such as Blog::Article:

Code

# Blog/app/models/blog/article.rb
module Blog
  class Article < ApplicationRecord
    # ...
  end
end


Create a file that reopens the class:

Code

# MyApp/app/overrides/models/blog/article_override.rb
Blog::Article.class_eval do
  # ...
end


Using class_eval ensures we are reopening the class or module, not redefining it.

#rails #rails-engines
Published
Author
user-image
Sujay
Remove the published gem from rubygems using

Code

gem yank GEM -v VERSION


#rails #rubygems
Published
Author
user-image
Satya
while using form.file_field for file attachments we can restrict the file type to any type if we want.
For eg: if we want to accept file of type image then we can pass accept attribute.

Code

<%= form.file_field :picture, accept: "image/*" %>


Note: This will disable other file types in your local file dialog while enabling the image files only 🪄 .
#rails , #active-storage
Published
Author
user-image
Sujay
To ensure a dependency is installed with the engine during gem install, it must be specified within the Gem::Specification block inside the engine's .gemspec file (e.g., blog.gemspec for an engine named Blog, located at the root)

Code

s.add_dependency "pagy"


#rails #rails-engines
Published
Author
user-image
Syed Sibtain
System Analyst
In Rails, gems are libraries that add specific functionality to a Rails application. They can be used across different projects and typically do not have their own structure or generators.
Examples include devise for authentication and nokogiri for XML parsing.

Engines, on the other hand, are miniature Rails applications that can have their own routes, controllers, models, and views. They are used to encapsulate and modularize specific features or components within a Rails app. An engine can be packaged as a gem, but it provides more extensive, self-contained functionality compared to a typical gem.

#rails
Published
Author
user-image
Sujay
A Rails engine is a pattern used to modularize a Rails application. These engines are self-contained applications with their own models, views, controllers, and routes, allowing them to function autonomously. They can be integrated into a larger Rails application. For example, in an e-commerce application, modules like orders, products, users, and payments can each be separate engines.
#rails
Published
Author
user-image
Pavankumarreddy
Resolving gen_random_uuid() Error with PostgreSQL While Implementing CI
When configuring CI with GitHub Actions, I encountered the following error:

Code

PG::UndefinedFunction: ERROR:  function gen_random_uuid() does not exist


This error occurred because the gen_random_uuid() function is not available in PostgreSQL versions older than 11. UUID generation functions were only available through external modules like uuid-ossp and pgcrypto in these older versions.

To resolve this issue, I upgraded to PostgreSQL 13, which includes the gen_random_uuid() function to generate version-4 UUIDs. After upgrading, the error was resolved.



#rails #postgresql #uuid #ci-cd #github-actions

Showing 18 to 20 of 82 results

Ready to Build Something Amazing?

Codemancers can bring your vision to life and help you achieve your goals