page_title_helper 1.0.1 → 5.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +79 -0
- data/CODE_OF_CONDUCT.md +77 -0
- data/Gemfile +5 -0
- data/README.md +201 -155
- data/Rakefile +8 -62
- data/lib/page_title_helper.rb +56 -52
- data/lib/page_title_helper/version.rb +5 -0
- data/page_title_helper.gemspec +30 -0
- data/test/en.yml +11 -1
- data/test/multiple_formats_test.rb +50 -50
- data/test/page_title_helper_test.rb +89 -71
- data/test/test_helper.rb +33 -13
- metadata +112 -43
- data/.gitignore +0 -4
- data/VERSION.yml +0 -4
- data/rails/init.rb +0 -4
data/Rakefile
CHANGED
@@ -1,24 +1,14 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
2
4
|
require 'rake/testtask'
|
3
|
-
require 'rake/rdoctask'
|
4
|
-
require 'yard'
|
5
5
|
|
6
|
-
|
7
|
-
task :default => :test
|
6
|
+
include Rake::DSL
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
8
|
+
Bundler::GemHelper.install_tasks
|
9
|
+
|
10
|
+
desc 'Default: run unit tests.'
|
11
|
+
task default: :test
|
22
12
|
|
23
13
|
desc 'Test the page_title_helper plugin.'
|
24
14
|
Rake::TestTask.new(:test) do |t|
|
@@ -26,47 +16,3 @@ Rake::TestTask.new(:test) do |t|
|
|
26
16
|
t.pattern = 'test/**/*_test.rb'
|
27
17
|
t.verbose = true
|
28
18
|
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/lib/page_title_helper.rb
CHANGED
@@ -1,104 +1,108 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# PageTitleHelper provides an +ActionView+ helper method to simplify adding
|
2
4
|
# custom titles to pages.
|
3
5
|
#
|
4
6
|
# Author:: Lukas Westermann
|
5
7
|
# Copyright:: Copyright (c) 2009 Lukas Westermann (Zurich, Switzerland)
|
6
|
-
# Licence:: MIT-Licence (
|
8
|
+
# Licence:: MIT-Licence (https://www.opensource.org/licenses/mit-license.php)
|
7
9
|
#
|
8
10
|
# See documentation for +page_title+ for usage examples and more informations.
|
11
|
+
require 'active_support'
|
9
12
|
|
10
13
|
# PageTitleHelper
|
11
14
|
module PageTitleHelper
|
12
|
-
|
13
|
-
# http://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/interpolations.rb
|
15
|
+
# https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/interpolations.rb
|
14
16
|
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
17
|
extend self
|
19
|
-
|
18
|
+
|
20
19
|
def self.interpolate(pattern, *args)
|
21
20
|
instance_methods(false).sort.reverse.inject(pattern.to_s.dup) do |result, tag|
|
22
|
-
result.gsub(/:#{tag}/) do |
|
21
|
+
result.gsub(/:#{tag}/) do |_match|
|
23
22
|
send(tag, *args)
|
24
23
|
end
|
25
24
|
end
|
26
25
|
end
|
27
|
-
|
26
|
+
|
28
27
|
def app(env)
|
29
|
-
env
|
28
|
+
env[:app] || I18n.translate('app.name', default: File.basename(Rails.root).humanize)
|
30
29
|
end
|
31
|
-
|
30
|
+
|
32
31
|
def title(env)
|
33
|
-
env
|
32
|
+
env[:title]
|
34
33
|
end
|
35
34
|
end
|
36
|
-
|
35
|
+
|
37
36
|
# Add new, custom, interpolation.
|
38
37
|
def self.interpolates(key, &block)
|
39
38
|
Interpolations.send(:define_method, key, &block)
|
40
39
|
end
|
41
|
-
|
40
|
+
|
42
41
|
# Default options, which are globally referenced and can
|
43
42
|
# be changed globally, which might be useful in some cases.
|
44
43
|
def self.options
|
45
44
|
@options ||= {
|
46
|
-
:
|
47
|
-
:
|
48
|
-
:suffix => :title
|
45
|
+
format: :default,
|
46
|
+
default: :'app.tagline'
|
49
47
|
}
|
50
48
|
end
|
51
|
-
|
49
|
+
|
52
50
|
# Defined alias formats, pretty useful.
|
53
51
|
def self.formats
|
54
52
|
@formats ||= {
|
55
|
-
:
|
56
|
-
:
|
57
|
-
:
|
53
|
+
app: ':app',
|
54
|
+
default: ':title - :app',
|
55
|
+
title: ':title'
|
58
56
|
}
|
59
57
|
end
|
60
|
-
|
58
|
+
|
61
59
|
# Specify page title
|
62
60
|
def page_title!(*args)
|
63
61
|
@_page_title = args.size > 1 ? args : args.first
|
64
62
|
@_page_title.is_a?(Array) ? @_page_title.first : @_page_title
|
65
63
|
end
|
66
|
-
|
67
|
-
def page_title(options = nil
|
64
|
+
|
65
|
+
def page_title(options = nil)
|
68
66
|
return page_title!(yield) if block_given? # define title
|
69
|
-
|
67
|
+
|
70
68
|
options = PageTitleHelper.options.merge(options || {}).symbolize_keys!
|
71
69
|
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
|
-
|
70
|
+
options.assert_valid_keys(:app, :default, :format)
|
71
|
+
|
77
72
|
# read page title and split into 'real' title and customized format
|
78
|
-
|
79
|
-
|
80
|
-
|
73
|
+
title = @_page_title ||= page_title_from_translation(options[:default])
|
74
|
+
title, options[:format] = *(title << options[:format]) if title.is_a?(Array)
|
75
|
+
|
81
76
|
# handle format aliases
|
82
|
-
format = options
|
83
|
-
|
84
|
-
|
77
|
+
format = options.delete(:format)
|
78
|
+
if PageTitleHelper.formats.include?(format)
|
79
|
+
format = PageTitleHelper.formats[format]
|
80
|
+
end
|
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
|
-
|
91
|
-
# Find
|
92
|
-
#
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
90
|
+
|
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 }
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'page_title_helper/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'page_title_helper'
|
7
|
+
s.version = PageTitleHelper::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.summary = 'Simple, internationalized and DRY page titles and headings for Rails.'
|
10
|
+
s.description = 'Simple, internationalized and DRY page titles and headings for Rails.'
|
11
|
+
|
12
|
+
s.required_ruby_version = '>= 2.5.0'
|
13
|
+
|
14
|
+
s.authors = ['Lukas Westermann']
|
15
|
+
s.email = ['lukas.westermann@gmail.com']
|
16
|
+
s.homepage = 'https://github.com/lwe/page_title_helper'
|
17
|
+
|
18
|
+
s.files = %w{Gemfile LICENSE README.md CODE_OF_CONDUCT.md CHANGELOG.md Rakefile page_title_helper.gemspec} + Dir['**/*.{rb,yml}']
|
19
|
+
s.test_files = s.files.grep(%r{^(test|spec)/})
|
20
|
+
s.require_path = 'lib'
|
21
|
+
|
22
|
+
s.license = 'MIT'
|
23
|
+
|
24
|
+
s.add_dependency 'rails', '>= 5.2.0', '< 6.2'
|
25
|
+
|
26
|
+
s.add_development_dependency 'rake', '>= 10.3.2'
|
27
|
+
s.add_development_dependency 'shoulda'
|
28
|
+
s.add_development_dependency 'rubocop', '~> 0.82.0'
|
29
|
+
s.add_development_dependency 'rubocop-rails', '~> 2.5.2'
|
30
|
+
end
|
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
|
@@ -1,73 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
1
4
|
require 'test_helper'
|
2
|
-
require 'page_title_helper'
|
3
5
|
|
4
|
-
class MultipleFormatsTest < ActiveSupport::TestCase
|
5
|
-
context
|
6
|
+
class MultipleFormatsTest < ActiveSupport::TestCase
|
7
|
+
context '#page_title supporting multiple formats through arrays' do
|
6
8
|
setup do
|
7
|
-
@view =
|
8
|
-
@view.template = ActionView::Template.new "contacts/list.html.erb"
|
9
|
+
@view = TestView.new('contacts', 'list')
|
9
10
|
end
|
10
|
-
|
11
|
-
should
|
12
|
-
@view.page_title { [
|
13
|
-
assert_equal
|
11
|
+
|
12
|
+
should 'accept an array passed in the page_title block and use the second argument as format' do
|
13
|
+
@view.page_title { ['Oh my...!', ':title // :app'] }
|
14
|
+
assert_equal 'Oh my...! // Page title helper', @view.page_title
|
14
15
|
end
|
15
|
-
|
16
|
-
should
|
17
|
-
assert_equal
|
16
|
+
|
17
|
+
should 'still return title as string and not the array' do
|
18
|
+
assert_equal('Oh my...!', @view.page_title { ['Oh my...!', ':title // :app'] })
|
18
19
|
end
|
19
20
|
end
|
20
|
-
|
21
|
-
context
|
21
|
+
|
22
|
+
context '#page_title with format aliases' do
|
22
23
|
setup do
|
23
|
-
PageTitleHelper.formats[:myformat] =
|
24
|
-
@view =
|
25
|
-
@view.template = ActionView::Template.new "contacts/list.html.erb"
|
24
|
+
PageTitleHelper.formats[:myformat] = ':title <-> :app'
|
25
|
+
@view = TestView.new('contacts', 'list')
|
26
26
|
end
|
27
|
-
|
28
|
-
should
|
29
|
-
assert_equal
|
27
|
+
|
28
|
+
should 'have a default alias named :app' do
|
29
|
+
assert_equal 'Page title helper', @view.page_title(format: :app)
|
30
30
|
end
|
31
|
-
|
32
|
-
should
|
33
|
-
@view.page_title {
|
34
|
-
assert_equal
|
31
|
+
|
32
|
+
should 'allow custom aliases to be defined and used' do
|
33
|
+
@view.page_title { 'Test' }
|
34
|
+
assert_equal 'Test <-> Page title helper', @view.page_title(format: :myformat)
|
35
35
|
end
|
36
|
-
|
37
|
-
should
|
38
|
-
assert_equal
|
39
|
-
assert_equal
|
36
|
+
|
37
|
+
should 'fallback to default format, if array is not big enough (i.e. only contains single element...)' do
|
38
|
+
assert_equal('Test', @view.page_title { ['Test'] })
|
39
|
+
assert_equal 'Test - Page title helper', @view.page_title
|
40
40
|
end
|
41
|
-
|
42
|
-
context
|
43
|
-
should
|
44
|
-
assert_equal
|
45
|
-
assert_equal
|
46
|
-
end
|
47
|
-
|
48
|
-
should
|
49
|
-
assert_equal
|
50
|
-
assert_equal
|
41
|
+
|
42
|
+
context 'used with the array block' do
|
43
|
+
should 'also allow aliases returned in that array thingy' do
|
44
|
+
assert_equal('Test', @view.page_title { ['Test', :myformat] })
|
45
|
+
assert_equal 'Test <-> Page title helper', @view.page_title
|
46
|
+
end
|
47
|
+
|
48
|
+
should 'override locally supplied :format arguments' do
|
49
|
+
assert_equal('Something', @view.page_title { ['Something', '* * * :title * * *'] })
|
50
|
+
assert_equal '* * * Something * * *', @view.page_title(format: '-= :title =-') # yeah, using x-tra ugly titles :)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
end
|
54
|
-
|
55
|
-
context
|
54
|
+
|
55
|
+
context '#page_title, aliases and YAML' do
|
56
56
|
setup do
|
57
|
-
I18n.load_path = [File.join(File.dirname(__FILE__),
|
57
|
+
I18n.load_path = [File.join(File.dirname(__FILE__), 'en_wohaapp.yml')]
|
58
58
|
I18n.reload!
|
59
|
-
PageTitleHelper.formats[:promo] =
|
60
|
-
@view =
|
59
|
+
PageTitleHelper.formats[:promo] = ':app > :title'
|
60
|
+
@view = TestView.new
|
61
61
|
end
|
62
|
-
|
63
|
-
should
|
64
|
-
@view.
|
62
|
+
|
63
|
+
should 'allow to overide format through YAML' do
|
64
|
+
@view.controller! 'pages', 'features'
|
65
65
|
assert_equal 'Wohaapp > Feature comparison', @view.page_title
|
66
66
|
end
|
67
|
-
|
68
|
-
should
|
69
|
-
@view.
|
67
|
+
|
68
|
+
should 'handle raw string formats from YAML as well' do
|
69
|
+
@view.controller! 'pages', 'signup'
|
70
70
|
assert_equal 'Sign up for Wohaapp now!', @view.page_title
|
71
71
|
end
|
72
72
|
end
|
73
|
-
end
|
73
|
+
end
|
@@ -1,117 +1,135 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
1
4
|
require 'test_helper'
|
2
|
-
require 'page_title_helper'
|
3
|
-
require 'ostruct'
|
4
5
|
|
5
6
|
class PageTitleHelperTest < ActiveSupport::TestCase
|
6
|
-
context
|
7
|
+
context 'PageTitleHelper' do
|
7
8
|
setup do
|
8
9
|
I18n.load_path = [File.join(File.dirname(__FILE__), 'en.yml')]
|
9
10
|
I18n.reload!
|
10
|
-
|
11
|
-
@view =
|
12
|
-
@view.template_path = "contacts/list.html.erb"
|
11
|
+
|
12
|
+
@view = TestView.new('contacts', 'list')
|
13
13
|
end
|
14
|
-
|
15
|
-
context
|
16
|
-
should
|
17
|
-
assert_equal 'Page title helper', PageTitleHelper::Interpolations.app(
|
18
|
-
assert_equal 'Appname', PageTitleHelper::Interpolations.app(
|
19
|
-
assert_equal 'untitled', PageTitleHelper::Interpolations.title(
|
14
|
+
|
15
|
+
context '::Interpolations' do
|
16
|
+
should 'interpolate :app and :title' do
|
17
|
+
assert_equal 'Page title helper', PageTitleHelper::Interpolations.app({})
|
18
|
+
assert_equal 'Appname', PageTitleHelper::Interpolations.app(app: 'Appname')
|
19
|
+
assert_equal 'untitled', PageTitleHelper::Interpolations.title(title: 'untitled')
|
20
20
|
end
|
21
21
|
|
22
|
-
should
|
22
|
+
should 'allow adding custom interpolations' do
|
23
23
|
# extend Interpolations
|
24
24
|
PageTitleHelper.interpolates(:app_reverse) { |env| app(env).reverse.downcase }
|
25
|
-
|
26
|
-
assert_equal
|
27
|
-
assert_equal
|
25
|
+
|
26
|
+
assert_equal 'anna', PageTitleHelper::Interpolations.app_reverse(app: 'Anna')
|
27
|
+
assert_equal 'ppa', PageTitleHelper::Interpolations.interpolate(':app_reverse', app: 'app')
|
28
28
|
end
|
29
29
|
|
30
|
-
should
|
31
|
-
PageTitleHelper.interpolates(:foobar) {
|
32
|
-
PageTitleHelper.interpolates(:foobar_test) {
|
33
|
-
PageTitleHelper.interpolates(:title_foobar) {
|
34
|
-
|
35
|
-
assert_equal
|
30
|
+
should 'interpolate in correct order, i.e. longest first' do
|
31
|
+
PageTitleHelper.interpolates(:foobar) { |_env| 'foobar' }
|
32
|
+
PageTitleHelper.interpolates(:foobar_test) { |_env| 'foobar_test' }
|
33
|
+
PageTitleHelper.interpolates(:title_foobar) { |_env| 'title_foobar' }
|
34
|
+
|
35
|
+
assert_equal 'title_foobar / foobar_test / foobar / foobar_x', PageTitleHelper::Interpolations.interpolate(':title_foobar / :foobar_test / :foobar / :foobar_x', {})
|
36
36
|
end
|
37
37
|
end
|
38
|
-
|
39
|
-
context
|
40
|
-
should
|
41
|
-
assert_equal
|
38
|
+
|
39
|
+
context '#page_title (define w/ block)' do
|
40
|
+
should 'return title from block and render with app name' do
|
41
|
+
assert_equal('foo', @view.page_title { 'foo' })
|
42
42
|
assert_equal 'foo - Page title helper', @view.page_title
|
43
43
|
end
|
44
44
|
|
45
|
-
should
|
46
|
-
assert_equal
|
47
|
-
assert_equal
|
48
|
-
end
|
45
|
+
should 'set custom title using a translation with a placeholder' do
|
46
|
+
assert_equal('Displaying Bella', @view.page_title { I18n.t(:placeholder, name: 'Bella') })
|
47
|
+
assert_equal 'Displaying Bella - Page title helper', @view.page_title
|
48
|
+
end
|
49
49
|
end
|
50
|
-
|
51
|
-
context
|
52
|
-
should
|
50
|
+
|
51
|
+
context '#page_title! (define)' do
|
52
|
+
should 'set page title' do
|
53
53
|
assert_equal 'test', @view.page_title!('test')
|
54
54
|
assert_equal 'test - Page title helper', @view.page_title
|
55
55
|
end
|
56
56
|
|
57
|
-
should
|
58
|
-
PageTitleHelper.formats[:bang] =
|
57
|
+
should 'set page title and interpret second argument as custom format' do
|
58
|
+
PageTitleHelper.formats[:bang] = ':title !! :app'
|
59
59
|
assert_equal 'test', @view.page_title!('test', :bang)
|
60
60
|
assert_equal 'test !! Page title helper', @view.page_title
|
61
|
-
end
|
61
|
+
end
|
62
62
|
end
|
63
|
-
|
64
|
-
context
|
65
|
-
should
|
63
|
+
|
64
|
+
context '#page_title (rendering)' do
|
65
|
+
should 'read default title from I18n, based on controller/action' do
|
66
66
|
assert_equal 'contacts.list.title - Page title helper', @view.page_title
|
67
67
|
end
|
68
|
-
|
69
|
-
should
|
70
|
-
assert_equal 'Page title helper', @view.page_title(:
|
68
|
+
|
69
|
+
should 'only print app name if format: :app' do
|
70
|
+
assert_equal 'Page title helper', @view.page_title(format: :app)
|
71
|
+
end
|
72
|
+
|
73
|
+
should 'print custom app name if :app defined and format: :app' do
|
74
|
+
assert_equal 'Some app', @view.page_title(app: 'Some app', format: :app)
|
75
|
+
end
|
76
|
+
|
77
|
+
should 'use custom format, if :format option is defined' do
|
78
|
+
assert_equal('test', @view.page_title { 'test' })
|
79
|
+
assert_equal 'Some app :: test', @view.page_title(app: 'Some app', format: ':app :: :title')
|
80
|
+
assert_equal 'Some app / test', @view.page_title(format: 'Some app / :title')
|
71
81
|
end
|
72
82
|
|
73
|
-
should
|
74
|
-
assert_equal
|
83
|
+
should 'return just title if format: false is passed' do
|
84
|
+
assert_equal('untitled', @view.page_title { 'untitled' })
|
85
|
+
assert_equal 'untitled', @view.page_title(format: false)
|
75
86
|
end
|
76
87
|
|
77
|
-
should
|
78
|
-
assert_equal '
|
79
|
-
assert_equal "Some app :: test", @view.page_title(:app => "Some app", :format => ':app :: :title')
|
80
|
-
assert_equal "Some app / test", @view.page_title(:format => 'Some app / :title')
|
88
|
+
should 'return title if format: false and when using the DRY-I18n titles' do
|
89
|
+
assert_equal 'contacts.list.title', @view.page_title(format: false)
|
81
90
|
end
|
82
91
|
|
83
|
-
should
|
84
|
-
|
85
|
-
assert_equal
|
92
|
+
should 'render translated :"app.tagline" if no title is available' do
|
93
|
+
@view.controller! 'view/does', 'not_exist'
|
94
|
+
assert_equal 'Default - Page title helper', @view.page_title
|
86
95
|
end
|
87
|
-
|
88
|
-
should
|
89
|
-
|
96
|
+
|
97
|
+
should 'render use controller.title as first fallback, if no title exists' do
|
98
|
+
@view.controller! 'admin/account', 'index'
|
99
|
+
assert_equal 'Account administration - Page title helper', @view.page_title(default: 'Other default')
|
90
100
|
end
|
91
|
-
|
92
|
-
should
|
93
|
-
@view.
|
94
|
-
assert_equal
|
101
|
+
|
102
|
+
should 'not fallback to controller.title if controller.action.title exists' do
|
103
|
+
@view.controller! 'admin/account', 'show'
|
104
|
+
assert_equal 'Account - Page title helper', @view.page_title(default: 'Other default')
|
95
105
|
end
|
96
106
|
|
97
|
-
should
|
98
|
-
@view.
|
99
|
-
assert_equal '
|
107
|
+
should 'fallback to controller.new.title if create has no title' do
|
108
|
+
@view.controller! 'admin/account', 'create'
|
109
|
+
assert_equal 'New account - Page title helper', @view.page_title(default: 'Other default')
|
100
110
|
end
|
101
111
|
|
102
|
-
should
|
103
|
-
@view.
|
104
|
-
assert_equal '
|
112
|
+
should 'fallback to controller.edit.title if update has no title' do
|
113
|
+
@view.controller! 'admin/account', 'update'
|
114
|
+
assert_equal 'Edit account - Page title helper', @view.page_title(default: 'Other default')
|
105
115
|
end
|
106
116
|
|
107
|
-
should
|
108
|
-
|
117
|
+
should 'render custom "default" string, if title is not available nor controller.title' do
|
118
|
+
@view.controller! 'view/does', 'not_exist'
|
119
|
+
assert_equal 'Some default - Page title helper', @view.page_title(default: 'Some default')
|
120
|
+
end
|
121
|
+
|
122
|
+
should 'render custom default translation, if title is not available nor controller.title' do
|
123
|
+
@view.controller! 'view/does', 'not_exist'
|
124
|
+
assert_equal 'Other default - Page title helper', @view.page_title(default: :'app.other_tagline')
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
context 'README.md' do
|
129
|
+
should 'interpolate :controller' do
|
130
|
+
PageTitleHelper.interpolates(:controller) { |env| env[:view].controller.controller_name.humanize }
|
131
|
+
assert_equal 'contacts.list.title - Test', @view.page_title(format: ':title - :controller')
|
109
132
|
end
|
110
|
-
|
111
|
-
should "work with other template engines, like HAML" do
|
112
|
-
@view.template_path = 'contacts/myhaml.html.haml'
|
113
|
-
assert_equal 'this is haml! - Page title helper', @view.page_title
|
114
|
-
end
|
115
133
|
end
|
116
134
|
end
|
117
135
|
end
|