Last active
December 22, 2015 23:58
-
-
Save henrik/6550039 to your computer and use it in GitHub Desktop.
Magic title helper for Rails: for FoosController#show, it uses `t(:"foos.show.title")` unless you do `<% self.title = "Something" %>` in the view.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html | |
head | |
title= title("My site") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module TitleHelper | |
def title=(title) | |
@page_title = title | |
end | |
def title(base_title = nil) | |
[page_title, base_title].compact.join(" – ") | |
end | |
private | |
def page_title | |
@page_title || default_page_title | |
end | |
def default_page_title | |
# Reinventing t(:".foo") since this is called from a layout. | |
key = ( | |
controller.controller_path.split("/") + | |
[controller.action_name, "title"] | |
).join(".") | |
t(key, default: "").presence | |
end | |
end |
FIXME: if e.g. #create renders "new", this breaks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I should spec and gemify this. I think I already have a gem from a previous implementation of something similar…