page_title_helper 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ doc/
2
+ pkg/
3
+ .yardoc
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 by Lukas Westermann (Zurich, Switzerland)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,170 @@
1
+ # Page title helper
2
+
3
+ Ever wondered if there was an easier and DRY-way to set your page titles (and/or headings). Backed
4
+ by Rails (only tested on 2.3.x) and it's new `I18n`-class the solution is a simple helper method.
5
+
6
+ In your layout add this to your `<head>-section`:
7
+
8
+ ...
9
+ <title><%=h page_title %></title>
10
+ ...
11
+
12
+ That's it. Now just add your translations, to the locales, in e.g. `config/locales/en.yml`:
13
+
14
+ en:
15
+ contacts:
16
+ index:
17
+ title: "Contacts"
18
+
19
+ When `contacs/index.html.erb` is rendered, the key `:en, :contacts, :index, :title`
20
+ is looked up and printed, together with the applications basename, like: `My cool app - Contacts`.
21
+ The format etc. is of course configurable, just head down to the options.
22
+
23
+ ## Customize titles
24
+
25
+ Need a custom title, or need to fill in some placeholders? Just provide a block, in e.g.
26
+ `contacts/show.html.erb` the requirement is to display the contacts name in the
27
+ `<title>-tag`as well as in the heading?
28
+
29
+ <h1><%=h page_title { @contact.name } %></h1>
30
+
31
+ A call to `page_title` will now return the contacts name, neat :) if for example the
32
+ `<h1>` does not match the +title+, then
33
+ well, just do something like:
34
+
35
+ <% page_title { @contact.name + " (" + @contact.company.name + ")" } %>
36
+ <h1><%=h @contact.name %></h1>
37
+
38
+ Guess, that's it. Of course it's also possible to use `translate` within the `page_title` block, so
39
+ to translate customzied titles, like:
40
+
41
+ # in config/locales/en.yml:
42
+ en:
43
+ dashboard:
44
+ index:
45
+ title: "Welcome back, {{name}}"
46
+
47
+ # in app/views/dashboard/index.html.erb:
48
+ <h1><%=h page_title { t '.title', :name => @user.first_name } %></h1>
49
+
50
+ Btw - a helpful rule-of-thumb: if `page_title` is used with a block a title is **defined**,
51
+ if it's used without the current title is rendered.
52
+
53
+ ## More fun with <tt>:format</tt>
54
+
55
+ The `:format` option is used to specify how a title is formatted, i.e. if the app name is
56
+ prependor appended, or if it contains the account name etc. It uses a similar approach as
57
+ paperclip's path interpolations:
58
+
59
+ page_title :format => ':title / :app' # => "Contacts / My cool app"
60
+
61
+ Adding custom interpolations is as easy as defining a block, for example to access the current
62
+ controller:
63
+
64
+ PageTitleHelper.interpolates :controller do |env|
65
+ env.controller.controller_name.humanize
66
+ end
67
+
68
+ page_title :format => ':title / :controller / :app' # => "Welcome back / Dashboard / My cool app"
69
+
70
+ To access just the title, without any magic app stuff interpolated or appended, use:
71
+
72
+ page_title { "untitled" }
73
+ page_title :format => false # => "untitled"
74
+
75
+ Need a custom format for a single title? Just return an array:
76
+
77
+ # in the view:
78
+ <h1><%= h(page_title { [@contact.name, ":title from :company - :app"] } %></h1> # => <h1>Franz Meyer</h1>
79
+
80
+ # in the <head>
81
+ <title><%= h(page_title) %></title> # => this time it will use custom title like "Franz Meyer from ABC Corp. - MyCoolApp"
82
+
83
+ To streamline that feature a bit and simplify reuse of often used formats, it's now possible to define format aliases like:
84
+
85
+ # in an initializer:
86
+ PageTitleHelper.formats[:with_company] = ":title from :company - :app"
87
+ PageTitleHelper.formats[:promo] = ":app - :title" # show app first for promo pages :)
88
+
89
+ # then in the view to display a contact...
90
+ page_title { [@contact.name, :with_company] }
91
+
92
+ # ...or for the promo page via config/locales/en.yml (!)
93
+ en:
94
+ pages:
95
+ features:
96
+ title:
97
+ - "Features comparison"
98
+ - !ruby/sym promo
99
+
100
+
101
+ Pretty, cool, aint it? The special `:format => :app` works also with the `formats` hash. Then there is also a
102
+ `:default` format, which should be used to override the default format.
103
+
104
+ ## All options - explained
105
+
106
+ <table>
107
+ <tr>
108
+ <th>Option</th><th>Description</th><th>Default</th><th>Values</th>
109
+ </tr>
110
+ <tr>
111
+ <td><tt>:app</tt></td>
112
+ <td>Specify the applications name, however it's
113
+ recommended to define the translation key <tt>:'app.name'</tt>.</td>
114
+ <td>Inflected from <tt>RAILS_ROOT</tt></td>
115
+ <td>string</td>
116
+ </tr>
117
+ <tr>
118
+ <td><tt>:default</tt></td>
119
+ <td>String which is displayed when no translation exists and no custom title
120
+ has been specified. Can also be set to a symbol or array to take advantage of
121
+ <tt>I18n.translate</tt>s <tt>:default</tt> option.</td>
122
+ <td><tt>:'app.tagline'</tt></td>
123
+ <td>string, symbol or array of those</td>
124
+ </tr>
125
+ <tr>
126
+ <td><tt>:format</tt></td>
127
+ <td>Defines the output format, accepts a string containing multiple interpolations, or
128
+ a symbol to a format alias, see <i>More fun with <tt>:format</tt></i>. If set to
129
+ +false+, just the current title is returned.</td>
130
+ <td><tt>:default</tt></td>
131
+ <td>string, symbol</td>
132
+ </tr>
133
+ <tr>
134
+ <td><tt>:suffix</tt></td>
135
+ <td>Not happy with the fact that the translations must be named like
136
+ <tt>en -> contacts -> index -> title</tt>, but prefer e.g. them to be suffixed with
137
+ <tt>page_title</tt>? Then just set <tt>:suffix => :page_title</tt>.</td>
138
+ <td><tt>:title</tt></td>
139
+ <td>symbol or string</td>
140
+ </tr>
141
+ </table>
142
+ </p>
143
+
144
+ If an option should be set globally it's possible to change the default options hash as follows:
145
+
146
+ PageTitleHelper.options[:suffix] = :page_title
147
+
148
+ Note, currently it only makes sense to set `:default` and/or `:page_title` globally.
149
+ To add or change formats use:
150
+
151
+ # change the default format used (if no format is specified):
152
+ PageTitleHelper.formats[:default] = ":title // :app"
153
+
154
+ # add a custom format alias (which can be used with page_title(:format => :promo))
155
+ PageTitleHelper.formats[:promo] = ":app // :title"
156
+
157
+ _Note:_ it's recommended to add this kind of stuff to an initializer.
158
+
159
+ ## A (maybe useful) interpolation
160
+
161
+ The internationalized controller name, with fallback to just display the humanized name:
162
+
163
+ PageTitleHelper.interpolates :controller do |env|
164
+ I18n.t env.controller.controller_path.tr('/','.') + '.controller', :default => env.controller.controller_name.humanize
165
+ end
166
+
167
+ _Note:_ Put this kind of stuff into an initilizer, like `config/initializers/page_title.rb` or someting like that.
168
+
169
+ ## Licence and copyright
170
+ Copyright (c) 2009 Lukas Westermann (Zurich, Switzerland), released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,72 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'yard'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ begin
10
+ require 'jeweler'
11
+ Jeweler::Tasks.new do |gemspec|
12
+ gemspec.name = "page_title_helper"
13
+ gemspec.summary = "Simple, internationalized and DRY page titles and headings for rails."
14
+ gemspec.email = "lukas.westermann@gmail.com"
15
+ gemspec.homepage = "http://github.com/lwe/page_title_helper"
16
+ gemspec.authors = ["Lukas Westermann"]
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
21
+ end
22
+
23
+ desc 'Test the page_title_helper plugin.'
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'test'
26
+ t.pattern = 'test/**/*_test.rb'
27
+ t.verbose = true
28
+ end
29
+
30
+ desc 'Generate documentation for gravatarify. (requires yard)'
31
+ YARD::Rake::YardocTask.new(:doc) do |t|
32
+ t.files = ['lib/**/*.rb']
33
+ t.options = [
34
+ "--readme", "README.md",
35
+ "--title", "page_title_helper API Documentation"
36
+ ]
37
+ end
38
+
39
+ namespace :metrics do
40
+ desc 'Report all metrics, i.e. stats and code coverage.'
41
+ task :all => [:stats, :coverage]
42
+
43
+ desc 'Report code statistics for library and tests to shell.'
44
+ task :stats do |t|
45
+ require 'code_statistics'
46
+ dirs = {
47
+ 'Libraries' => 'lib',
48
+ 'Unit tests' => 'test'
49
+ }.map { |name,dir| [name, File.join(File.dirname(__FILE__), dir)] }
50
+ CodeStatistics.new(*dirs).to_s
51
+ end
52
+
53
+ desc 'Report code coverage to HTML (doc/coverage) and shell (requires rcov).'
54
+ task :coverage do |t|
55
+ rm_f "doc/coverage"
56
+ mkdir_p "doc/coverage"
57
+ rcov = %(rcov -Ilib:test --exclude '\/gems\/' -o doc/coverage -T test/*_test.rb )
58
+ system rcov
59
+ end
60
+ end
61
+
62
+ desc 'Start IRB console with loaded test/test_helper.rb and PageTitleHelper.'
63
+ task :console do |t|
64
+ chdir File.dirname(__FILE__)
65
+ exec 'irb -Ilib -r test/test_helper.rb -r page_title_helper'
66
+ end
67
+
68
+ desc 'Clean up generated files.'
69
+ task :clean do |t|
70
+ FileUtils.rm_rf "doc"
71
+ FileUtils.rm_rf "pkg"
72
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 7
4
+ :patch: 0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'lib', 'page_title_helper')
@@ -0,0 +1,116 @@
1
+ # PageTitleHelper provides an +ActionView+ helper method to simplify adding
2
+ # custom titles to pages.
3
+ #
4
+ # Author:: Lukas Westermann
5
+ # Copyright:: Copyright (c) 2009 Lukas Westermann (Zurich, Switzerland)
6
+ # Licence:: MIT-Licence (http://www.opensource.org/licenses/mit-license.php)
7
+ #
8
+ # See documentation for +page_title+ for usage examples and more informations.
9
+
10
+ # PageTitleHelper
11
+ module PageTitleHelper
12
+ module Interpolations
13
+ # Represents the environment which is passed into each interpolation call.
14
+ class Env < Struct.new(:options, :view, :controller, :title); end
15
+
16
+ extend self
17
+
18
+ def self.interpolate(pattern, *args)
19
+ instance_methods(false).sort.reverse.inject(pattern.to_s.dup) do |result, tag|
20
+ result.gsub(/:#{tag}/) do |match|
21
+ send(tag, *args)
22
+ end
23
+ end
24
+ end
25
+
26
+ def app(env)
27
+ env.options[:app] || I18n.translate(:'app.name', :default => File.basename(RAILS_ROOT).humanize)
28
+ end
29
+
30
+ def title(env)
31
+ env.title
32
+ end
33
+ end
34
+
35
+ # Add new, custom, interpolation.
36
+ def self.interpolates(key, &block)
37
+ Interpolations.send(:define_method, key, &block)
38
+ end
39
+
40
+ # Default options, which are globally referenced and can
41
+ # be changed globally, which might be useful in some cases.
42
+ def self.options
43
+ @options ||= {
44
+ :format => :default,
45
+ :default => :'app.tagline',
46
+ :suffix => :title
47
+ }
48
+ end
49
+
50
+ # Defined alias formats, pretty useful.
51
+ def self.formats
52
+ @formats ||= {
53
+ :app => ":app",
54
+ :default => ':title - :app',
55
+ :title => ":title"
56
+ }
57
+ end
58
+
59
+ def page_title(options = nil, &block)
60
+ if block_given? # define title
61
+ content_for(:page_title) { yield }
62
+ return (title = read_page_title_content_block).is_a?(Array) ? title.first : title
63
+ end
64
+
65
+ options = PageTitleHelper.options.merge(options || {}).symbolize_keys!
66
+ options[:format] ||= :title # handles :format => false
67
+ options.assert_valid_keys(:app, :suffix, :default, :format)
68
+
69
+ # construct basic env to pass around
70
+ env = Interpolations::Env.new(options, self, self.controller)
71
+
72
+ # read page title and split into 'real' title and customized format
73
+ env.title = read_page_title_content_block || I18n.translate(i18n_template_key(options[:suffix]), :default => options[:default])
74
+ env.title, options[:format] = *(env.title << options[:format]) if env.title.is_a?(Array)
75
+
76
+ # handle format aliases
77
+ format = options[:format]
78
+ format = PageTitleHelper.formats[format] if PageTitleHelper.formats.include?(format)
79
+
80
+ # interpolate format
81
+ Interpolations.interpolate format, env
82
+ end
83
+
84
+ protected
85
+
86
+ # Access <tt>@content_for_page_title</tt> variable, though this is a tad
87
+ # hacky, because... what if they (the rails guys) change the behaviour of
88
+ # <tt>content_for</tt>? Well, in Rails 2.3.x it works so far.
89
+ #
90
+ # But to simplify compatibility with later versions, this method kinda abstracts
91
+ # away access to the content within a <tt>content_for</tt> block.
92
+ def read_page_title_content_block
93
+ instance_variable_get(:'@content_for_page_title')
94
+ end
95
+
96
+ # Access +ActionView+s internal <tt>@_first_render</tt> variable, to access
97
+ # template first rendered, this is to help create the DRY-I18n-titles magic,
98
+ # and also kind of a hack, because this really seems to be some sort if
99
+ # internal variable, that's why it's "abstracted" away as well.
100
+ #
101
+ # Also ensure that the extensions (like <tt>.html.erb</tt> or
102
+ # <tt>.html.haml</tt>) have been stripped of and translated in the sense
103
+ # of converting <tt>/</tt> to <tt>.</tt>.
104
+ def first_render_path_translated
105
+ @_first_render.template_path.gsub(/\.[^\/]*\Z/, '').tr('/', '.')
106
+ end
107
+
108
+ def i18n_template_key(suffix = nil)
109
+ first_render_path_translated + (suffix.present? ? ".#{suffix}" : "")
110
+ end
111
+ end
112
+
113
+ # tie stuff together
114
+ if Object.const_defined?('ActionView')
115
+ ActionView::Base.send(:include, PageTitleHelper)
116
+ end
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{page_title_helper}
8
+ s.version = "0.7.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Lukas Westermann"]
12
+ s.date = %q{2009-09-29}
13
+ s.email = %q{lukas.westermann@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "LICENSE",
21
+ "README.md",
22
+ "Rakefile",
23
+ "VERSION.yml",
24
+ "init.rb",
25
+ "lib/page_title_helper.rb",
26
+ "page_title_helper.gemspec",
27
+ "test/en.yml",
28
+ "test/en_wohaapp.yml",
29
+ "test/mocks.rb",
30
+ "test/multiple_formats_test.rb",
31
+ "test/page_title_helper_test.rb",
32
+ "test/test_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/lwe/page_title_helper}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.5}
38
+ s.summary = %q{Simple, internationalized and DRY page titles and headings for rails.}
39
+ s.test_files = [
40
+ "test/mocks.rb",
41
+ "test/multiple_formats_test.rb",
42
+ "test/page_title_helper_test.rb",
43
+ "test/test_helper.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
51
+ else
52
+ end
53
+ else
54
+ end
55
+ end
data/test/en.yml ADDED
@@ -0,0 +1,11 @@
1
+ en:
2
+ contacts:
3
+ list:
4
+ title: contacts.list.title
5
+ page_title: custom contacts title
6
+ myhaml:
7
+ title: this is haml!
8
+ placeholder: Displaying {{name}}
9
+ app:
10
+ tagline: Default
11
+ other_tagline: Other default
@@ -0,0 +1,12 @@
1
+ en:
2
+ app:
3
+ name: Wohaapp
4
+ pages:
5
+ features:
6
+ title:
7
+ - "Feature comparison"
8
+ - !ruby/sym promo
9
+ signup:
10
+ title:
11
+ - "Sign up"
12
+ - ":title for :app now!"
data/test/mocks.rb ADDED
@@ -0,0 +1,20 @@
1
+ class MockRender
2
+ def initialize(template_path = 'contacts/index.html.erb')
3
+ @template_path = template_path
4
+ end
5
+ def template_path; @template_path; end
6
+ end
7
+
8
+ class MockView
9
+ include PageTitleHelper
10
+
11
+ def initialize(template_path = 'contacts/index.html.erb')
12
+ @_first_render = MockRender.new template_path
13
+ end
14
+
15
+ def content_for(sym, &block)
16
+ instance_variable_set('@content_for_' + sym.to_s, yield)
17
+ end
18
+
19
+ def controller; nil; end
20
+ end
@@ -0,0 +1,71 @@
1
+ require 'test_helper'
2
+ require 'page_title_helper'
3
+ require 'mocks'
4
+
5
+ class MultipleFormatsTest < ActiveSupport::TestCase
6
+ context "#page_title supporting multiple formats through arrays" do
7
+ setup do
8
+ @view = MockView.new
9
+ end
10
+
11
+ should "accept an array passed in the page_title block and use the second argument as format" do
12
+ @view.page_title { ["Oh my...!", ":title // :app"] }
13
+ assert_equal "Oh my...! // Page title helper", @view.page_title
14
+ end
15
+
16
+ should "still return title as string and not the array" do
17
+ assert_equal "Oh my...!", @view.page_title { ["Oh my...!", ":title // :app"] }
18
+ end
19
+ end
20
+
21
+ context "#page_title with format aliases" do
22
+ setup do
23
+ PageTitleHelper.formats[:myformat] = ":title <-> :app"
24
+ @view = MockView.new
25
+ end
26
+
27
+ should "have a default alias named :app" do
28
+ assert_equal "Page title helper", @view.page_title(:format => :app)
29
+ end
30
+
31
+ should "allow custom aliases to be defined and used" do
32
+ @view.page_title { "Test" }
33
+ assert_equal "Test <-> Page title helper", @view.page_title(:format => :myformat)
34
+ end
35
+
36
+ should "fallback to default format, if array is not big enough (i.e. only contains single element...)" do
37
+ assert_equal "Test", @view.page_title { ["Test"] }
38
+ assert_equal "Test - Page title helper", @view.page_title
39
+ end
40
+
41
+ context "used with the array block" do
42
+ should "also allow aliases returned in that array thingy" do
43
+ assert_equal "Test", @view.page_title { ["Test", :myformat] }
44
+ assert_equal "Test <-> Page title helper", @view.page_title
45
+ end
46
+
47
+ should "override locally supplied :format arguments" do
48
+ assert_equal "Something", @view.page_title { ["Something", "* * * :title * * *"] }
49
+ assert_equal "* * * Something * * *", @view.page_title(:format => "-= :title =-") # yeah, using x-tra ugly titles :)
50
+ end
51
+ end
52
+ end
53
+
54
+ context "#page_title, aliases and YAML" do
55
+ setup do
56
+ I18n.load_path = [File.join(File.dirname(__FILE__), "en_wohaapp.yml")]
57
+ I18n.reload!
58
+ PageTitleHelper.formats[:promo] = ":app > :title"
59
+ end
60
+
61
+ should "allow to overide format through YAML" do
62
+ @view = MockView.new('pages/features.html.haml')
63
+ assert_equal 'Wohaapp > Feature comparison', @view.page_title
64
+ end
65
+
66
+ should "handle raw string formats from YAML as well" do
67
+ @view = MockView.new('pages/signup.html.haml')
68
+ assert_equal 'Sign up for Wohaapp now!', @view.page_title
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,109 @@
1
+ require 'test_helper'
2
+ require 'page_title_helper'
3
+ require 'ostruct'
4
+ require 'mocks'
5
+
6
+
7
+ class PageTitleHelperTest < ActiveSupport::TestCase
8
+ def setup
9
+ I18n.load_path = [File.join(File.dirname(__FILE__), 'en.yml')]
10
+ I18n.reload!
11
+ end
12
+
13
+ test "interpolations" do
14
+ assert_equal 'Page title helper', PageTitleHelper::Interpolations.app(OpenStruct.new(:options => {}))
15
+ assert_equal 'Appname', PageTitleHelper::Interpolations.app(OpenStruct.new(:options => { :app => 'Appname' }))
16
+ assert_equal 'untitled', PageTitleHelper::Interpolations.title(OpenStruct.new({:title => 'untitled'}))
17
+ end
18
+
19
+ test "adding custom interpolation" do
20
+ # extend Interpolations
21
+ PageTitleHelper.interpolates(:app_reverse) { |env| app(env).reverse.downcase }
22
+
23
+ assert_equal "anna", PageTitleHelper::Interpolations.app_reverse(OpenStruct.new(:options => { :app => 'Anna' }))
24
+ assert_equal "ppa", PageTitleHelper::Interpolations.interpolate(':app_reverse', OpenStruct.new(:options => { :app => 'app' }))
25
+ end
26
+
27
+ test "that interpolations which are equaly named do match in correct order (longest first)" do
28
+ PageTitleHelper.interpolates(:foobar) { "foobar" }
29
+ PageTitleHelper.interpolates(:foobar_test) { "foobar_test" }
30
+ PageTitleHelper.interpolates(:title_foobar) { "title_foobar" }
31
+
32
+ assert_equal "title_foobar / foobar_test / foobar / foobar_x", PageTitleHelper::Interpolations.interpolate(":title_foobar / :foobar_test / :foobar / :foobar_x", nil)
33
+ end
34
+
35
+
36
+ test "setting title to 'foo' returns 'foo'" do
37
+ view = MockView.new
38
+ view.page_title { "foo" }
39
+ assert_equal 'foo - Page title helper', view.page_title
40
+ end
41
+
42
+ test "reading defaults from I18n" do
43
+ view = MockView.new 'contacts/list.html.erb'
44
+ assert_equal 'contacts.list.title - Page title helper', view.page_title
45
+ end
46
+
47
+ test "printing app name only if :format => :app" do
48
+ view = MockView.new
49
+ assert_equal 'Page title helper', view.page_title(:format => :app)
50
+ end
51
+
52
+ test "printing custom app name if :app defined and :format => :app" do
53
+ view = MockView.new
54
+ assert_equal "Some app", view.page_title(:app => 'Some app', :format => :app)
55
+ end
56
+
57
+ test "custom formatting options" do
58
+ view = MockView.new
59
+ view.page_title { "test" }
60
+
61
+ assert_equal "Some app :: test", view.page_title(:app => "Some app", :format => ':app :: :title')
62
+ assert_equal "Some app / test", view.page_title(:format => 'Some app / :title')
63
+ end
64
+
65
+ test "return value of block to be used as heading" do
66
+ view = MockView.new
67
+ assert_equal "untitled", view.page_title { "untitled" }
68
+ end
69
+
70
+ test "calling :format => false returns just current title, without any interpolations etc." do
71
+ view = MockView.new
72
+ view.page_title { "untitled" }
73
+ assert_equal "untitled", view.page_title(:format => false)
74
+
75
+ i18n_view = MockView.new 'contacts/list.html.erb'
76
+ assert_equal "contacts.list.title", i18n_view.page_title(:format => false)
77
+ end
78
+
79
+ test "custom title using a translation with a placeholder" do
80
+ view = MockView.new
81
+ view.page_title { I18n.t :placeholder, :name => 'Bella' }
82
+ assert_equal "Displaying Bella - Page title helper", view.page_title
83
+ end
84
+
85
+ test "render translated :'app.tagline' if no title is available" do
86
+ view = MockView.new 'view/does/not_exist.html.erb'
87
+ assert_equal "Default - Page title helper", view.page_title
88
+ end
89
+
90
+ test "render custom 'default' string, if title is not available" do
91
+ view = MockView.new 'view/does/not_exist.html.erb'
92
+ assert_equal 'Some default - Page title helper', view.page_title(:default => 'Some default')
93
+ end
94
+
95
+ test "render custom default translation, if title is not available" do
96
+ view = MockView.new 'view/does/not_exist.html.erb'
97
+ assert_equal 'Other default - Page title helper', view.page_title(:default => :'app.other_tagline')
98
+ end
99
+
100
+ test "render auto-title using custom suffix 'page_title'" do
101
+ view = MockView.new 'contacts/list.html.erb'
102
+ assert_equal 'custom contacts title - Page title helper', view.page_title(:suffix => :page_title)
103
+ end
104
+
105
+ test "ensure that it works with other template engines, like .html.haml" do
106
+ view = MockView.new('contacts/myhaml.html.haml')
107
+ assert_equal 'this is haml! - Page title helper', view.page_title
108
+ end
109
+ end
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'action_view'
4
+
5
+ unless Object.const_defined?('IRB')
6
+ require 'active_support/test_case'
7
+ require 'shoulda'
8
+ require 'rr'
9
+ # enable RR to use all the mocking magic, uweee!
10
+ Test::Unit::TestCase.send(:include, RR::Adapters::TestUnit)
11
+ end
12
+
13
+ ROOT = File.expand_path File.dirname(File.dirname(__FILE__))
14
+ RAILS_ROOT = '/this/is/just/for/testing/page_title_helper'
15
+ RAILS_ENV = 'test'
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: page_title_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.0
5
+ platform: ruby
6
+ authors:
7
+ - Lukas Westermann
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-29 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: lukas.westermann@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.md
25
+ files:
26
+ - .gitignore
27
+ - LICENSE
28
+ - README.md
29
+ - Rakefile
30
+ - VERSION.yml
31
+ - init.rb
32
+ - lib/page_title_helper.rb
33
+ - page_title_helper.gemspec
34
+ - test/en.yml
35
+ - test/en_wohaapp.yml
36
+ - test/mocks.rb
37
+ - test/multiple_formats_test.rb
38
+ - test/page_title_helper_test.rb
39
+ - test/test_helper.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/lwe/page_title_helper
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.5
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Simple, internationalized and DRY page titles and headings for rails.
68
+ test_files:
69
+ - test/mocks.rb
70
+ - test/multiple_formats_test.rb
71
+ - test/page_title_helper_test.rb
72
+ - test/test_helper.rb