Meta-Tags

Install meta-tags Gem, tries to inject display_meta_tags into application.html.(erb|slim) if exists
Icons/chart bar
Used 25 times
Created by
S Stefan Wienert

Usage
Run to install meta-tags Gem and make a base configuration.

The script tries to inject a call to display_meta_tags(default_meta_tags) in your application.html.(erb|slim|haml) files and also adds a default_meta_tags method into ApplicationHelper with some sensible defaults (OG / Twitter meta tags), where you can adjust the default meta tags.

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/XnJs6n"
Template Source

Review the code before running this template on your machine.

run 'bundle add meta-tags'
# gem 'meta-tags'
# run 'bundle install'

generate 'meta_tags:install'


if File.exists?('app/views/layouts/application.html.slim') and yes?("Add display_meta_tags snippet to application.html.slim? [Yn]")
  inject_into_file 'app/views/layouts/application.html.slim', after: /^\s+head[\r\n]+/ do <<-STRING
    = display_meta_tags(default_meta_tags)
  STRING
  end
end

if File.exists?('app/views/layouts/application.html.haml') and yes?("Add display_meta_tags snippet to application.html.haml? [Yn]")
  inject_into_file 'app/views/layouts/application.html.haml', after: /^\s+%head[\r\n]+/ do <<-STRING
    = display_meta_tags(default_meta_tags)
  STRING
  end
end

if File.exists?('app/views/layouts/application.html.erb') and yes?("Add display_meta_tags snippet to application.html.erb? [Yn]")
  inject_into_file 'app/views/layouts/application.html.erb', after: /^\s+<head>[\r\n]*/ do <<-STRING
    <%= display_meta_tags(default_meta_tags) %>

  STRING
  end
end

if File.exists?('app/helpers/application_helper.rb')
  inject_into_file 'app/helpers/application_helper.rb', after: /module ApplicationHelper[\r\n]*/ do <<-STRING

  def default_meta_tags
    # TODO: change favicon
    img = image_url("logo-large.png")
    {
      title: "TODO",
      site: "#{Rails.application.class.name.split('::').first}",
      reverse: true,
      description: "TODO",
      keywords: "TODO",
      separator: "&mdash;".html_safe,
      image: img,
      image_src: img,
      og: {
        title: :title,
        type: 'website',
        url: request.url,
        image: :image,
        description: :description
      },
      twitter: { image: :image_src }
    }
  end

    STRING
  end
end
Comments

Sign up or Login to leave a comment.