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
Sujay
Rails templates accept any locals as arguments. However, starting from Rails 7.1, we can restrict which locals a template must accept using "Strict Locals".
I wonder who in the world wrote this snippet!!! :)
#javascript #browser
Published
Author
Syed Sibtain
System Analyst
Ransack adds several class methods to ActiveRecord::Base for managing search and sort capabilities. These methods allow us to define which attributes, associations, and scopes are accessible through Ransack queries, thus enhancing security and control.
ransackable_attributes: Defines which model attributes can be used in Ransack searches. For example, only the order_number, status, and business_unit fields can be used in Ransack searches.
Code
def self.ransackable_attributes(auth_object = nil)%w[order_number status business_unit] end
ransackable_associations: Specifies which model associations are accessible for searching. For examle, only the customer and vendor associations are allowed for searching.
Code
def self.ransackable_associations(auth_object = nil)%w[customer vendor] end
ransortable_attributes: Lists which attributes can be used for sorting results. In this example, sorting is allowed only by order_number and created_at
Code
def self.ransortable_attributes(auth_object = nil)%w[order_number created_at] end
ransackable_scopes: Determines which custom scopes can be applied in searches.
#rails #ransack #search
Published
Author
Sujay
In postgres, current_setting() function is used to get the value of a configuration parameter.
Code
Set rls.tenant_id =1;SETselect current_setting('rls.tenant_id'); current_setting-----------------1(1 row)
#postgres
Published
Author
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.
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.
Lesson: Even a browser can cause browser compatibility issues.
#regex #browserCompatibility #safari #javascript
Published
Author
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.
# MyApp/app/overrides/models/blog/article_override.rbBlog::Article.class_eval do # ...end
Using class_eval ensures we are reopening the class or module, not redefining it.
#rails #rails-engines
Published
Author
Sujay
Remove the published gem from rubygems using
Code
gem yank GEM-v VERSION
#rails #rubygems
Published
Author
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
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
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