Class: Frozen::Generators::SeoGenerator

Inherits:
FrozenRails::Generator show all
Defined in:
lib/generators/frozen/seo_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_application_helpersObject

helpers for meta tags



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/generators/frozen/seo_generator.rb', line 44

def add_application_helpers
  unless File.exist?("app/helpers/application_helper.rb")
    create_file "app/helpers/application_helper.rb", "module ApplicationHelper\nend\n"
  end

  inject_into_file "app/helpers/application_helper.rb",
    after: "module ApplicationHelper\n" do
    <<~RUBY
      def site_name
        "Rails Static"
      end

      def title
        [ @page&.title.presence, site_name ].uniq.compact.join(" · ")
      end

      def description
        @page.frontmatter&.dig(:description)
      end

      def author
        "François Catuhe"
      end

      def canonical_url
        url_for(only_path: false)
      end

      def robots_content
        Rails.env.production? ? "index, follow" : "noindex, nofollow"
      end

      def og_meta_tag(key, content)
        case key
        when :title, :description, :image
          tag(:meta, name: key, property: "og:\#{key}", content: content)
        when :author
          tag(:meta, name: key, content: content)
        else
          tag(:meta, property: "og:\#{key}", content: content)
        end
      end

    RUBY
  end
end

#add_favicon_noticeObject



91
92
93
# File 'lib/generators/frozen/seo_generator.rb', line 91

def add_favicon_notice
  say_status :info, "Add favicons (favicon.ico, icon.svg, apple-touch-icon.png) to app/assets/images and create og-image.jpg"
end

#create_og_partialObject

add meta partial and update layout



11
12
13
# File 'lib/generators/frozen/seo_generator.rb', line 11

def create_og_partial
  template "_og_meta_tags.html.erb", "app/views/layouts/_og_meta_tags.html.erb"
end

#create_sitemap_modelsObject

sitemap models



96
97
98
99
100
# File 'lib/generators/frozen/seo_generator.rb', line 96

def create_sitemap_models
  template "sitemap.rb", "app/models/sitemap.rb"
  template "sitemap_entry.rb", "app/models/sitemap/entry.rb"
  template "robots_generatable.rb", "app/models/sitemap/robots_generatable.rb"
end

#inject_layout_tagsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/generators/frozen/seo_generator.rb', line 15

def inject_layout_tags
  return unless File.exist?("app/views/layouts/application.html.erb")

  inject_into_file "app/views/layouts/application.html.erb",
    after: "<head>\n" do
    <<~ERB
      <%= render 'layouts/og_meta_tags', tags: {
        title: title,
        description: description,
        image: image_url('og-image.jpg'),
        author: author,
        type: "website",
        site_name: site_name,
        url: canonical_url,
      } %>

      <%= tag :link, rel: "canonical", href: canonical_url %>

      <%= favicon_link_tag 'favicon.ico', sizes: '32x32' %>
      <%= favicon_link_tag 'icon.svg', type: 'image/svg+xml' %>
      <%= favicon_link_tag 'apple-touch-icon.png', rel: 'apple-touch-icon' %>

      <%= tag :meta, name: "robots", content: robots_content %>

    ERB
  end
end

#remove_default_robotsObject



102
103
104
# File 'lib/generators/frozen/seo_generator.rb', line 102

def remove_default_robots
  remove_file "public/robots.txt" if File.exist?("public/robots.txt")
end