Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for meta tags in locales #84

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,21 @@ Also you could use `set_meta_tags` method to define all meta tags simultaneously

You can find allowed options for `set_meta_tags` method below.

### Using MetaTags in locales with I18n

You can define title, description and keywords of any view in your locales file.
en:
meta_tags:
visitors:
index:
title: My page title
description: My page description
keywords: My, Page, Keywords

This would default the controller `visitors` with action `index` title, description and keywords.
You can still overwrite them from a view or controller like normally.


### Using MetaTags in view

To set meta tags you can use following methods:
Expand Down
14 changes: 14 additions & 0 deletions lib/meta_tags/controller_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module ControllerHelper

included do
alias_method_chain :render, :meta_tags
before_filter :meta_tags_from_locales
end

# Processes the <tt>@page_title</tt>, <tt>@page_keywords</tt>, and
Expand All @@ -27,6 +28,19 @@ def render_with_meta_tags(*args, &block)
end
protected :render_with_meta_tags

# Processes meta tags from locales file in the name space
# [locale].meta_tags.[controller_name].[action].[title|description|keywords]
# So en.meta_tags.visitors.index.title would be loaded as title for default
# welcome page in the visitors controller in english.
def meta_tags_from_locales
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment Branch Condition size for meta_tags_from_locales is too high. [17.29/15]

name_space = "meta_tags.#{controller_name}.#{action_name}"

self.meta_tags[:title] = I18n.t("#{name_space}.title") unless I18n.t("#{name_space}.title", default: '').blank?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [123/80]
Unnecessary spacing detected.
Redundant self detected.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

self.meta_tags[:keywords] = I18n.t("#{name_space}.keywords") unless I18n.t("#{name_space}.keywords", default: '').blank?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [129/80]
Unnecessary spacing detected.
Redundant self detected.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

self.meta_tags[:description] = I18n.t("#{name_space}.description") unless I18n.t("#{name_space}.description", default: '').blank?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [135/80]
Redundant self detected.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end
protected :meta_tags_from_locales

# Set meta tags for the page.
#
# See <tt>MetaTags::ViewHelper#set_meta_tags</tt> for details.
Expand Down