page_title_helper 1.0.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +0 -1
- data/README.md +50 -41
- data/Rakefile +17 -1
- data/init.rb +1 -0
- data/lib/page_title_helper.rb +35 -31
- data/test/en.yml +11 -1
- data/test/multiple_formats_test.rb +6 -7
- data/test/page_title_helper_test.rb +48 -24
- data/test/test_helper.rb +22 -11
- metadata +49 -9
- data/VERSION.yml +0 -4
- data/rails/init.rb +0 -4
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,37 +1,54 @@
|
|
1
1
|
# Page title helper
|
2
2
|
|
3
|
-
Ever wondered if there was an easier and DRY-way to set your page titles (and/or headings)
|
4
|
-
|
3
|
+
Ever wondered if there was an easier and DRY-way to set your page titles (and/or headings),
|
4
|
+
introducing _page title helper_, a small Rails 3 view helper to inflect titles from controllers
|
5
|
+
& actions.
|
5
6
|
|
6
|
-
|
7
|
+
**Note:** for Rails 2.x please use version 1.0.1: `gem install page_title_helper -v 1.0.1`, because
|
8
|
+
the latest versions are no longer compatible with Rails 2.x.
|
9
|
+
|
10
|
+
In your layout add this to your `<head>`-section:
|
7
11
|
|
8
12
|
...
|
9
|
-
<title><%=
|
13
|
+
<title><%= page_title %></title>
|
10
14
|
...
|
11
15
|
|
12
|
-
That's it. Now just add
|
16
|
+
That's it. Now just add translations, in e.g. `config/locales/en.yml`:
|
13
17
|
|
14
18
|
en:
|
15
19
|
contacts:
|
16
20
|
index:
|
17
21
|
title: "Contacts"
|
18
22
|
|
19
|
-
When
|
20
|
-
is looked up and printed, together with the applications basename, like: `My cool
|
23
|
+
When `/contacs/` is requested, the key `:en, :contacts, :index, :title`
|
24
|
+
is looked up and printed, together with the applications basename, like: `Contacts - My cool App`.
|
21
25
|
The format etc. is of course configurable, just head down to the options.
|
22
26
|
|
23
27
|
## Installation
|
24
28
|
|
25
|
-
As gem (from
|
29
|
+
As gem (from rubygems.org):
|
26
30
|
|
27
|
-
|
31
|
+
# then add the following line to Gemfile
|
32
|
+
gem 'page_title_helper'
|
28
33
|
|
29
|
-
#
|
30
|
-
|
34
|
+
# living on the bleeding edge?
|
35
|
+
gem 'page_title_helper', :git => 'git://github.com/lwe/page_title_helper.git'
|
31
36
|
|
32
37
|
or as plain old Rails plugin:
|
33
38
|
|
34
|
-
|
39
|
+
rails plugin install git://github.com/lwe/page_title_helper.git
|
40
|
+
|
41
|
+
## Translated titles
|
42
|
+
|
43
|
+
All translated titles are inflected from the current controller and action, so to
|
44
|
+
easily explain all lookups, here an example with the corresponding lookups:
|
45
|
+
|
46
|
+
Admin::AccountController#index => :'admin.account.index.title'
|
47
|
+
:'admin.account.title'
|
48
|
+
options[:default]
|
49
|
+
|
50
|
+
For `create` and `update` a further fallback to `new.title` and `edit.title` have
|
51
|
+
been added, because they certainly are duplicates.
|
35
52
|
|
36
53
|
## Customize titles
|
37
54
|
|
@@ -39,16 +56,16 @@ Need a custom title, or need to fill in some placeholders? Just use the _bang_ m
|
|
39
56
|
`contacts/show.html.erb` the requirement is to display the contacts name in the
|
40
57
|
`<title>-tag`as well as in the heading?
|
41
58
|
|
42
|
-
<h1><%=
|
59
|
+
<h1><%= page_title!(@contact.name) %></h1>
|
43
60
|
|
44
61
|
A call to `page_title` will now return the contacts name, neat :) if for example the
|
45
62
|
`<h1>` does not match the `<title>`, then well, just do something like:
|
46
63
|
|
47
64
|
<% page_title!(@contact.name + " (" + @contact.company.name + ")") %>
|
48
|
-
<h1><%=
|
65
|
+
<h1><%= @contact.name %></h1>
|
49
66
|
|
50
|
-
Guess, that's it. Of course it's also possible to use `translate`
|
51
|
-
translate
|
67
|
+
Guess, that's it. Of course it's also possible to use `translate` within `page_title!`, to
|
68
|
+
translate custom titles, like:
|
52
69
|
|
53
70
|
# in config/locales/en.yml:
|
54
71
|
en:
|
@@ -57,7 +74,7 @@ translate customzied titles, like:
|
|
57
74
|
title: "Welcome back, {{name}}"
|
58
75
|
|
59
76
|
# in app/views/dashboard/index.html.erb:
|
60
|
-
<h1><%=
|
77
|
+
<h1><%= page_title!(t('.title', :name => @user.first_name)) %></h1>
|
61
78
|
|
62
79
|
## More fun with <tt>:format</tt>
|
63
80
|
|
@@ -71,7 +88,7 @@ Adding custom interpolations is as easy as defining a block, for example to acce
|
|
71
88
|
controller:
|
72
89
|
|
73
90
|
PageTitleHelper.interpolates :controller do |env|
|
74
|
-
env.controller.controller_name.humanize
|
91
|
+
env[:view].controller.controller_name.humanize
|
75
92
|
end
|
76
93
|
|
77
94
|
page_title :format => ':title / :controller / :app' # => "Welcome back / Dashboard / My cool app"
|
@@ -84,14 +101,14 @@ To access just the title, without any magic app stuff interpolated or appended,
|
|
84
101
|
Need a custom format for a single title? Just return an array:
|
85
102
|
|
86
103
|
# in the view:
|
87
|
-
<h1><%=
|
104
|
+
<h1><%= page_title!(@contact.name, ":title from :company - :app") %></h1> # => <h1>Franz Meyer</h1>
|
88
105
|
|
89
106
|
# in the <head>
|
90
|
-
<title><%=
|
107
|
+
<title><%= page_title %></title> # => this time it will use custom title like "Franz Meyer from ABC Corp. - My cool app"
|
91
108
|
|
92
|
-
To streamline that feature a bit and simplify reuse of often used formats, it's
|
109
|
+
To streamline that feature a bit and simplify reuse of often used formats, it's possible to define format aliases like:
|
93
110
|
|
94
|
-
# in an initializer:
|
111
|
+
# in an initializer, e.g. config/initializers/page_title_helper.rb:
|
95
112
|
PageTitleHelper.formats[:with_company] = ":title from :company - :app"
|
96
113
|
PageTitleHelper.formats[:promo] = ":app - :title" # show app first for promo pages :)
|
97
114
|
|
@@ -106,8 +123,8 @@ To streamline that feature a bit and simplify reuse of often used formats, it's
|
|
106
123
|
- "Features comparison"
|
107
124
|
- !ruby/sym promo
|
108
125
|
|
109
|
-
Pretty, cool, aint it? The special `:format => :app` works also
|
110
|
-
`:default` format, which
|
126
|
+
Pretty, cool, aint it? The special `:format => :app` works also via the `formats` hash. Then there is also a
|
127
|
+
`:default` format, which can be used to override the default format.
|
111
128
|
|
112
129
|
## All options - explained
|
113
130
|
|
@@ -118,8 +135,8 @@ Pretty, cool, aint it? The special `:format => :app` works also with the `format
|
|
118
135
|
<tr>
|
119
136
|
<td><tt>:app</tt></td>
|
120
137
|
<td>Specify the applications name, however it's
|
121
|
-
recommended to define
|
122
|
-
<td>Inflected from <tt>
|
138
|
+
recommended to define it via translation key <tt>:'app.name'</tt>.</td>
|
139
|
+
<td>Inflected from <tt>Rails.root</tt></td>
|
123
140
|
<td>string</td>
|
124
141
|
</tr>
|
125
142
|
<tr>
|
@@ -138,22 +155,12 @@ Pretty, cool, aint it? The special `:format => :app` works also with the `format
|
|
138
155
|
<td><tt>:default</tt></td>
|
139
156
|
<td>string, symbol</td>
|
140
157
|
</tr>
|
141
|
-
<tr>
|
142
|
-
<td><tt>:suffix</tt></td>
|
143
|
-
<td>Not happy with the fact that the translations must be named like
|
144
|
-
<tt>en -> contacts -> index -> title</tt>, but prefer e.g. them to be suffixed with
|
145
|
-
<tt>page_title</tt>? Then just set <tt>:suffix => :page_title</tt>.</td>
|
146
|
-
<td><tt>:title</tt></td>
|
147
|
-
<td>symbol or string</td>
|
148
|
-
</tr>
|
149
158
|
</table>
|
150
159
|
</p>
|
151
160
|
|
152
|
-
|
161
|
+
Options can be set globally via `PageTitleHelper.options`. Note, currently it only
|
162
|
+
makes sense to set `:default` globally.
|
153
163
|
|
154
|
-
PageTitleHelper.options[:suffix] = :page_title
|
155
|
-
|
156
|
-
Note, currently it only makes sense to set `:default` and/or `:page_title` globally.
|
157
164
|
To add or change formats use:
|
158
165
|
|
159
166
|
# change the default format used (if no format is specified):
|
@@ -162,17 +169,19 @@ To add or change formats use:
|
|
162
169
|
# add a custom format alias (which can be used with page_title(:format => :promo))
|
163
170
|
PageTitleHelper.formats[:promo] = ":app // :title"
|
164
171
|
|
165
|
-
_Note:_ it's recommended to add this kind of stuff to an initializer.
|
172
|
+
_Note:_ it's recommended to add this kind of stuff to an initializer, like e.g.
|
173
|
+
`config/initializers/page_title_helper.rb`.
|
166
174
|
|
167
175
|
## A (maybe useful) interpolation
|
168
176
|
|
169
177
|
The internationalized controller name, with fallback to just display the humanized name:
|
170
178
|
|
171
179
|
PageTitleHelper.interpolates :controller do |env|
|
172
|
-
|
180
|
+
c = env[:view].controller
|
181
|
+
I18n.t(c.controller_path.tr('/', '.') + '.controller', :default => c.controller_name.humanize)
|
173
182
|
end
|
174
183
|
|
175
|
-
_Note:_ Put this kind of stuff into an
|
184
|
+
_Note:_ Put this kind of stuff into an initializer, like `config/initializers/page_title_helper.rb` or someting like that.
|
176
185
|
|
177
186
|
## Licence and copyright
|
178
187
|
Copyright (c) 2009 Lukas Westermann (Zurich, Switzerland), released under the MIT license
|
data/Rakefile
CHANGED
@@ -6,14 +6,30 @@ require 'yard'
|
|
6
6
|
desc 'Default: run unit tests.'
|
7
7
|
task :default => :test
|
8
8
|
|
9
|
+
def version
|
10
|
+
defined?(PageTitleHelper) ? PageTitleHelper::VERSION : "0.0.0.error"
|
11
|
+
end
|
12
|
+
|
13
|
+
begin
|
14
|
+
require File.join(File.dirname(__FILE__), 'lib', 'page_title_helper')
|
15
|
+
rescue
|
16
|
+
puts "Oops, there was en error loading page_title_helper.rb"
|
17
|
+
end
|
18
|
+
|
9
19
|
begin
|
10
20
|
require 'jeweler'
|
11
21
|
Jeweler::Tasks.new do |gemspec|
|
12
22
|
gemspec.name = "page_title_helper"
|
23
|
+
gemspec.version = version
|
13
24
|
gemspec.summary = "Simple, internationalized and DRY page titles and headings for rails."
|
14
25
|
gemspec.email = "lukas.westermann@gmail.com"
|
15
26
|
gemspec.homepage = "http://github.com/lwe/page_title_helper"
|
16
27
|
gemspec.authors = ["Lukas Westermann"]
|
28
|
+
|
29
|
+
gemspec.files.reject! { |f| f =~ /\.gemspec$/ }
|
30
|
+
|
31
|
+
gemspec.add_dependency('rails', '>= 3.0.0')
|
32
|
+
gemspec.add_development_dependency('shoulda')
|
17
33
|
end
|
18
34
|
Jeweler::GemcutterTasks.new
|
19
35
|
rescue LoadError
|
@@ -32,7 +48,7 @@ YARD::Rake::YardocTask.new(:doc) do |t|
|
|
32
48
|
t.files = ['lib/**/*.rb']
|
33
49
|
t.options = [
|
34
50
|
"--readme", "README.md",
|
35
|
-
"--title", "page_title_helper API Documentation"
|
51
|
+
"--title", "page_title_helper API v#{version} Documentation"
|
36
52
|
]
|
37
53
|
end
|
38
54
|
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'page_title_helper'
|
data/lib/page_title_helper.rb
CHANGED
@@ -6,15 +6,16 @@
|
|
6
6
|
# Licence:: MIT-Licence (http://www.opensource.org/licenses/mit-license.php)
|
7
7
|
#
|
8
8
|
# See documentation for +page_title+ for usage examples and more informations.
|
9
|
+
require 'active_support'
|
9
10
|
|
10
11
|
# PageTitleHelper
|
11
12
|
module PageTitleHelper
|
12
13
|
|
14
|
+
# Page title version number
|
15
|
+
VERSION = "2.0.0".freeze
|
16
|
+
|
13
17
|
# http://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/interpolations.rb
|
14
|
-
module Interpolations
|
15
|
-
# Represents the environment which is passed into each interpolation call.
|
16
|
-
class TitleEnv < ::Struct.new(:options, :view, :controller, :title); end
|
17
|
-
|
18
|
+
module Interpolations
|
18
19
|
extend self
|
19
20
|
|
20
21
|
def self.interpolate(pattern, *args)
|
@@ -26,11 +27,11 @@ module PageTitleHelper
|
|
26
27
|
end
|
27
28
|
|
28
29
|
def app(env)
|
29
|
-
env
|
30
|
+
env[:app] || I18n.translate(:'app.name', :default => File.basename(Rails.root).humanize)
|
30
31
|
end
|
31
32
|
|
32
33
|
def title(env)
|
33
|
-
env
|
34
|
+
env[:title]
|
34
35
|
end
|
35
36
|
end
|
36
37
|
|
@@ -44,8 +45,7 @@ module PageTitleHelper
|
|
44
45
|
def self.options
|
45
46
|
@options ||= {
|
46
47
|
:format => :default,
|
47
|
-
:default => :'app.tagline'
|
48
|
-
:suffix => :title
|
48
|
+
:default => :'app.tagline'
|
49
49
|
}
|
50
50
|
end
|
51
51
|
|
@@ -69,36 +69,40 @@ module PageTitleHelper
|
|
69
69
|
|
70
70
|
options = PageTitleHelper.options.merge(options || {}).symbolize_keys!
|
71
71
|
options[:format] ||= :title # handles :format => false
|
72
|
-
options.assert_valid_keys(:app, :
|
73
|
-
|
74
|
-
# construct basic env to pass around
|
75
|
-
env = Interpolations::TitleEnv.new(options, self, self.controller)
|
76
|
-
|
72
|
+
options.assert_valid_keys(:app, :default, :format)
|
73
|
+
|
77
74
|
# read page title and split into 'real' title and customized format
|
78
|
-
|
79
|
-
|
80
|
-
|
75
|
+
title = @_page_title || page_title_from_translation(options[:default])
|
76
|
+
title, options[:format] = *(title << options[:format]) if title.is_a?(Array)
|
77
|
+
|
81
78
|
# handle format aliases
|
82
|
-
format = options
|
79
|
+
format = options.delete(:format)
|
83
80
|
format = PageTitleHelper.formats[format] if PageTitleHelper.formats.include?(format)
|
84
81
|
|
82
|
+
# construct basic env to pass around
|
83
|
+
env = { :title => title, :app => options.delete(:app), :options => options, :view => self }
|
84
|
+
|
85
85
|
# interpolate format
|
86
|
-
Interpolations.interpolate
|
86
|
+
Interpolations.interpolate(format, env)
|
87
87
|
end
|
88
88
|
|
89
89
|
protected
|
90
90
|
|
91
|
-
# Find
|
92
|
-
#
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
91
|
+
# Find translation for `controller.action.title` combination, falls back to
|
92
|
+
# `controller.title` or supplied default if no title was found.
|
93
|
+
def page_title_from_translation(default)
|
94
|
+
base = controller.controller_path.tr('/', '.')
|
95
|
+
action = params[:action].to_s
|
96
|
+
|
97
|
+
keys = [:"#{base}.#{action}.title"]
|
98
|
+
keys << :"#{base}.new.title" if action == 'create'
|
99
|
+
keys << :"#{base}.edit.title" if action == 'update'
|
100
|
+
keys << :"#{base}.title"
|
101
|
+
keys << default
|
102
|
+
|
103
|
+
I18n.translate(keys.shift, :default => keys)
|
103
104
|
end
|
104
|
-
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# include helper methods in ActionView
|
108
|
+
ActiveSupport.on_load(:action_view) { include PageTitleHelper }
|
data/test/en.yml
CHANGED
@@ -5,7 +5,17 @@ en:
|
|
5
5
|
page_title: custom contacts title
|
6
6
|
myhaml:
|
7
7
|
title: this is haml!
|
8
|
-
|
8
|
+
admin:
|
9
|
+
account:
|
10
|
+
title: Account administration
|
11
|
+
show:
|
12
|
+
title: Account
|
13
|
+
new:
|
14
|
+
title: New account
|
15
|
+
edit:
|
16
|
+
title: Edit account
|
17
|
+
|
18
|
+
placeholder: "Displaying %{name}"
|
9
19
|
app:
|
10
20
|
tagline: Default
|
11
21
|
other_tagline: Other default
|
@@ -4,8 +4,7 @@ require 'page_title_helper'
|
|
4
4
|
class MultipleFormatsTest < ActiveSupport::TestCase
|
5
5
|
context "#page_title supporting multiple formats through arrays" do
|
6
6
|
setup do
|
7
|
-
@view =
|
8
|
-
@view.template = ActionView::Template.new "contacts/list.html.erb"
|
7
|
+
@view = TestView.new('contacts', 'list')
|
9
8
|
end
|
10
9
|
|
11
10
|
should "accept an array passed in the page_title block and use the second argument as format" do
|
@@ -21,8 +20,8 @@ class MultipleFormatsTest < ActiveSupport::TestCase
|
|
21
20
|
context "#page_title with format aliases" do
|
22
21
|
setup do
|
23
22
|
PageTitleHelper.formats[:myformat] = ":title <-> :app"
|
24
|
-
@view =
|
25
|
-
@view.template =
|
23
|
+
@view = TestView.new('contacts', 'list')
|
24
|
+
@view.template = "contacts/list.html.erb"
|
26
25
|
end
|
27
26
|
|
28
27
|
should "have a default alias named :app" do
|
@@ -57,16 +56,16 @@ class MultipleFormatsTest < ActiveSupport::TestCase
|
|
57
56
|
I18n.load_path = [File.join(File.dirname(__FILE__), "en_wohaapp.yml")]
|
58
57
|
I18n.reload!
|
59
58
|
PageTitleHelper.formats[:promo] = ":app > :title"
|
60
|
-
@view =
|
59
|
+
@view = TestView.new
|
61
60
|
end
|
62
61
|
|
63
62
|
should "allow to overide format through YAML" do
|
64
|
-
@view.
|
63
|
+
@view.controller! 'pages', 'features'
|
65
64
|
assert_equal 'Wohaapp > Feature comparison', @view.page_title
|
66
65
|
end
|
67
66
|
|
68
67
|
should "handle raw string formats from YAML as well" do
|
69
|
-
@view.
|
68
|
+
@view.controller! 'pages', 'signup'
|
70
69
|
assert_equal 'Sign up for Wohaapp now!', @view.page_title
|
71
70
|
end
|
72
71
|
end
|
@@ -8,23 +8,22 @@ class PageTitleHelperTest < ActiveSupport::TestCase
|
|
8
8
|
I18n.load_path = [File.join(File.dirname(__FILE__), 'en.yml')]
|
9
9
|
I18n.reload!
|
10
10
|
|
11
|
-
@view =
|
12
|
-
@view.template_path = "contacts/list.html.erb"
|
11
|
+
@view = TestView.new('contacts', 'list')
|
13
12
|
end
|
14
13
|
|
15
14
|
context "::Interpolations" do
|
16
15
|
should "interpolate :app and :title" do
|
17
|
-
assert_equal 'Page title helper', PageTitleHelper::Interpolations.app(
|
18
|
-
assert_equal 'Appname', PageTitleHelper::Interpolations.app(
|
19
|
-
assert_equal 'untitled', PageTitleHelper::Interpolations.title(
|
16
|
+
assert_equal 'Page title helper', PageTitleHelper::Interpolations.app({})
|
17
|
+
assert_equal 'Appname', PageTitleHelper::Interpolations.app({ :app => 'Appname' })
|
18
|
+
assert_equal 'untitled', PageTitleHelper::Interpolations.title({:title => 'untitled'})
|
20
19
|
end
|
21
20
|
|
22
21
|
should "allow adding custom interpolations" do
|
23
22
|
# extend Interpolations
|
24
23
|
PageTitleHelper.interpolates(:app_reverse) { |env| app(env).reverse.downcase }
|
25
24
|
|
26
|
-
assert_equal "anna", PageTitleHelper::Interpolations.app_reverse(
|
27
|
-
assert_equal "ppa", PageTitleHelper::Interpolations.interpolate(':app_reverse',
|
25
|
+
assert_equal "anna", PageTitleHelper::Interpolations.app_reverse({ :app => 'Anna' })
|
26
|
+
assert_equal "ppa", PageTitleHelper::Interpolations.interpolate(':app_reverse', { :app => 'app' })
|
28
27
|
end
|
29
28
|
|
30
29
|
should "interpolate in correct order, i.e. longest first" do
|
@@ -32,8 +31,8 @@ class PageTitleHelperTest < ActiveSupport::TestCase
|
|
32
31
|
PageTitleHelper.interpolates(:foobar_test) { "foobar_test" }
|
33
32
|
PageTitleHelper.interpolates(:title_foobar) { "title_foobar" }
|
34
33
|
|
35
|
-
assert_equal "title_foobar / foobar_test / foobar / foobar_x", PageTitleHelper::Interpolations.interpolate(":title_foobar / :foobar_test / :foobar / :foobar_x",
|
36
|
-
end
|
34
|
+
assert_equal "title_foobar / foobar_test / foobar / foobar_x", PageTitleHelper::Interpolations.interpolate(":title_foobar / :foobar_test / :foobar / :foobar_x", {})
|
35
|
+
end
|
37
36
|
end
|
38
37
|
|
39
38
|
context "#page_title (define w/ block)" do
|
@@ -62,10 +61,17 @@ class PageTitleHelperTest < ActiveSupport::TestCase
|
|
62
61
|
end
|
63
62
|
|
64
63
|
context "#page_title (rendering)" do
|
65
|
-
should "read default title from I18n, based on
|
64
|
+
should "read default title from I18n, based on controller/action" do
|
66
65
|
assert_equal 'contacts.list.title - Page title helper', @view.page_title
|
67
66
|
end
|
68
67
|
|
68
|
+
# currently not feasible :(
|
69
|
+
# should "if passed in a hash, use it to expand translations" do
|
70
|
+
# @view.controller! 'contacts', 'show'
|
71
|
+
# assert_equal 'Contact: Bella', @view.page_title!( :name => "Bella" )
|
72
|
+
# assert_equal 'Contact: Bella - Page title helper', @view.page_title
|
73
|
+
#end
|
74
|
+
|
69
75
|
should "only print app name if :format => :app" do
|
70
76
|
assert_equal 'Page title helper', @view.page_title(:format => :app)
|
71
77
|
end
|
@@ -88,29 +94,47 @@ class PageTitleHelperTest < ActiveSupport::TestCase
|
|
88
94
|
should "return title if :format => false and when using the DRY-I18n titles" do
|
89
95
|
assert_equal "contacts.list.title", @view.page_title(:format => false)
|
90
96
|
end
|
91
|
-
|
97
|
+
|
92
98
|
should "render translated :'app.tagline' if no title is available" do
|
93
|
-
@view.
|
99
|
+
@view.controller! 'view/does', 'not_exist'
|
94
100
|
assert_equal "Default - Page title helper", @view.page_title
|
95
101
|
end
|
96
102
|
|
97
|
-
should "render
|
98
|
-
@view.
|
99
|
-
assert_equal '
|
103
|
+
should "render use controller.title as first fallback, if no title exists" do
|
104
|
+
@view.controller! 'admin/account', 'index'
|
105
|
+
assert_equal 'Account administration - Page title helper', @view.page_title(:default => 'Other default')
|
100
106
|
end
|
107
|
+
|
108
|
+
should "not fallback to controller.title if controller.action.title exists" do
|
109
|
+
@view.controller! 'admin/account', 'show'
|
110
|
+
assert_equal 'Account - Page title helper', @view.page_title(:default => 'Other default')
|
111
|
+
end
|
101
112
|
|
102
|
-
should
|
103
|
-
@view.
|
104
|
-
assert_equal '
|
113
|
+
should 'fallback to controller.new.title if create has no title' do
|
114
|
+
@view.controller! 'admin/account', 'create'
|
115
|
+
assert_equal 'New account - Page title helper', @view.page_title(:default => 'Other default')
|
116
|
+
end
|
117
|
+
|
118
|
+
should 'fallback to controller.edit.title if update has no title' do
|
119
|
+
@view.controller! 'admin/account', 'update'
|
120
|
+
assert_equal 'Edit account - Page title helper', @view.page_title(:default => 'Other default')
|
121
|
+
end
|
122
|
+
|
123
|
+
should "render custom 'default' string, if title is not available nor controller.title" do
|
124
|
+
@view.controller! 'view/does', 'not_exist'
|
125
|
+
assert_equal 'Some default - Page title helper', @view.page_title(:default => 'Some default')
|
105
126
|
end
|
106
127
|
|
107
|
-
should "render
|
108
|
-
|
128
|
+
should "render custom default translation, if title is not available nor controller.title" do
|
129
|
+
@view.controller! 'view/does', 'not_exist'
|
130
|
+
assert_equal 'Other default - Page title helper', @view.page_title(:default => :'app.other_tagline')
|
109
131
|
end
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
132
|
+
end
|
133
|
+
|
134
|
+
context "README.md" do
|
135
|
+
should "interpolate :controller" do
|
136
|
+
PageTitleHelper.interpolates(:controller) { |env| env[:view].controller.controller_name.humanize }
|
137
|
+
assert_equal "contacts.list.title - Test", @view.page_title(:format => ":title - :controller")
|
114
138
|
end
|
115
139
|
end
|
116
140
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,23 +1,34 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'active_support'
|
3
3
|
require 'action_view'
|
4
|
-
require File.join(File.dirname(__FILE__), '..', '
|
4
|
+
require File.join(File.dirname(__FILE__), '..', 'init')
|
5
5
|
|
6
6
|
unless defined?(IRB)
|
7
7
|
require 'active_support/test_case'
|
8
8
|
require 'shoulda'
|
9
|
-
require 'rr'
|
10
|
-
# enable RR to use all the mocking magic, uweee!
|
11
|
-
Test::Unit::TestCase.send(:include, RR::Adapters::TestUnit)
|
12
9
|
end
|
13
10
|
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
# fake global rails object
|
12
|
+
Rails = Object.new
|
13
|
+
Rails.class_eval do
|
14
|
+
def root; @pathname ||= Pathname.new('/this/is/just/for/testing/page_title_helper') end
|
15
|
+
def env; "test" end
|
16
|
+
end
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
# Mock ActionView a bit to allow easy (fake) template assignment
|
19
|
+
class TestView < ActionView::Base
|
20
|
+
def initialize(controller_path = nil, action = nil)
|
21
|
+
@controller = ActionView::TestCase::TestController.new
|
22
|
+
@controller.controller_path = controller_path
|
23
|
+
self.params[:action] = action if action
|
24
|
+
end
|
25
|
+
|
26
|
+
def controller!(controller_path, action)
|
27
|
+
@controller.controller_path = controller_path
|
28
|
+
self.params[:action] = action
|
29
|
+
end
|
30
|
+
|
31
|
+
def controller
|
32
|
+
@controller
|
21
33
|
end
|
22
|
-
alias_method :template_path=, :template=
|
23
34
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: page_title_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 15
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 2.0.0
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Lukas Westermann
|
@@ -9,10 +15,39 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2010-09-14 00:00:00 +02:00
|
13
19
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rails
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 3.0.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: shoulda
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
16
51
|
description:
|
17
52
|
email: lukas.westermann@gmail.com
|
18
53
|
executables: []
|
@@ -27,9 +62,8 @@ files:
|
|
27
62
|
- LICENSE
|
28
63
|
- README.md
|
29
64
|
- Rakefile
|
30
|
-
-
|
65
|
+
- init.rb
|
31
66
|
- lib/page_title_helper.rb
|
32
|
-
- rails/init.rb
|
33
67
|
- test/en.yml
|
34
68
|
- test/en_wohaapp.yml
|
35
69
|
- test/multiple_formats_test.rb
|
@@ -45,21 +79,27 @@ rdoc_options:
|
|
45
79
|
require_paths:
|
46
80
|
- lib
|
47
81
|
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
48
83
|
requirements:
|
49
84
|
- - ">="
|
50
85
|
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
51
89
|
version: "0"
|
52
|
-
version:
|
53
90
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
54
92
|
requirements:
|
55
93
|
- - ">="
|
56
94
|
- !ruby/object:Gem::Version
|
95
|
+
hash: 3
|
96
|
+
segments:
|
97
|
+
- 0
|
57
98
|
version: "0"
|
58
|
-
version:
|
59
99
|
requirements: []
|
60
100
|
|
61
101
|
rubyforge_project:
|
62
|
-
rubygems_version: 1.3.
|
102
|
+
rubygems_version: 1.3.7
|
63
103
|
signing_key:
|
64
104
|
specification_version: 3
|
65
105
|
summary: Simple, internationalized and DRY page titles and headings for rails.
|
data/VERSION.yml
DELETED
data/rails/init.rb
DELETED