title_helpers 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e5693bd6d2f7573f0e5a69a0f1697e87c730ab2b
4
+ data.tar.gz: bc66c6741e147e9057c4b456e11687bbe498552b
5
+ SHA512:
6
+ metadata.gz: 3c47c0652ee0a5ddfb542e5167f4a601032813f87efa5f29f5886f30ac8f3fb588d16a097b099ddb3beb93acc97d392f3949c2de5252ec3e06bc3c599add0a15
7
+ data.tar.gz: 231d45d31a8341a0805f051f0472071134261d23e9d8e56ced8c7c11badd9d56834200479a69d26e7badbb08647574fe2184ad7119aa9cdc25b5f4c05f2463f7
data/README.markdown ADDED
@@ -0,0 +1,144 @@
1
+ # Title Helpers
2
+
3
+ Rails plugin providing helpers to set and show the site `<title>`.
4
+
5
+
6
+ ## Showing the title
7
+
8
+ Show titles like
9
+
10
+ <title><%= title("My Site") %></title>
11
+
12
+ where "My Site" is your base title. If no custom title has been set for the current page, the returned title will just be that base title (except in development – see below).
13
+
14
+ You can provide the base title inline, like above. You could also, of course, provide it from a constant or a configuration object that you set up in an initializer, e.g.
15
+
16
+ <title><%= title(SITE_TITLE) %></title>
17
+
18
+ Your views and controllers can set title prefixes ("Welcome – My Site") as well as override the full title ("Welcome to My Site!").
19
+
20
+ You can use `title` without an argument to get just the title prefix. This is handy for headers:
21
+
22
+ <% self.title = "Welcome" %>
23
+
24
+ <title><%= title("My Site")</title>
25
+
26
+ <h1><%= title %></h1>
27
+
28
+ will give the title "Welcome – My Site" and the header "Welcome".
29
+
30
+ You could even do something like
31
+
32
+ <% self.title = "Welcome" %>
33
+ <% self.full_title = "Welcome to %s!" %>
34
+
35
+ <title><%= title("My Site")</title>
36
+
37
+ <h1><%= title %></h1>
38
+
39
+ to set the full title to "Welcome to My Site!" and the header to "Welcome".
40
+
41
+ The `title` helper applies `h` so you don't have to.
42
+
43
+
44
+ ## Setting the title
45
+
46
+ Define titles like
47
+
48
+ self.title = "Welcome"
49
+
50
+ in a controller or
51
+
52
+ <% self.title = "Welcome" %>
53
+
54
+ in a view. The above example would give "Welcome – My Site".
55
+
56
+ If you do not want the page suffix for a particular page, do e.g.
57
+
58
+ self.full_title = "Welcome"
59
+
60
+ which would give "Welcome". You can use "%s" to substitute the base title when
61
+ using `full_title`:
62
+
63
+ self.full_title = "Welcome to %s!"
64
+
65
+ gives "Welcome to My Site!"
66
+
67
+ Since the `full_title` is treated as a format string (with %s), remember
68
+ to escape any non-format string percentage signs by doubling them:
69
+
70
+ self.full_title = "Welcome to %s – 100%% French bulldogs"
71
+
72
+ gives "Welcome to My Site – 100% French bulldogs".
73
+
74
+ If your `full_title` contains dynamic data, you can use the `format_string_escape`
75
+ (or `fs_escape` for short) helper:
76
+
77
+ user.name = "%see%my%vest%"
78
+ self.full_title = "#{fs_escape user.name} on %s"
79
+
80
+ When using `title=`, data is automatically escaped, since format strings can only be
81
+ used with `full_title`:
82
+
83
+ user.name = "%see%my%vest%"
84
+ self.title = user.name
85
+
86
+ gives "%see%my%vest% – My Site".
87
+
88
+
89
+ ## Title hints in development
90
+
91
+ If no title has been set for a page, the base title will be shown. *In development*, however, you will see
92
+
93
+ ~ PLEASE SET A TITLE ~
94
+
95
+ This is intended to help you remember to title every page. If you really just wanted the
96
+ base title on a page and do not want to see this helpful hint, just do
97
+
98
+ self.title!
99
+
100
+
101
+ ## Tips
102
+
103
+ You can override the `title=` method in your controllers to e.g. get a common prefix:
104
+
105
+ class UsersController < ApplicationController
106
+
107
+ protected
108
+
109
+ def title=(title)
110
+ title = "User: #{title}"
111
+ super(title)
112
+ end
113
+
114
+ end
115
+
116
+ Now you can do
117
+
118
+ self.title = @user.name
119
+
120
+ from the controller or its views, to get titles like "User: User Name – My Site".
121
+
122
+ ## Credits and license
123
+
124
+ By [Henrik Nyh](http://henrik.nyh.se/) under the MIT license:
125
+
126
+ > Copyright (c) 2008 Henrik Nyh
127
+ >
128
+ > Permission is hereby granted, free of charge, to any person obtaining a copy
129
+ > of this software and associated documentation files (the "Software"), to deal
130
+ > in the Software without restriction, including without limitation the rights
131
+ > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
132
+ > copies of the Software, and to permit persons to whom the Software is
133
+ > furnished to do so, subject to the following conditions:
134
+ >
135
+ > The above copyright notice and this permission notice shall be included in
136
+ > all copies or substantial portions of the Software.
137
+ >
138
+ > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
139
+ > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
140
+ > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
141
+ > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
142
+ > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
143
+ > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
144
+ > THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = "test/*_test.rb"
7
+ end
8
+
9
+ task :default => :test
data/TODO.tasks ADDED
@@ -0,0 +1,3 @@
1
+ - Customize delimiter?
2
+ - Multi-level titles?
3
+ - Perhaps _self.title = "Full title"_ for full and _self.title << "Prefix"_ for prefix? Ties in with multi-level.
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ ActionController::Base.send(:include, TitleHelpers)
@@ -0,0 +1,3 @@
1
+ module TitleHelpers
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+
3
+ require "erb"
4
+ require "rubygems"
5
+ require "active_support"
6
+
7
+ module TitleHelpers
8
+ HINT = "~ PLEASE SET A TITLE ~"
9
+
10
+ def self.included(klass)
11
+ klass.helper_method :title, :title=,
12
+ :full_title=, :title!,
13
+ :format_string_escape, :fs_escape
14
+ end
15
+
16
+ private
17
+
18
+ def title(site_title = nil)
19
+ if Rails.env.development? && !@title && !@full_title
20
+ @full_title = HINT
21
+ end
22
+
23
+ unless site_title
24
+ title = fs_escape(@title || @full_title)
25
+ else
26
+ title = @full_title || (@title && "#{fs_escape @title} – %s") || "%s"
27
+ end
28
+
29
+ ERB::Util.h(title % site_title)
30
+ end
31
+
32
+ # If the helpers are used in a view, then the code runs _after_ instance variables have
33
+ # been copied from the controller to the template. Without manually copying them over like
34
+ # this, they would not be available to layouts.
35
+
36
+ def title=(title)
37
+ @title = @template.instance_variable_set("@title", title)
38
+ end
39
+
40
+ def full_title=(title)
41
+ @full_title = @template.instance_variable_set("@full_title", title)
42
+ end
43
+
44
+ def title!
45
+ self.full_title=("%s")
46
+ end
47
+
48
+ def format_string_escape(string)
49
+ string.to_s.gsub('%', '%%')
50
+ end
51
+ alias_method :fs_escape, :format_string_escape
52
+ end
Binary file
@@ -0,0 +1,164 @@
1
+ # encoding: utf-8
2
+
3
+ require 'test/unit'
4
+ require File.join(File.dirname(__FILE__), '../lib/title_helpers')
5
+
6
+ class Rails
7
+ class << self
8
+ attr_accessor :is_dev
9
+ end
10
+
11
+ def self.env
12
+ env = Object.new
13
+ def env.development?() Rails.is_dev end
14
+ env
15
+ end
16
+ end
17
+
18
+ class FakeController
19
+ def self.helper_method(*args); end
20
+
21
+ def initialize
22
+ @template = Object.new
23
+ end
24
+
25
+ include TitleHelpers
26
+ end
27
+
28
+
29
+ class TitleHelpersTest < Test::Unit::TestCase
30
+
31
+ def setup
32
+ @controller = FakeController.new
33
+ end
34
+
35
+ def assert_equal_in_controller(expected, &block)
36
+ assert_equal expected, @controller.instance_eval(&block)
37
+ end
38
+
39
+
40
+ def test_format_string_escape
41
+ assert_equal_in_controller("x") { format_string_escape("x") }
42
+ assert_equal_in_controller("%%s") { format_string_escape("%s") }
43
+ end
44
+
45
+
46
+ def test_title_with_suffix
47
+ assert_equal_in_controller("Welcome – My Site") do
48
+ self.title = "Welcome"
49
+ title("My Site")
50
+ end
51
+ end
52
+
53
+ def test_title_with_no_suffix
54
+ assert_equal_in_controller("Welcome") do
55
+ self.title = "Welcome"
56
+ title
57
+ end
58
+ end
59
+
60
+ def test_title_with_format_string_without_suffix
61
+ assert_equal_in_controller("%s") do
62
+ self.title = "%s"
63
+ title
64
+ end
65
+ end
66
+
67
+ def test_title_with_format_string_with_suffix
68
+ assert_equal_in_controller("%s – My Site") do
69
+ self.title = "%s"
70
+ title("My Site")
71
+ end
72
+ end
73
+
74
+ def test_title_escapes_html
75
+ assert_equal_in_controller("&lt;b&gt;ad") do
76
+ self.title = "<b>ad"
77
+ title
78
+ end
79
+ end
80
+
81
+
82
+
83
+ def test_full_title_without_format_string
84
+ assert_equal_in_controller("Welcome!") do
85
+ self.full_title = "Welcome!"
86
+ title("My Site")
87
+ end
88
+ end
89
+
90
+ def test_full_title_with_format_string
91
+ assert_equal_in_controller("Welcome to My Site!") do
92
+ self.full_title = "Welcome to %s!"
93
+ title("My Site")
94
+ end
95
+ end
96
+
97
+ def test_full_title_without_suffix_without_format_string
98
+ assert_equal_in_controller("Welcome") do
99
+ self.full_title = "Welcome"
100
+ title
101
+ end
102
+ end
103
+
104
+ def test_full_title_without_suffix_with_format_string
105
+ assert_equal_in_controller("Welcome to %s") do
106
+ self.full_title = "Welcome to %s"
107
+ title
108
+ end
109
+ end
110
+
111
+ def test_full_title_with_percentage_sign_and_suffix
112
+ assert_raises(ArgumentError) do
113
+ assert_equal_in_controller("100% French bulldogs") do
114
+ self.full_title = "100% French bulldogs"
115
+ title("My Site")
116
+ end
117
+ end
118
+ assert_nothing_raised do
119
+ assert_equal_in_controller("100% French bulldogs") do
120
+ self.full_title = "100%% French bulldogs"
121
+ title("My Site")
122
+ end
123
+ end
124
+ end
125
+
126
+ def test_full_title_with_percentage_sign_and_no_suffix
127
+ assert_equal_in_controller("100% French bulldogs") do
128
+ self.full_title = "100% French bulldogs"
129
+ title
130
+ end
131
+ end
132
+
133
+ def test_full_title_escapes_html
134
+ assert_equal_in_controller("&lt;b&gt;ad") do
135
+ self.full_title = "<b>ad"
136
+ title("My Site")
137
+ end
138
+ end
139
+
140
+
141
+ def test_combining_title_and_full_title
142
+ assert_equal_in_controller(["Welcome to My Site!", "Welcome"]) do
143
+ self.title = "Welcome"
144
+ self.full_title = "Welcome to %s!"
145
+ [title("My Site"), title]
146
+ end
147
+ end
148
+
149
+
150
+ def test_no_title_set_in_development
151
+ Rails.is_dev = true
152
+ assert_equal_in_controller([TitleHelpers::HINT, TitleHelpers::HINT]) do
153
+ [title("My Site"), title]
154
+ end
155
+ end
156
+
157
+ def test_no_title_set_in_non_development
158
+ Rails.is_dev = false
159
+ assert_equal_in_controller(["My Site", ""]) do
160
+ [title("My Site"), title]
161
+ end
162
+ end
163
+
164
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/title_helpers/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Henrik Nyh"]
6
+ gem.email = ["henrik@nyh.se"]
7
+ gem.summary = %q{Rails title helpers.}
8
+ gem.homepage = "https://github.com/henrik/title_helpers"
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.name = "title_helpers"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = TitleHelpers::VERSION
16
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: title_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Henrik Nyh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - henrik@nyh.se
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.markdown
21
+ - Rakefile
22
+ - TODO.tasks
23
+ - init.rb
24
+ - lib/title_helpers.rb
25
+ - lib/title_helpers/version.rb
26
+ - pkg/title_helpers-0.0.1.gem
27
+ - test/title_helpers_test.rb
28
+ - title_helpers.gemspec
29
+ homepage: https://github.com/henrik/title_helpers
30
+ licenses: []
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.2.2
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Rails title helpers.
52
+ test_files:
53
+ - test/title_helpers_test.rb