headliner 0.1.1
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.
- data/LICENSE +19 -0
- data/README.markdown +119 -0
- data/Rakefile +38 -0
- data/headliner.gemspec +43 -0
- data/init.rb +2 -0
- data/lib/headliner.rb +71 -0
- data/spec/headliner_spec.rb +105 -0
- data/spec/spec_helper.rb +8 -0
- metadata +73 -0
data/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2007 Patrick Crowley, the.railsi.st
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
data/README.markdown
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
Headliner
|
|
2
|
+
=========
|
|
3
|
+
|
|
4
|
+
Headliner is a Rails plugin that makes it easier to assign and format page titles from your views.
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Background
|
|
8
|
+
==========
|
|
9
|
+
|
|
10
|
+
Normally, if your Rails application has lots of actions and a shared layout, you might find yourself setting custom page title names in your controllers.
|
|
11
|
+
|
|
12
|
+
Here's an example:
|
|
13
|
+
|
|
14
|
+
class PagesController < ApplicationController
|
|
15
|
+
def about
|
|
16
|
+
@title = "About us"
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
Then, in your main layout, you might have something like this:
|
|
21
|
+
|
|
22
|
+
<head>
|
|
23
|
+
<title>My website<% if @title %>: <%= @title %><% end %></title>
|
|
24
|
+
</head
|
|
25
|
+
|
|
26
|
+
This works okay... but page titles don't really belong in controllers, do they?
|
|
27
|
+
|
|
28
|
+
So, by moving these titles into your views, we can DRY things up a bit and reinforce the MVC design pattern that's so fundamental to Ruby on Rails.
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
Install
|
|
32
|
+
=======
|
|
33
|
+
|
|
34
|
+
To install, just add Headliner to your `vendor/plugins` directory:
|
|
35
|
+
|
|
36
|
+
script/plugin install git://github.com/mokolabs/headliner.git
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
Usage
|
|
40
|
+
=====
|
|
41
|
+
|
|
42
|
+
First, add this code to your main layout:
|
|
43
|
+
|
|
44
|
+
<head>
|
|
45
|
+
<%= title :site => "My website" %>
|
|
46
|
+
</head>
|
|
47
|
+
|
|
48
|
+
Then, to set the page title, add this to each of your views:
|
|
49
|
+
|
|
50
|
+
<h1><%= title "My page title" %></h1>
|
|
51
|
+
|
|
52
|
+
When views are rendered, the page title will be included in the right
|
|
53
|
+
spots:
|
|
54
|
+
|
|
55
|
+
<head>
|
|
56
|
+
<title>My website | My page title</title>
|
|
57
|
+
</head>
|
|
58
|
+
<body>
|
|
59
|
+
<h1>My page title</h1>
|
|
60
|
+
</body>
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
Options
|
|
64
|
+
=======
|
|
65
|
+
|
|
66
|
+
Use these options to customize the title format:
|
|
67
|
+
|
|
68
|
+
:prefix (text between site name and separator)
|
|
69
|
+
:separator (text used to separate website name from page title)
|
|
70
|
+
:suffix (text between separator and page title)
|
|
71
|
+
:lowercase (when true, the page name will be lowercase)
|
|
72
|
+
:reverse (when true, the page and site names will be reversed)
|
|
73
|
+
:default (default title to use when title is blank)
|
|
74
|
+
|
|
75
|
+
And here are a few examples to give you ideas.
|
|
76
|
+
|
|
77
|
+
<%= title :separator => "—" %>
|
|
78
|
+
<%= title :prefix => false, :separator => ":" %>
|
|
79
|
+
<%= title :lowercase => true %>
|
|
80
|
+
<%= title :reverse => true, :prefix => false %>
|
|
81
|
+
<%= title :default => "The ultimate site for Rails" %>
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
Dealing with special pages
|
|
85
|
+
==========================
|
|
86
|
+
|
|
87
|
+
How do you set the page title without showing it in the view?
|
|
88
|
+
|
|
89
|
+
<% title "My page title" %>
|
|
90
|
+
|
|
91
|
+
What if your view headline is different from your page title?
|
|
92
|
+
|
|
93
|
+
<%= title "My page title", "My headline" %>
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
Mr. T says, ‘Use my method, fool!’
|
|
97
|
+
==================================
|
|
98
|
+
|
|
99
|
+
Just like ERB's HTML safe method, you can invoke Headliner with a single letter alias.
|
|
100
|
+
|
|
101
|
+
<h1><%= pt "My page title" %></h1>
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
How does it work?
|
|
105
|
+
=================
|
|
106
|
+
|
|
107
|
+
Ruby on Rails renders actions *before* inserting them into layouts. So, if you set a variable in your view, it will be accessible in your layout. But, at first glance, it looks like you're using a variable (in the head) before it's been assigned a value (in the body). Cool, huh?
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
Contributors
|
|
111
|
+
============
|
|
112
|
+
|
|
113
|
+
Special thanks to James Chan, Nick Zadrozny, and Jordan Fowler.
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
Feedback
|
|
117
|
+
========
|
|
118
|
+
|
|
119
|
+
Comments and patches welcome at [http://github.com/mokolabs/headliner/](http://github.com/mokolabs/headliner/).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require 'rake'
|
|
2
|
+
require 'rdoc/task'
|
|
3
|
+
require File.join(File.dirname(__FILE__), 'lib', 'headliner')
|
|
4
|
+
|
|
5
|
+
require 'rspec/core'
|
|
6
|
+
require 'rspec/core/rake_task'
|
|
7
|
+
|
|
8
|
+
desc 'Default: run unit tests.'
|
|
9
|
+
task :default => :spec
|
|
10
|
+
|
|
11
|
+
desc 'Test plugin'
|
|
12
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
|
13
|
+
spec.pattern = FileList['spec/*_spec.rb']
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
18
|
+
rdoc.title = 'Headliner'
|
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
|
20
|
+
rdoc.rdoc_files.include('README')
|
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
begin
|
|
25
|
+
require 'jeweler'
|
|
26
|
+
Jeweler::Tasks.new do |gemspec|
|
|
27
|
+
gemspec.name = "headliner"
|
|
28
|
+
gemspec.version = Headliner::VERSION
|
|
29
|
+
gemspec.summary = "Assign and format page titles from your views."
|
|
30
|
+
gemspec.description = "Assign and format page titles from your views"
|
|
31
|
+
gemspec.email = "shura.71@gmail.com"
|
|
32
|
+
gemspec.homepage = "http://github.com/shura71/headliner"
|
|
33
|
+
gemspec.authors = ["Alexander Shcherban"]
|
|
34
|
+
end
|
|
35
|
+
Jeweler::GemcutterTasks.new
|
|
36
|
+
rescue LoadError
|
|
37
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler -s http://gemcutter.org"
|
|
38
|
+
end
|
data/headliner.gemspec
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Generated by jeweler
|
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
|
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 = "headliner"
|
|
8
|
+
s.version = "0.1.1"
|
|
9
|
+
|
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
|
+
s.authors = ["Alexander Shcherban"]
|
|
12
|
+
s.date = "2012-01-24"
|
|
13
|
+
s.description = "Assign and format page titles from your views"
|
|
14
|
+
s.email = "shura.71@gmail.com"
|
|
15
|
+
s.extra_rdoc_files = [
|
|
16
|
+
"LICENSE",
|
|
17
|
+
"README.markdown"
|
|
18
|
+
]
|
|
19
|
+
s.files = [
|
|
20
|
+
"LICENSE",
|
|
21
|
+
"README.markdown",
|
|
22
|
+
"Rakefile",
|
|
23
|
+
"headliner.gemspec",
|
|
24
|
+
"init.rb",
|
|
25
|
+
"lib/headliner.rb",
|
|
26
|
+
"spec/headliner_spec.rb",
|
|
27
|
+
"spec/spec_helper.rb"
|
|
28
|
+
]
|
|
29
|
+
s.homepage = "http://github.com/shura71/headliner"
|
|
30
|
+
s.require_paths = ["lib"]
|
|
31
|
+
s.rubygems_version = "1.8.15"
|
|
32
|
+
s.summary = "Assign and format page titles from your views."
|
|
33
|
+
|
|
34
|
+
if s.respond_to? :specification_version then
|
|
35
|
+
s.specification_version = 3
|
|
36
|
+
|
|
37
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
|
38
|
+
else
|
|
39
|
+
end
|
|
40
|
+
else
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
data/init.rb
ADDED
data/lib/headliner.rb
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
module Headliner
|
|
2
|
+
VERSION = "0.1.1".freeze
|
|
3
|
+
|
|
4
|
+
def title(options, headline='')
|
|
5
|
+
if options.is_a? String
|
|
6
|
+
save_title(options, headline)
|
|
7
|
+
else
|
|
8
|
+
display_title(options)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def save_title(title, headline)
|
|
13
|
+
@title = title.gsub(/<\/?[^>]*>/, '')
|
|
14
|
+
headline.blank? ? title : headline
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def display_title(options)
|
|
18
|
+
# Prefix (leading space)
|
|
19
|
+
if options[:prefix]
|
|
20
|
+
prefix = options[:prefix]
|
|
21
|
+
elsif options[:prefix] == false
|
|
22
|
+
prefix = ''
|
|
23
|
+
else
|
|
24
|
+
prefix = ' '
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Separator
|
|
28
|
+
unless options[:separator].blank?
|
|
29
|
+
separator = options[:separator]
|
|
30
|
+
else
|
|
31
|
+
separator = '|'
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Suffix (trailing space)
|
|
35
|
+
if options[:suffix]
|
|
36
|
+
suffix = options[:suffix]
|
|
37
|
+
elsif options[:suffix] == false
|
|
38
|
+
suffix = ''
|
|
39
|
+
else
|
|
40
|
+
suffix = ' '
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Lowercase title?
|
|
44
|
+
if options[:lowercase] == true
|
|
45
|
+
@title = @title.downcase unless @title.blank?
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Default page title
|
|
49
|
+
if @title.blank? && options[:default]
|
|
50
|
+
@title = options[:default]
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Set website/page order
|
|
54
|
+
unless @title.blank?
|
|
55
|
+
if options[:reverse] == true
|
|
56
|
+
# Reverse order => "Page : Website"
|
|
57
|
+
return content_tag(:title, @title + prefix + separator + suffix + options[:site])
|
|
58
|
+
else
|
|
59
|
+
# Standard order => "Website : Page"
|
|
60
|
+
return content_tag(:title, options[:site] + prefix + separator + suffix + @title)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# If title is blank, return only website name
|
|
65
|
+
content_tag :title, options[:site]
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Mr. T says, "Use my method, fool!"
|
|
69
|
+
alias pt title
|
|
70
|
+
|
|
71
|
+
end
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
|
2
|
+
|
|
3
|
+
describe "Headliner" do
|
|
4
|
+
|
|
5
|
+
before(:each) do
|
|
6
|
+
@view = ActionView::Base.new
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
describe "loading plugin" do
|
|
10
|
+
|
|
11
|
+
it "should be mixed into ActionView::Base" do
|
|
12
|
+
ActionView::Base.included_modules.include?(Headliner).should be_true
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "should respond to 'title' helper" do
|
|
16
|
+
@view.should respond_to(:title)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "should respond to 'title' helper alias" do
|
|
20
|
+
@view.should respond_to(:t)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
describe "saving title" do
|
|
26
|
+
|
|
27
|
+
it "should save title" do
|
|
28
|
+
@view.title("iPhone").should eql("iPhone")
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "should save headline if present" do
|
|
32
|
+
@view.title("iPhone", "iPhone 3G is now on sale!").should eql("iPhone 3G is now on sale!")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
describe "displaying title" do
|
|
38
|
+
|
|
39
|
+
it "should use website name if title is empty" do
|
|
40
|
+
@view.title(:site => "Apple").should eql("<title>Apple</title>")
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "should use website before page by default" do
|
|
44
|
+
save_basic_title
|
|
45
|
+
@view.title(:site => "Apple").should eql("<title>Apple | iPhone</title>")
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it "should only use markup in titles in the view" do
|
|
49
|
+
save_basic_title("<b>iPhone</b>").should eql("<b>iPhone</b>")
|
|
50
|
+
@view.title(:site => "Apple").should eql("<title>Apple | iPhone</title>")
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "should use page before website if :reverse" do
|
|
54
|
+
save_basic_title
|
|
55
|
+
@view.title(:site => "Apple", :reverse => true).should eql("<title>iPhone | Apple</title>")
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "should be lowercase if :lowercase" do
|
|
59
|
+
save_basic_title
|
|
60
|
+
@view.title(:site => "apple", :lowercase => true).should eql("<title>apple | iphone</title>")
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it "should use custom separator if :separator" do
|
|
64
|
+
save_basic_title
|
|
65
|
+
@view.title(:site => "Apple", :separator => "-").should eql("<title>Apple - iPhone</title>")
|
|
66
|
+
@view.title(:site => "Apple", :separator => ":").should eql("<title>Apple : iPhone</title>")
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it "should use custom prefix and suffix if available" do
|
|
70
|
+
save_basic_title
|
|
71
|
+
@view.title(:site => "Apple", :prefix => " |", :suffix => "| ").should eql("<title>Apple ||| iPhone</title>")
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it "should collapse prefix if false" do
|
|
75
|
+
save_basic_title
|
|
76
|
+
@view.title(:site => "Apple", :prefix => false, :separator => ":").should eql("<title>Apple: iPhone</title>")
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it "should collapse suffix if false" do
|
|
80
|
+
save_basic_title
|
|
81
|
+
@view.title(:site => "Apple", :suffix => false, :separator => "~").should eql("<title>Apple ~iPhone</title>")
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
it "should use all custom options if available" do
|
|
85
|
+
save_basic_title
|
|
86
|
+
@view.title(:site => "apple",
|
|
87
|
+
:prefix => " ",
|
|
88
|
+
:suffix => " ",
|
|
89
|
+
:separator => "-",
|
|
90
|
+
:lowercase => true,
|
|
91
|
+
:reverse => true).should eql("<title>iphone - apple</title>")
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
it "should use default one if title is not present or blank" do
|
|
95
|
+
save_basic_title("")
|
|
96
|
+
@view.title(:site => "Apple", :default => "New MacBook").should eql("<title>Apple | New MacBook</title>")
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def save_basic_title(title='iPhone')
|
|
102
|
+
@view.title(title)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: headliner
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
hash: 25
|
|
5
|
+
prerelease:
|
|
6
|
+
segments:
|
|
7
|
+
- 0
|
|
8
|
+
- 1
|
|
9
|
+
- 1
|
|
10
|
+
version: 0.1.1
|
|
11
|
+
platform: ruby
|
|
12
|
+
authors:
|
|
13
|
+
- Alexander Shcherban
|
|
14
|
+
autorequire:
|
|
15
|
+
bindir: bin
|
|
16
|
+
cert_chain: []
|
|
17
|
+
|
|
18
|
+
date: 2012-01-24 00:00:00 Z
|
|
19
|
+
dependencies: []
|
|
20
|
+
|
|
21
|
+
description: Assign and format page titles from your views
|
|
22
|
+
email: shura.71@gmail.com
|
|
23
|
+
executables: []
|
|
24
|
+
|
|
25
|
+
extensions: []
|
|
26
|
+
|
|
27
|
+
extra_rdoc_files:
|
|
28
|
+
- LICENSE
|
|
29
|
+
- README.markdown
|
|
30
|
+
files:
|
|
31
|
+
- LICENSE
|
|
32
|
+
- README.markdown
|
|
33
|
+
- Rakefile
|
|
34
|
+
- headliner.gemspec
|
|
35
|
+
- init.rb
|
|
36
|
+
- lib/headliner.rb
|
|
37
|
+
- spec/headliner_spec.rb
|
|
38
|
+
- spec/spec_helper.rb
|
|
39
|
+
homepage: http://github.com/shura71/headliner
|
|
40
|
+
licenses: []
|
|
41
|
+
|
|
42
|
+
post_install_message:
|
|
43
|
+
rdoc_options: []
|
|
44
|
+
|
|
45
|
+
require_paths:
|
|
46
|
+
- lib
|
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
48
|
+
none: false
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
hash: 3
|
|
53
|
+
segments:
|
|
54
|
+
- 0
|
|
55
|
+
version: "0"
|
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
|
+
none: false
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
hash: 3
|
|
62
|
+
segments:
|
|
63
|
+
- 0
|
|
64
|
+
version: "0"
|
|
65
|
+
requirements: []
|
|
66
|
+
|
|
67
|
+
rubyforge_project:
|
|
68
|
+
rubygems_version: 1.8.15
|
|
69
|
+
signing_key:
|
|
70
|
+
specification_version: 3
|
|
71
|
+
summary: Assign and format page titles from your views.
|
|
72
|
+
test_files: []
|
|
73
|
+
|