smart_titles 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/Readme.md +30 -13
- data/lib/smart_titles/helper.rb +19 -21
- data/lib/smart_titles/version.rb +1 -1
- data/smart_titles.gemspec +0 -3
- data/test/smart_titles/helper_test.rb +30 -11
- metadata +9 -32
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZmM2MjRlNTYzMzZhNDdmM2ZkNmIzNDQ3MTExN2I1NzQ5ZDU0YjYwYw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NzM4Y2Q3YjRmNzI0ZWM3NDljYjcwYjVjYTkwNTE5NTUzOGViN2M5Ng==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YzBlNGY3YzgzMTg2YTYwM2JmYTUzMzI1MGIzNWViZjVkMWYyM2ZiODkxNmZi
|
10
|
+
MWNhMjY3Y2M0Yzk4MmVjOTMyYzhhYzBjNjE0ZTdjNWE1MTRiYTRkODUzM2Q5
|
11
|
+
ZjVhMTc5NDdhNzkzY2NkMTc2NTdmMTQwYjVkM2UyNmM2MDBmYTc=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MTM2ODMyZDU1NzY3ZDVkODNiOTEzODg0YzY1ZTYxOWFlY2MxOTdiMDRhMmY1
|
14
|
+
YjgxMDcwYWVmMzNmOTVlMTUxOTZjNTFjN2RiNWUyYTQ2MmZmNTNlM2EyNWI4
|
15
|
+
ZTQ0Y2QwMzQ3MzhlNmViYTAzMDA5NDFkNmI5N2FkYzA4ODI4Y2U=
|
data/Readme.md
CHANGED
@@ -12,8 +12,7 @@ If you don't want to display it inside the page simply remove an equality sign:
|
|
12
12
|
<% title "Title for a browser window" %>
|
13
13
|
<h1>Different title for the page</h1>
|
14
14
|
|
15
|
-
|
16
|
-
It works only with Rails 3.1 and higher.
|
15
|
+
The gem uses [Semantic Versioning](http://semver.org/). It works with Rails 3.1 and higher.
|
17
16
|
|
18
17
|
Installation
|
19
18
|
---
|
@@ -27,7 +26,7 @@ And run:
|
|
27
26
|
|
28
27
|
Integration
|
29
28
|
---
|
30
|
-
|
29
|
+
Insert `head_title` inside the `<title>` tag in your layout:
|
31
30
|
|
32
31
|
<title><%= head_title "Default title" %></title>
|
33
32
|
|
@@ -35,10 +34,12 @@ The default title is displayed only if you haven't specified title in your view.
|
|
35
34
|
|
36
35
|
With Internationalization
|
37
36
|
---
|
38
|
-
|
37
|
+
It is easy to translate titles using built-in Rails I18n.
|
39
38
|
|
40
39
|
en:
|
41
|
-
|
40
|
+
layouts:
|
41
|
+
application:
|
42
|
+
title: Default title
|
42
43
|
pages:
|
43
44
|
index:
|
44
45
|
title: All pages
|
@@ -49,7 +50,11 @@ And then you should just call `title` without any arguments.
|
|
49
50
|
|
50
51
|
<%= title %>
|
51
52
|
|
52
|
-
|
53
|
+
The same goes for `head_title`:
|
54
|
+
|
55
|
+
<title><%= head_title %></title>
|
56
|
+
|
57
|
+
Note that you _have to call `title`_ even if you don't output it.
|
53
58
|
|
54
59
|
<% title %>
|
55
60
|
|
@@ -57,17 +62,15 @@ Dynamic titles are set as usual:
|
|
57
62
|
|
58
63
|
<%= title @page.title %>
|
59
64
|
|
60
|
-
"Default title" will be only used by `head_title` if you call it with no arguments and there will be no title set by the current page.
|
61
|
-
|
62
|
-
<title><%= head_title %></title>
|
63
|
-
|
64
65
|
Title Template
|
65
66
|
---
|
66
|
-
If you want to add your website name to all of your titles just add
|
67
|
+
If you want to add your website name to all of your titles just add title_template translation
|
67
68
|
|
68
69
|
en:
|
69
|
-
|
70
|
-
|
70
|
+
layouts:
|
71
|
+
application:
|
72
|
+
title: The Coolest Store
|
73
|
+
title_template: %{title} from the Coolest Store
|
71
74
|
products:
|
72
75
|
index:
|
73
76
|
title: Products
|
@@ -76,6 +79,20 @@ And now products#index page will have "Products from the Coolest Store" browser'
|
|
76
79
|
|
77
80
|
Changelog
|
78
81
|
---
|
82
|
+
|
83
|
+
### 0.4.0
|
84
|
+
The website title and template translations can now be scoped by layout. Example:
|
85
|
+
|
86
|
+
layouts:
|
87
|
+
application:
|
88
|
+
title: My Website
|
89
|
+
title_template: "%{title} - My Website"
|
90
|
+
admin:
|
91
|
+
title: Web Admin
|
92
|
+
|
93
|
+
Also, this prevents h1 from showing default title when translation is missing if you use cascading I18n backend.
|
94
|
+
The old-style global translations still do work.
|
95
|
+
|
79
96
|
### 0.3.2
|
80
97
|
Page title is no longer double-escaped.
|
81
98
|
|
data/lib/smart_titles/helper.rb
CHANGED
@@ -1,6 +1,4 @@
|
|
1
1
|
module SmartTitles
|
2
|
-
MISSING_TRANSLATION = Object.new
|
3
|
-
|
4
2
|
module Helper
|
5
3
|
# <title><%= head_title %></title>
|
6
4
|
# Will return title if it was set for the current page.
|
@@ -17,9 +15,10 @@ module SmartTitles
|
|
17
15
|
# head_title # => The Coolest Store
|
18
16
|
# title "Drinks" # => <h1>Drinks</h1>
|
19
17
|
# head_title # => Drinks from the Coolest Store
|
20
|
-
def head_title(default_title = t(:title))
|
21
|
-
if
|
22
|
-
|
18
|
+
def head_title(default_title = t('.title', default: :title))
|
19
|
+
if content_for?(:page_title)
|
20
|
+
title = content_for(:page_title)
|
21
|
+
t('.title_template', title: title, default: [:title_template, title])
|
23
22
|
else
|
24
23
|
default_title
|
25
24
|
end
|
@@ -28,24 +27,23 @@ module SmartTitles
|
|
28
27
|
# Convinient helper method that will:
|
29
28
|
# * Set custom title for the current page if it is passed. Otherwise the title will be automatically set
|
30
29
|
# * Return the title passed or looked up from locale wrapped into h1 tag
|
31
|
-
def title(
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
content_tag(:h1, page_title) if page_title
|
36
|
-
end
|
37
|
-
|
38
|
-
private
|
39
|
-
|
40
|
-
# This is a page title that was set by the #title helper method
|
41
|
-
# It defaults to ".title" translation
|
42
|
-
def page_title
|
43
|
-
if content_for?(:page_title)
|
44
|
-
content_for(:page_title)
|
30
|
+
def title(custom_title_or_options = nil)
|
31
|
+
case custom_title_or_options
|
32
|
+
when Hash
|
33
|
+
options = custom_title_or_options
|
45
34
|
else
|
46
|
-
|
47
|
-
|
35
|
+
options = {}
|
36
|
+
custom_title = custom_title_or_options
|
48
37
|
end
|
38
|
+
|
39
|
+
title = custom_title
|
40
|
+
title ||= begin t('.title', options.merge(raise: true))
|
41
|
+
rescue I18n::MissingTranslationData
|
42
|
+
end
|
43
|
+
|
44
|
+
title &&= title.html_safe
|
45
|
+
provide(:page_title, title)
|
46
|
+
content_tag(:h1, title) if title
|
49
47
|
end
|
50
48
|
end
|
51
49
|
end
|
data/lib/smart_titles/version.rb
CHANGED
data/smart_titles.gemspec
CHANGED
@@ -14,10 +14,7 @@ Gem::Specification.new do |s|
|
|
14
14
|
|
15
15
|
s.add_dependency 'activesupport'
|
16
16
|
s.add_dependency 'actionpack'
|
17
|
-
s.add_development_dependency 'rake'
|
18
17
|
|
19
18
|
s.files = `git ls-files`.split("\n")
|
20
19
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
-
s.require_paths = ["lib"]
|
23
20
|
end
|
@@ -17,19 +17,24 @@ class SmartTitlesHelperTest < ActionView::TestCase
|
|
17
17
|
|
18
18
|
|
19
19
|
def test_custom_title
|
20
|
-
|
20
|
+
store_layout_title
|
21
21
|
store_page_title
|
22
22
|
assert_equal "<h1>Hi</h1>", title("Hi")
|
23
23
|
end
|
24
24
|
|
25
25
|
def test_translated_title
|
26
|
-
|
26
|
+
store_layout_title
|
27
27
|
store_page_title
|
28
28
|
assert_equal "<h1>New post</h1>", title
|
29
29
|
end
|
30
30
|
|
31
|
+
def test_translated_title_with_options
|
32
|
+
store_translations posts: { new: { title: "New post - %{category}" } }
|
33
|
+
assert_equal "<h1>New post - News</h1>", title(category: "News")
|
34
|
+
end
|
35
|
+
|
31
36
|
def test_no_page_title
|
32
|
-
|
37
|
+
store_layout_title
|
33
38
|
assert_nil title
|
34
39
|
end
|
35
40
|
|
@@ -39,21 +44,21 @@ class SmartTitlesHelperTest < ActionView::TestCase
|
|
39
44
|
|
40
45
|
|
41
46
|
def test_head_title_with_custom_title
|
42
|
-
|
47
|
+
store_layout_title
|
43
48
|
store_page_title
|
44
49
|
title("Hi")
|
45
50
|
assert_equal "Hi", head_title
|
46
51
|
end
|
47
52
|
|
48
53
|
def test_head_title_with_translated_title
|
49
|
-
|
54
|
+
store_layout_title
|
50
55
|
store_page_title
|
51
56
|
title
|
52
57
|
assert_equal "New post", head_title
|
53
58
|
end
|
54
59
|
|
55
60
|
def test_head_title_with_translated_global_title
|
56
|
-
|
61
|
+
store_layout_title
|
57
62
|
assert_equal "My Website", head_title
|
58
63
|
end
|
59
64
|
|
@@ -73,7 +78,7 @@ class SmartTitlesHelperTest < ActionView::TestCase
|
|
73
78
|
|
74
79
|
|
75
80
|
def test_head_title_with_template_and_translated_titles
|
76
|
-
|
81
|
+
store_layout_title
|
77
82
|
store_page_title
|
78
83
|
store_title_template
|
79
84
|
title
|
@@ -81,7 +86,7 @@ class SmartTitlesHelperTest < ActionView::TestCase
|
|
81
86
|
end
|
82
87
|
|
83
88
|
def test_head_title_with_template_and_custom_title
|
84
|
-
|
89
|
+
store_layout_title
|
85
90
|
store_page_title
|
86
91
|
store_title_template
|
87
92
|
title("Hi")
|
@@ -89,11 +94,20 @@ class SmartTitlesHelperTest < ActionView::TestCase
|
|
89
94
|
end
|
90
95
|
|
91
96
|
def test_head_title_with_template_and_translated_title
|
92
|
-
|
97
|
+
store_layout_title
|
93
98
|
store_title_template
|
94
99
|
assert_equal "My Website", head_title
|
95
100
|
end
|
96
101
|
|
102
|
+
def test_head_title_old
|
103
|
+
store_old_global_title
|
104
|
+
assert_equal "My Website", head_title
|
105
|
+
|
106
|
+
store_page_title
|
107
|
+
title
|
108
|
+
assert_equal "d New post b", head_title
|
109
|
+
end
|
110
|
+
|
97
111
|
|
98
112
|
def test_head_title_template_is_escaped
|
99
113
|
store_translations title_template: '"%{title}"'
|
@@ -145,11 +159,16 @@ private
|
|
145
159
|
store_page_title
|
146
160
|
end
|
147
161
|
|
148
|
-
def
|
149
|
-
store_translations title: "My Website"
|
162
|
+
def store_layout_title
|
163
|
+
store_translations layouts: { application: { title: "My Website" } }
|
150
164
|
end
|
151
165
|
|
152
166
|
def store_title_template
|
167
|
+
store_translations layouts: { application: { title_template: "d %{title} b" } }
|
168
|
+
end
|
169
|
+
|
170
|
+
def store_old_global_title
|
171
|
+
store_translations title: "My Website"
|
153
172
|
store_translations title_template: "d %{title} b"
|
154
173
|
end
|
155
174
|
|
metadata
CHANGED
@@ -1,64 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_titles
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.4.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Semyon Perepelitsa
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-05-23 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
|
-
name: activesupport
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
22
|
-
type: :runtime
|
23
14
|
prerelease: false
|
15
|
+
name: activesupport
|
24
16
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
17
|
requirements:
|
27
18
|
- - ! '>='
|
28
19
|
- !ruby/object:Gem::Version
|
29
20
|
version: '0'
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
name: actionpack
|
32
21
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
22
|
requirements:
|
35
23
|
- - ! '>='
|
36
24
|
- !ruby/object:Gem::Version
|
37
25
|
version: '0'
|
38
26
|
type: :runtime
|
27
|
+
- !ruby/object:Gem::Dependency
|
39
28
|
prerelease: false
|
29
|
+
name: actionpack
|
40
30
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
31
|
requirements:
|
43
32
|
- - ! '>='
|
44
33
|
- !ruby/object:Gem::Version
|
45
34
|
version: '0'
|
46
|
-
- !ruby/object:Gem::Dependency
|
47
|
-
name: rake
|
48
35
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
|
-
requirements:
|
51
|
-
- - ! '>='
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: '0'
|
54
|
-
type: :development
|
55
|
-
prerelease: false
|
56
|
-
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
36
|
requirements:
|
59
37
|
- - ! '>='
|
60
38
|
- !ruby/object:Gem::Version
|
61
39
|
version: '0'
|
40
|
+
type: :runtime
|
62
41
|
description:
|
63
42
|
email: sema@sema.in
|
64
43
|
executables: []
|
@@ -82,29 +61,27 @@ files:
|
|
82
61
|
homepage: http://github.com/semaperepelitsa/smart_titles
|
83
62
|
licenses:
|
84
63
|
- MIT
|
64
|
+
metadata: {}
|
85
65
|
post_install_message:
|
86
66
|
rdoc_options: []
|
87
67
|
require_paths:
|
88
68
|
- lib
|
89
69
|
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
-
none: false
|
91
70
|
requirements:
|
92
71
|
- - ! '>='
|
93
72
|
- !ruby/object:Gem::Version
|
94
73
|
version: '0'
|
95
74
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
-
none: false
|
97
75
|
requirements:
|
98
76
|
- - ! '>='
|
99
77
|
- !ruby/object:Gem::Version
|
100
78
|
version: '0'
|
101
79
|
requirements: []
|
102
80
|
rubyforge_project:
|
103
|
-
rubygems_version:
|
81
|
+
rubygems_version: 2.0.3
|
104
82
|
signing_key:
|
105
|
-
specification_version:
|
83
|
+
specification_version: 4
|
106
84
|
summary: Really convenient way to set up page titles in a Rails application.
|
107
85
|
test_files:
|
108
86
|
- test/smart_titles/helper_test.rb
|
109
87
|
- test/test_helper.rb
|
110
|
-
has_rdoc:
|