<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Vincent Bakker</title>
    <description>The latest articles on Forem by Vincent Bakker (@vinbak).</description>
    <link>https://forem.com/vinbak</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F748520%2Fa4936184-e018-421d-b05c-38f99cd09526.jpeg</url>
      <title>Forem: Vincent Bakker</title>
      <link>https://forem.com/vinbak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vinbak"/>
    <language>en</language>
    <item>
      <title>Clone record with overrides</title>
      <dc:creator>Vincent Bakker</dc:creator>
      <pubDate>Wed, 05 Jul 2023 07:15:40 +0000</pubDate>
      <link>https://forem.com/vinbak/clone-record-with-overrides-an9</link>
      <guid>https://forem.com/vinbak/clone-record-with-overrides-an9</guid>
      <description>&lt;p&gt;The clone_with_overrides function duplicates the original record using the dup method, assigns the overridden fields from the overrides hash, saves the new record, and returns it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person &amp;lt; ApplicationRecord
  def clone_with_overrides(overrides = {})
    # Create a new instance of the model
    new_record = self.dup

    # Assign the overridden fields to the new instance
    overrides.each do |field, value|
      new_record[field] = value
    end

    # Save the new record
    new_record.save

    # Return the cloned record with overrides
    new_record
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use this function, you can call it on an instance of the model and pass a hash of field-value pairs to override. Here's an example usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Assuming you have an existing record with ID 1
original_record = Person.find(1)

# Clone the record and override some fields
cloned_record = original_record.clone_with_overrides(
  name: 'John',
  age: 42
)

# The cloned record is saved and ready to use
puts cloned_record.inspect

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to adjust the field names and their types according to your model's attributes.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>Search Engine Optimization (MetaTags) for Ruby on Rails applications</title>
      <dc:creator>Vincent Bakker</dc:creator>
      <pubDate>Thu, 09 Dec 2021 09:38:33 +0000</pubDate>
      <link>https://forem.com/vinbak/search-engine-optimization-metatags-for-ruby-on-rails-applications-22lf</link>
      <guid>https://forem.com/vinbak/search-engine-optimization-metatags-for-ruby-on-rails-applications-22lf</guid>
      <description>&lt;p&gt;The following code set's up the minimal required meta tags for Social Media and Search Engines using the MetaTags gem. ([&lt;a href="https://moz.com/blog/meta-data-templates-123%5D"&gt;https://moz.com/blog/meta-data-templates-123]&lt;/a&gt;) &lt;/p&gt;

&lt;p&gt;You can easily extend this with other properties. Setting this up with a couple of helper functions makes it easy to extend and maintain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;p&gt;Add the "meta-tags" gem to your Gemfile.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem 'meta-tags'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And run &lt;code&gt;bundle install&lt;/code&gt; command.&lt;/p&gt;

&lt;h3&gt;
  
  
  MetaTags Usage
&lt;/h3&gt;

&lt;p&gt;First, add this code to your main layout:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;head&amp;gt;
  &amp;lt;%= display_meta_tags site: 'My website' %&amp;gt;
&amp;lt;/head&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, to set the page title, add this to each of your views (see below for other options):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h1&amp;gt;&amp;lt;%= title 'My page title' %&amp;gt;&amp;lt;/h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When views are rendered, the page title will be included in the right spots:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;head&amp;gt;
  &amp;lt;title&amp;gt;My website | My page title&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;h1&amp;gt;My page title&amp;lt;/h1&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Setting up default values
&lt;/h3&gt;

