lwe-page_title_helper 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +19 -4
- data/Rakefile +2 -1
- data/VERSION.yml +1 -1
- data/lib/page_title_helper.rb +15 -8
- data/page_title_helper.gemspec +1 -1
- data/test/page_title_helper_test.rb +16 -10
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -61,8 +61,8 @@ prependor appended, or if it contains the account name etc. It uses a similar ap
|
|
61
61
|
Adding custom interpolations is as easy as defining a block, for example to access the current
|
62
62
|
controller:
|
63
63
|
|
64
|
-
PageTitleHelper.interpolates :controller do |
|
65
|
-
controller.
|
64
|
+
PageTitleHelper.interpolates :controller do |env|
|
65
|
+
env.controller.controller_name.humanize
|
66
66
|
end
|
67
67
|
|
68
68
|
page_title :format => ':title / :controller / :app' # => "Welcome back / Dashboard / My cool app"
|
@@ -77,16 +77,31 @@ To access just the title, without any magic app stuff interpolated or appended,
|
|
77
77
|
* <tt>page_title { ... }</tt> - not per se an option, but if a block is given the methods sets a
|
78
78
|
custom title and overwrites any DRY-I18n title.
|
79
79
|
* <tt>:app</tt> - specifiy the applications name, however it's recommended to define the
|
80
|
-
translation key <tt>:'app.name'</tt
|
81
|
-
|
80
|
+
translation key <tt>:'app.name'</tt> or use the the fallback behaviour for the humanzied
|
81
|
+
basename of +RAILS_ROOT+.
|
82
82
|
* <tt>:default</tt> - string which is displayed when no translation exists and no custom title
|
83
83
|
has been specified. Can also be set to a symbol or array to take advantage of
|
84
84
|
<tt>I18n.translate</tt>s <tt>:default</tt> option. (Default is <tt>:'app.tagline'</tt>)
|
85
85
|
* <tt>:format</tt> - defines the output format, accepts a string containing multiple interpolations,
|
86
86
|
see <i>More fun with <tt>:format</tt></i>. If set to +false+, just the current title is returned.
|
87
|
+
If <tt>:format => :app</tt> then just the application name is returned.
|
87
88
|
(Default is <tt>":app - :title"</tt>)
|
88
89
|
* <tt>:suffix</tt> - not happy with the fact that the translations must be named like
|
89
90
|
<tt>en -> contacts -> index -> title</tt>, but prefer e.g. them to be suffixed with
|
90
91
|
<tt>page_title</tt>? Then just set <tt>:suffix => :page_title</tt>. (Default <tt>:title</tt>)
|
91
92
|
|
93
|
+
If an option should be set globally it's possible to change the default options hash as follows:
|
94
|
+
|
95
|
+
PageTitleHelper.options[:format] = ':title / :app'
|
96
|
+
|
97
|
+
Note, currently it only makes sense to set <tt>:format</tt> and/or <tt>:default</tt> globally.
|
98
|
+
|
99
|
+
== Some (maybe useful) interpolations
|
100
|
+
|
101
|
+
# internationalized controller names, with fallback
|
102
|
+
PageTitleHelper.interpolates :controller do |env|
|
103
|
+
I18n.t env.controller.controller_path.tr('/','.') + '.controller', :default => env.controller.controller_name.humanize
|
104
|
+
end
|
105
|
+
|
106
|
+
== Licence and copyright
|
92
107
|
Copyright (c) 2009 Lukas Westermann (Zurich, Switzerland), released under the MIT license
|
data/Rakefile
CHANGED
data/VERSION.yml
CHANGED
data/lib/page_title_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
1
3
|
# PageTitleHelper
|
2
4
|
module PageTitleHelper
|
3
5
|
module Interpolations
|
@@ -16,12 +18,12 @@ module PageTitleHelper
|
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
|
-
def app(
|
20
|
-
options[:app] || I18n.translate(:'app.name', :default => File.basename(RAILS_ROOT).humanize)
|
21
|
+
def app(env)
|
22
|
+
env.options[:app] || I18n.translate(:'app.name', :default => File.basename(RAILS_ROOT).humanize)
|
21
23
|
end
|
22
24
|
|
23
|
-
def title(
|
24
|
-
title
|
25
|
+
def title(env)
|
26
|
+
env.title
|
25
27
|
end
|
26
28
|
end
|
27
29
|
|
@@ -45,21 +47,26 @@ module PageTitleHelper
|
|
45
47
|
content_for(:page_title) { yield }
|
46
48
|
return read_page_title_content_block
|
47
49
|
end
|
48
|
-
|
50
|
+
|
49
51
|
options = PageTitleHelper.options.merge(options || {})
|
50
52
|
options.assert_valid_keys(:app, :suffix, :default, :format)
|
53
|
+
|
54
|
+
# construct basic env
|
55
|
+
env = OpenStruct.new(:options => options, :view => self, :controller => self.controller)
|
56
|
+
|
51
57
|
# just return the applications name
|
52
|
-
return Interpolations.app(
|
58
|
+
return Interpolations.app(env) if options[:format] == :app
|
53
59
|
|
54
60
|
# read page title
|
55
61
|
page_title = read_page_title_content_block
|
56
62
|
page_title = I18n.translate(i18n_template_key(options[:suffix]), :default => options[:default]) if page_title.blank?
|
57
63
|
|
58
|
-
# return page title if format is set explicitly to false
|
64
|
+
# return page title if format is set explicitly set to false
|
59
65
|
return page_title if options[:format] == false
|
60
66
|
|
61
67
|
# else -> interpolate
|
62
|
-
|
68
|
+
env.title = page_title
|
69
|
+
Interpolations.interpolate options[:format], env
|
63
70
|
end
|
64
71
|
|
65
72
|
protected
|
data/page_title_helper.gemspec
CHANGED
@@ -18,27 +18,28 @@ class MockView
|
|
18
18
|
def content_for(sym, &block)
|
19
19
|
instance_variable_set('@content_for_' + sym.to_s, yield)
|
20
20
|
end
|
21
|
+
|
22
|
+
def controller; nil; end
|
21
23
|
end
|
22
24
|
|
23
25
|
I18n.backend.store_translations :en, :contacts => { :list => { :title => 'contacts.list.title' }}
|
24
26
|
I18n.backend.store_translations :en, :placeholder => 'Displaying {{name}}'
|
25
27
|
|
26
|
-
|
27
28
|
class PageTitleHelperTest < ActiveSupport::TestCase
|
28
29
|
test "interpolations" do
|
29
|
-
assert_equal 'Page title helper', PageTitleHelper::Interpolations.app(
|
30
|
-
assert_equal 'Appname', PageTitleHelper::Interpolations.app(
|
31
|
-
assert_equal 'untitled', PageTitleHelper::Interpolations.title('untitled'
|
30
|
+
assert_equal 'Page title helper', PageTitleHelper::Interpolations.app(OpenStruct.new(:options => {}))
|
31
|
+
assert_equal 'Appname', PageTitleHelper::Interpolations.app(OpenStruct.new(:options => { :app => 'Appname' }))
|
32
|
+
assert_equal 'untitled', PageTitleHelper::Interpolations.title(OpenStruct.new({:title => 'untitled'}))
|
32
33
|
end
|
33
34
|
|
34
35
|
test "adding custom interpolation" do
|
35
36
|
# extend Interpolations
|
36
|
-
PageTitleHelper.interpolates :app_reverse do |
|
37
|
-
app(
|
37
|
+
PageTitleHelper.interpolates :app_reverse do |env|
|
38
|
+
app(env).reverse.downcase
|
38
39
|
end
|
39
40
|
|
40
|
-
assert_equal "anna", PageTitleHelper::Interpolations.app_reverse(
|
41
|
-
assert_equal "ppa", PageTitleHelper::Interpolations.interpolate(':app_reverse',
|
41
|
+
assert_equal "anna", PageTitleHelper::Interpolations.app_reverse(OpenStruct.new(:options => { :app => 'Anna' }))
|
42
|
+
assert_equal "ppa", PageTitleHelper::Interpolations.interpolate(':app_reverse', OpenStruct.new(:options => { :app => 'app' }))
|
42
43
|
end
|
43
44
|
|
44
45
|
test "setting title to 'foo' returns 'foo'" do
|
@@ -52,9 +53,14 @@ class PageTitleHelperTest < ActiveSupport::TestCase
|
|
52
53
|
assert_equal 'Page title helper - contacts.list.title', view.page_title
|
53
54
|
end
|
54
55
|
|
55
|
-
test "printing app name only if :
|
56
|
+
test "printing app name only if :format => :app" do
|
57
|
+
view = MockView.new
|
58
|
+
assert_equal 'Page title helper', view.page_title(:format => :app)
|
59
|
+
end
|
60
|
+
|
61
|
+
test "printing custom app name if :app defined and :format => :app" do
|
56
62
|
view = MockView.new
|
57
|
-
assert_equal
|
63
|
+
assert_equal "Some app", view.page_title(:app => 'Some app', :format => :app)
|
58
64
|
end
|
59
65
|
|
60
66
|
test "custom formatting options" do
|