Ariel Rodriguez Romero

Override-able options in rails partial view

Sometimes you have partial views with static values that would be nice to change, but adding specific options for each one of them would be painful.

A better approach is to have all these values in a hash, at the beginning of the partial view. And allow changing all of them with a parameter.

options = {
  styles: {
    main: stylesheet_url('external/styles'),
    list: stylesheet_url('external/list'),
    side: stylesheet_url('external/side'),
}.deep_merge((defined?(options) && options) || {})

	Use options here as usual...

	<p><%= options[:styles][:main] %></p>
	<p><%= options[:styles][:list] %></p>
	<p><%= options[:styles][:side] %></p>

This partial view can be rendered as usual, with the only difference that you can pass an additional object options and override each one of the values there.

<%= render 'partial_view_name', options: {styles: { main: stylesheet_url('other/main') }} %>

Note that you don’t need to pass all the parameters, just the ones you need to change. As both hashes are merged using deep merge.

Related posts

Deploying a serverless NextJS App

We migrated to NextJS recently and started using ZEIT to deploy our frontend. We required some configuration and even found a bug on ZEIT’s platform in the process.

Set up end-to-end tests with jest-pupeteer and NextJS

This contains details and the configuration files required to setup jest-pupeteer for end-to-end testing in a NextJS app.

Get route information from Google Flights

Simple strategy to scrape information from google flights (with manual work). It’s helpful if you don’t need a lot of information.