&lt;p&gt;Let's add a helper function to setup default values&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def default_meta_tags
    {
      site: 'Site Name',
      title: '',
      reverse: true,
      separator: '|',
      description: 'Site Description',
      keywords: 'your, key, words',
      canonical: request.original_url,
      noindex: !Rails.env.production?,
      icon: [
        { href: image_url('logo.png') },
        { href: image_url('logo.png'), rel: 'apple-touch-icon', sizes: '180x180', type: 'image/png' },
      ],
      og: {
        site_name: 'Site Name',
        title: '',
        description: 'Site Description', 
        type: 'website',
        url: request.original_url,
        image: image_url('social.jpg')
      }
    }
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Implement the helper function in your main layout&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;head&amp;gt;
  &amp;lt;%= display_meta_tags(default_meta_tags %&amp;gt;
&amp;lt;/head&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Overriding the default values
&lt;/h3&gt;

&lt;p&gt;The following example overrides the default values for a Post model. (Add this code to the show.erb for example)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set_meta_tags title: @post.title, 
  description: @post.intro, 
  twitter: {
   card: "summary",
   site: "@getSmart_NL"
 },
  og: { 
    title: @post.title, 
    description: @post.intro, 
    image: image_url(@post.header.attached? ? url_for(@post.header) : "social.jpg"), 
    type: "article" 
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*Instructions based on MetaTags readme and tweaked to my personal usage.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>programming</category>
    </item>
    <item>
      <title>Adding TailwindCSS to your Rails project with PostCSS 7 compatibility
</title>
      <dc:creator>Vincent Bakker</dc:creator>
      <pubDate>Tue, 16 Nov 2021 08:58:37 +0000</pubDate>
      <link>https://forem.com/vinbak/adding-tailwindcss-to-your-rails-project-with-postcss-7-compatibility-2o81</link>
      <guid>https://forem.com/vinbak/adding-tailwindcss-to-your-rails-project-with-postcss-7-compatibility-2o81</guid>
      <description>&lt;h3&gt;
  
  
  Install Tailwind CSS with PostCSS 7 compatibility
&lt;/h3&gt;

&lt;p&gt;Run the following command to add tailwind to your project and &lt;code&gt;package.json&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add tailwindcss@npm:@tailwindcss/postcss7-compat @tailwindcss/postcss7-compat postcss@^7 autoprefixer@^9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create Tailwind Configuration
&lt;/h3&gt;

&lt;p&gt;The following command will create a tailwind.config.js file where you can setup the default configuration for TailwindCSS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tailwindcss init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Tailwind config file will be empty and should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  purge: [],
  theme: {
    extend: {},
  },
  variants: {},
  plugins: [],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you compile all Tailwind CSS classes the generated CSS file will be huge. Let's enable Just-in-time compilation to only extract the classes we use from our views, helpers and javascript files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  mode: 'jit',
  purge: [
      './app/views/**/*.html.erb',
      './app/helpers/**/*.rb',
      './app/javascript/**/*.js',
  ],
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adding Tailwind to PostCSS config
&lt;/h3&gt;

&lt;p&gt;You will need to add the following line to the postcss.config.js file to include it in the compiling process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require('tailwindcss')('tailwind.js'),    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is an example of my postcss.config.js file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  plugins: [
    require('tailwindcss')('./app/javascript/css/tailwind.js'),    
    require('postcss-import'),
    require('postcss-flexbugs-fixes'),
    require('postcss-preset-env')({
      autoprefixer: {
        flexbox: 'no-2009'
      },
      stage: 3
    })
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Import Tailwind to your Javascript Pack
&lt;/h3&gt;

&lt;p&gt;You will need to import tailwind via javascript.&lt;br&gt;
Create an application.css file in app/javascript/css/&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I usually keep this in a folder called css, you could choose any other folder that's convenient for you inside the app/javascript directory  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Add the following to your application.css file that you just created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* tailwind */
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Import application.css in your app/javascript/packs/application.js file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import  "../layouts/application.css";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Import Tailwind to your layout
&lt;/h3&gt;

&lt;p&gt;Now that you have added TailwindCSS to your application pack, you will have to import it in application.html.erb to use tailwind globally in your application and make it work with Webpack.&lt;/p&gt;

&lt;p&gt;Add the following line to &lt;code&gt;app/views/layouts/application.html.erb&lt;/code&gt; in &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;%=  stylesheet_pack_tag  'application',  media: 'all',  'data-turbolinks-track': 'reload'  %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that’s it! &lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Instructions based on Bodhish Thomas post and tweaked to my personal usage.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>rails</category>
      <category>tailwindcss</category>
    </item>
  </channel>
</rss>
