happy-titles 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/.git_ignore ADDED
@@ -0,0 +1 @@
1
+ .DS_Store
data/MIT-LICENCE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Andy Pearson
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.markdown ADDED
@@ -0,0 +1,139 @@
1
+ # Happy Titles!
2
+
3
+ A simple Rails plugin which adds an easy (and cheerful) way to handle page titles in your layouts.
4
+
5
+ ## Getting Started
6
+
7
+ ### Install the Plugin
8
+
9
+ script/plugin install git://github.com/andypearson/happy-titles.git
10
+
11
+ ### Set the Defaults
12
+
13
+ Create a new file in `config/initializers` called `happy_titles.rb` or something else which makes sense!
14
+
15
+ In this new file, add the following lines to set the default Site and Tagline.
16
+
17
+ ActionView::Base.happy_title_setting(:site, 'Your Site')
18
+ ActionView::Base.happy_title_setting(:tagline, 'Your witty but informative tagline')
19
+
20
+ ### Update your Layout
21
+
22
+ In the layout where you want to display your title add the following call to the Happy Titles helper method.
23
+
24
+ <%= happy_title %>
25
+
26
+ The header element of your layout might end up looking something like:
27
+
28
+ <head>
29
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
30
+ <%= happy_title %>
31
+ </head>
32
+
33
+ Notice you don't need to wrap the helper in a `<title>` element, this is done for you.
34
+
35
+ ### Set the Title in your Views
36
+
37
+ In each of your views call the `title` method to set the title for that page.
38
+
39
+ <% title 'Your very first Happy Title!' %>
40
+
41
+ ### See your Titles!
42
+
43
+ Given the above settings, let's have a look at the output you will receive when you call the `happy_title` method in your layouts.
44
+
45
+ On pages where the title has not been set you will see...
46
+
47
+ <title>Your Site | Your witty but informative tagline</title>
48
+
49
+ ...and on pages where the title _has_ been set you will see...
50
+
51
+ <title>Your very first Happy Title! | Your Site</title>
52
+
53
+ That really is all there is to setting up and using Happy Titles!
54
+
55
+
56
+ ## Customising the Title Templates
57
+
58
+ ### Intro
59
+
60
+ You have already seen how to set up and use Happy Titles, now let's take a look at how to customise the individual title templates.
61
+
62
+ In all of the following examples I am going to assume the same defaults that are set up in the Set the Defaults section of this Readme.
63
+
64
+ Before we have a look at that, let's have a look at the **placeholders** you can use. These placeholders will be replaced with the real content when the title is rendered.
65
+
66
+ :site -> 'Your Site'
67
+ :tagline -> 'Your witty but informative tagline'
68
+ :title -> Will become 'Your very first Happy Title!' or :tagline if a page title has not been set
69
+
70
+ Now let's have a look at the default templates.
71
+
72
+ ':site | :title' -> Is used when there is no title set: 'Your Site | Your witty but informative tagline'
73
+ ':title | :site' -> Is used when there IS a title set: 'Your very first Happy Title! | Your Site'
74
+
75
+ You can change the default templates by using the following setting in `config/initializers/happy_titles.rb`
76
+
77
+ # The first param sets the name of the template and takes a symbol
78
+ # The second param is the template to use when there is no title set
79
+ # The third param is the template to use when there is a title set
80
+ ActionView::Base.happy_title_template(:default, '[:site]', '[:site] :title')
81
+
82
+ Then when you render your titles you will see the following
83
+
84
+ <title>[Your Site]</title> <!-- when there is no title -->
85
+ <title>[Your Site] Your very first Happy Title!</title> <!-- when the title is set -->
86
+
87
+ ### Adding Additional Templates
88
+
89
+ As well as changing the default template, you can also add new templates and then use those when you need a different title format in a different layout.
90
+
91
+ # config/initializers/happy_titles.rb
92
+ ActionView::Base.happy_title_template(:extra, '++ :site ++', '++ :site ++ :title ++')
93
+
94
+ Then in your layout, you can use the following to call the extra template.
95
+
96
+ <%= happy_title :extra %>
97
+
98
+ And you will get the following output, as you probably would of guessed by now!
99
+
100
+ <title>++ Your Site ++</title> <!-- when there is no title -->
101
+ <title>++ Your Site ++ Your very first Happy Title! ++</title> <!-- when the title is set -->
102
+
103
+ You can add as many additional title templates as you need!
104
+
105
+ One final thing to mention, you can create templates that just have one format. So...
106
+
107
+ # config/initializers/happy_titles.rb
108
+ ActionView::Base.happy_title_template(:single, ':site (:title)')
109
+
110
+ # In your layout
111
+ <%= happy_title :single %>
112
+
113
+ # Output
114
+ <title>Your Site (Your witty but informative tagline)</title> <!-- when there is no title -->
115
+ <title>Your Site (Your very first Happy Title!)</title> <!-- when the title is set -->
116
+
117
+
118
+ ## Other Things...
119
+
120
+ ### Why Use Happy Titles?
121
+
122
+ Happy Titles is a simple way to use multiple title formats in the same app. In my case, the app I was working on at the time required different title formats for public and private pages, and this is the best way I could think of managing that requirement in a suitably DRY fashion.
123
+
124
+ ### Why on earth is it called Happy Titles?
125
+
126
+ What can I say, I was in a good mood when I started writing the plugin and I couldn't think of anything better to call it!
127
+
128
+ ### Inspiration and Thanks
129
+
130
+ When it came to actually writing the plugin I studied the [Headliner plugin][hl] to see how that was done and got some ideas on how to set up my specs. I also want to thank [Justin French][jf] for his help when I created some small patches for his [Formtastic][ft] plugin. He made me realise that even though I don't have a lot of experience I can still help out the open source community. Please take a look at [Formtastic][ft] for a great way to make semantic forms really easy in Rails. Thanks also to all the clever chaps working on GitHub, I don't think I would of ever had the oomph to release this if GitHub wasn't here to make it so wonderfully easy and fun!
131
+
132
+ ### Please give me some feedback
133
+
134
+ This plugin was written as an experiment and a learning experience. I am a relative newcomer to the world of Ruby, Rails and Github so if you have 5 Minutes to skim read the code and maybe suggest something I could do better in the future then please leave a comment and I will be very grateful of the feedback, thanks in advance!
135
+
136
+
137
+ [hl]: http://github.com/mokolabs/headliner/tree/master "mokolabs Headliner on GitHub"
138
+ [jf]: http://justinfrench.com/ "Justin French"
139
+ [ft]: http://github.com/justinfrench/formtastic/tree/master "justinfrench Formtastic on GitHub"
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'rake'
2
+ require 'spec/rake/spectask'
3
+
4
+ desc 'Test the happy-titles plugin.'
5
+ Spec::Rake::SpecTask.new('spec') do |t|
6
+ t.spec_files = FileList['spec/**/*_spec.rb']
7
+ t.spec_opts = ["-c"]
8
+ end
9
+
10
+ begin
11
+ require 'jeweler'
12
+ Jeweler::Tasks.new do |gemspec|
13
+ gemspec.name = "happy-titles"
14
+ gemspec.summary = "A simple way to handle page titles in your layouts."
15
+ gemspec.email = "andy@andy-pearson.com"
16
+ gemspec.homepage = "http://github.com/andypearson/happy-titles"
17
+ gemspec.authors = ["Andy Pearson"]
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler not available. Install it with: gem install jeweler"
21
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.9.0
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require File.dirname(__FILE__) + '/lib/happy_titles'
2
+ ActionView::Base.send(:include, HappyTitles)
@@ -0,0 +1,61 @@
1
+ module HappyTitles
2
+
3
+ attr_accessor :page_title
4
+
5
+ @@happy_title_settings = {
6
+ :site => 'My Site',
7
+ :tagline => 'My short, descriptive and witty tagline',
8
+ :templates => {
9
+ :default => [':site | :title', ':title | :site']
10
+ }
11
+ }
12
+
13
+ def happy_title(template_key = :default)
14
+ content_tag(:title, render_title_template(template_key))
15
+ end
16
+
17
+ def title(page_title = nil)
18
+ if page_title
19
+ @page_title = h(page_title.gsub(/<\/?[^>]*>/, '')) if page_title
20
+ else
21
+ @page_title || ''
22
+ end
23
+ end
24
+
25
+ def self.included(base)
26
+ base.class_eval do
27
+
28
+ cattr_accessor :happy_title_settings
29
+
30
+ def self.happy_title_template(template_key, format, additional_format = nil)
31
+ template = [format]
32
+ template << additional_format if additional_format
33
+ @@happy_title_settings[:templates][template_key] = template
34
+ end
35
+
36
+ def self.happy_title_setting(key, value)
37
+ @@happy_title_settings[key] = value
38
+ end
39
+
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def render_title_template(template_key)
46
+ key = (@page_title && @@happy_title_settings[:templates][template_key][1]) ? 1 : 0
47
+ substitute_placeholders(@@happy_title_settings[:templates][template_key][key])
48
+ end
49
+
50
+ def substitute_placeholders(template)
51
+ title = template
52
+ title = title.gsub(':title', title_text)
53
+ title = title.gsub(':site', @@happy_title_settings[:site])
54
+ title = title.gsub(':tagline', @@happy_title_settings[:tagline])
55
+ end
56
+
57
+ def title_text
58
+ (@page_title) ? @page_title : @@happy_title_settings[:tagline]
59
+ end
60
+
61
+ end
@@ -0,0 +1,274 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe 'Happy Titles!' do
4
+
5
+ before do
6
+ @view = ActionView::Base.new
7
+ end
8
+
9
+ describe 'after loading the plugin' do
10
+
11
+ it "should be mixed into ActionView::Base" do
12
+ ActionView::Base.included_modules.include?(HappyTitles).should be_true
13
+ end
14
+
15
+ it 'should respond to happy_title_settings class variable' do
16
+ ActionView::Base.happy_title_settings.should be_a(Hash)
17
+ end
18
+
19
+ it 'should respond to happy_title_template class method' do
20
+ ActionView::Base.should respond_to(:happy_title_template)
21
+ end
22
+
23
+ it 'should respond to happy_title_setting class method' do
24
+ ActionView::Base.should respond_to(:happy_title_setting)
25
+ end
26
+
27
+ it 'should respond to happy_title helper' do
28
+ @view.should respond_to(:happy_title)
29
+ end
30
+
31
+ it 'should respond to title helper' do
32
+ @view.should respond_to(:title)
33
+ end
34
+
35
+ end
36
+
37
+ describe 'default settings' do
38
+
39
+ it 'should have a default site setting' do
40
+ ActionView::Base.happy_title_settings[:site].should == 'My Site'
41
+ end
42
+ it 'should have a default tagline setting' do
43
+ ActionView::Base.happy_title_settings[:tagline].should == 'My short, descriptive and witty tagline'
44
+ end
45
+
46
+ describe 'templates' do
47
+
48
+ it 'should have a list of templates' do
49
+ ActionView::Base.happy_title_settings[:templates].should be_a(Hash)
50
+ end
51
+
52
+ it 'should have a default template' do
53
+ ActionView::Base.happy_title_settings[:templates][:default].should be_an(Array)
54
+ end
55
+
56
+ it 'should have a default template for when the page title is not set' do
57
+ ActionView::Base.happy_title_settings[:templates][:default][0].should == ':site | :title'
58
+ end
59
+
60
+ it 'should have a default template for when the page title is set' do
61
+ ActionView::Base.happy_title_settings[:templates][:default][1].should == ':title | :site'
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+
68
+ describe 'custom settings' do
69
+
70
+ before do
71
+ @default_site = ActionView::Base.happy_title_settings[:site].dup
72
+ @default_tagline = ActionView::Base.happy_title_settings[:tagline].dup
73
+ end
74
+
75
+ after do
76
+ ActionView::Base.happy_title_setting(:site, @default_site)
77
+ ActionView::Base.happy_title_setting(:tagline, @default_tagline)
78
+ end
79
+
80
+ it 'should be able to change the default site' do
81
+ ActionView::Base.happy_title_setting(:site, 'My Custom Site')
82
+ ActionView::Base.happy_title_settings[:site].should == 'My Custom Site'
83
+ end
84
+
85
+ it 'should be able to change the default tagline' do
86
+ ActionView::Base.happy_title_setting(:tagline, 'My very custom tagline')
87
+ ActionView::Base.happy_title_settings[:tagline].should == 'My very custom tagline'
88
+ end
89
+
90
+ describe 'templates' do
91
+
92
+ before do
93
+ @default_templates = ActionView::Base.happy_title_settings[:templates].dup
94
+ end
95
+
96
+ after do
97
+ ActionView::Base.happy_title_settings[:templates] = @default_templates
98
+ end
99
+
100
+ it 'should be able to change the default templates' do
101
+ ActionView::Base.happy_title_template(:default, '[:site] :title', ':title at :site')
102
+ ActionView::Base.happy_title_settings[:templates][:default].should == ['[:site] :title', ':title at :site']
103
+ end
104
+
105
+ it 'should be able to add a new template' do
106
+ ActionView::Base.happy_title_template(:new_template, '[:site] :title', ':title at :site')
107
+ ActionView::Base.happy_title_settings[:templates][:new_template].should == ['[:site] :title', ':title at :site']
108
+ end
109
+
110
+ it 'should be able to add a new template with one format' do
111
+ ActionView::Base.happy_title_template(:new_template, '[:site] :title')
112
+ ActionView::Base.happy_title_settings[:templates][:new_template].should == ['[:site] :title']
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+
119
+ describe 'setting the title' do
120
+
121
+ it 'should set the title' do
122
+ @view.title("Happy Title!")
123
+ @view.send(:page_title).should == "Happy Title!"
124
+ end
125
+
126
+ it 'should overwrite a privously set title' do
127
+ @view.title('First Page Title')
128
+ @view.title('Second Page Title')
129
+ @view.page_title.should eql("Second Page Title")
130
+ end
131
+
132
+ it 'should strip HTML elements from the title' do
133
+ @view.title('<strong>Cat is</strong>!')
134
+ @view.page_title.should eql('Cat is!')
135
+ end
136
+
137
+ it 'should escape special entities in the title element' do
138
+ @view.title('This & That')
139
+ @view.page_title.should eql('This &amp; That')
140
+ end
141
+
142
+ it 'should handle a mix of HTML and special entities' do
143
+ @view.title('<strong>This & That</strong>')
144
+ @view.page_title.should eql('This &amp; That')
145
+ end
146
+
147
+ end
148
+
149
+ describe 'reading the title method' do
150
+
151
+ it 'should return an empty string when called with no args' do
152
+ @view.title.should == ''
153
+ end
154
+
155
+ it 'should return @page_title when @page_title is set and is called with no args' do
156
+ @view.title('Happy Title!')
157
+ @view.title.should == 'Happy Title!'
158
+ end
159
+
160
+ it 'should not set the title when called with no args' do
161
+ @view.title
162
+ @view.happy_title.should have_tag('title', 'My Site | My short, descriptive and witty tagline')
163
+ end
164
+
165
+ end
166
+
167
+ describe 'showing the title' do
168
+
169
+ it 'should output a valid title element' do
170
+ @view.happy_title.should have_tag('title')
171
+ end
172
+
173
+ describe 'with default settings' do
174
+
175
+ it 'should use the default template when the page title is not set' do
176
+ @view.happy_title.should have_tag('title', 'My Site | My short, descriptive and witty tagline')
177
+ end
178
+
179
+ it 'should use the default template when the page title is set' do
180
+ @view.title('Example Page Title')
181
+ @view.happy_title.should have_tag('title', 'Example Page Title | My Site')
182
+ end
183
+
184
+ end
185
+
186
+ describe 'with custom settings' do
187
+
188
+ before do
189
+ @default_site = ActionView::Base.happy_title_settings[:site].dup
190
+ @default_tagline = ActionView::Base.happy_title_settings[:tagline].dup
191
+ end
192
+
193
+ after do
194
+ ActionView::Base.happy_title_setting(:site, @default_site)
195
+ ActionView::Base.happy_title_setting(:tagline, @default_tagline)
196
+ end
197
+
198
+ it 'should render the title with a custom site' do
199
+ ActionView::Base.happy_title_setting(:site, 'Custom Site')
200
+ @view.happy_title.should have_tag('title', 'Custom Site | My short, descriptive and witty tagline')
201
+ end
202
+
203
+ it 'should render the title with a custom tagline' do
204
+ ActionView::Base.happy_title_setting(:tagline, 'My custom tagline...')
205
+ @view.happy_title.should have_tag('title', 'My Site | My custom tagline...')
206
+ end
207
+
208
+ describe '(templates)' do
209
+
210
+ before do
211
+ @default_templates = ActionView::Base.happy_title_settings[:templates].dup
212
+ end
213
+
214
+ after do
215
+ ActionView::Base.happy_title_settings[:templates] = @default_templates
216
+ end
217
+
218
+ describe 'template with a single format' do
219
+
220
+ before do
221
+ ActionView::Base.happy_title_template(:default, '[:site] :title')
222
+ end
223
+
224
+ it 'should render the template when the title is not set' do
225
+ @view.happy_title.should have_tag('title', '[My Site] My short, descriptive and witty tagline')
226
+ end
227
+
228
+ it 'should render the template when the title is set' do
229
+ @view.title('Another example title')
230
+ @view.happy_title.should have_tag('title', '[My Site] Another example title')
231
+ end
232
+
233
+ end
234
+
235
+ describe 'template with two formats' do
236
+
237
+ before do
238
+ ActionView::Base.happy_title_template(:default, '[:site] :title', ':title at :site')
239
+ end
240
+
241
+ it 'should render the template when the title is not set' do
242
+ @view.happy_title.should have_tag('title', '[My Site] My short, descriptive and witty tagline')
243
+ end
244
+
245
+ it 'should render the alternate template when the title is set' do
246
+ @view.title('Would of thought these titles would of got a bit funnier by now')
247
+ @view.happy_title.should have_tag('title', 'Would of thought these titles would of got a bit funnier by now at My Site')
248
+ end
249
+ end
250
+
251
+ describe 'using a custom template' do
252
+
253
+ before do
254
+ ActionView::Base.happy_title_template(:custom, '++:site++', ':title ++:site++')
255
+ end
256
+
257
+ it 'should render a custom template when the title is not set' do
258
+ @view.happy_title(:custom).should have_tag('title', '++My Site++')
259
+ end
260
+
261
+ it 'should render a custom template when the title is not set' do
262
+ @view.title('Fraid not, I got nothing')
263
+ @view.happy_title(:custom).should have_tag('title', 'Fraid not, I got nothing ++My Site++')
264
+ end
265
+
266
+ end
267
+
268
+ end
269
+
270
+ end
271
+
272
+ end
273
+
274
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ require 'rubygems'
3
+
4
+ gem 'activesupport', '2.3.8'
5
+ gem 'actionpack', '2.3.8'
6
+ require 'active_support'
7
+ require 'action_pack'
8
+ require 'action_view'
9
+ require 'action_controller'
10
+
11
+ gem 'rspec', '>= 1.2.6'
12
+ gem 'rspec-rails', '>= 1.2.6'
13
+ gem 'hpricot', '>= 0.6.1'
14
+ gem 'rspec_tag_matchers', '>= 1.0.0'
15
+ require 'rspec_tag_matchers'
16
+
17
+ require File.join(File.dirname(__FILE__), "../lib/happy_titles")
18
+
19
+ Spec::Runner.configure do |config|
20
+ config.include(RspecTagMatchers)
21
+ end
22
+
23
+ ActionView::Base.class_eval do
24
+ include HappyTitles
25
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: happy-titles
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 9
8
+ - 0
9
+ version: 0.9.0
10
+ platform: ruby
11
+ authors:
12
+ - Andy Pearson
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-01 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: andy@andy-pearson.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.markdown
29
+ files:
30
+ - .git_ignore
31
+ - MIT-LICENCE
32
+ - README.markdown
33
+ - Rakefile
34
+ - VERSION
35
+ - init.rb
36
+ - lib/happy_titles.rb
37
+ - spec/happy_titles_spec.rb
38
+ - spec/spec_helper.rb
39
+ has_rdoc: true
40
+ homepage: http://github.com/andypearson/happy-titles
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.6
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: A simple way to handle page titles in your layouts.
69
+ test_files:
70
+ - spec/happy_titles_spec.rb
71
+ - spec/spec_helper.rb