hierarchical_page_titles 0.1.0.pre

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .DS_Store
2
+ *.tmproj
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.8.7@hierarchical_page_titles
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,126 @@
1
+ ## hierarchical_page_titles
2
+
3
+ ### What is this?
4
+
5
+ This is a little gem that provides controller and view methods to make displaying of window/page titles DRYer.
6
+
7
+ ### Why did you make it?
8
+
9
+ I made it because I found that I was doing the same thing over and over in my Rails apps. Setting the window or page title is a very common thing, and usually you'd handle this by simply setting a `@title` variable in your view and using it in your layout. However, I kept running into the case where I wanted to set the window title separate from the page title, or I wanted to set part of the window title globally for a controller, or I wanted to set the window title but hide the page title, and so on. So I decided to come up with a way to satisfy these requirements and yet keep things simple.
10
+
11
+ ### How do I use it?
12
+
13
+ I'm going to walk through a few use cases and hopefully you'll get the idea.
14
+
15
+ #### Window and page title are the same
16
+
17
+ Here's what your layout might look like:
18
+
19
+ <html>
20
+ <head><title><%= window_title %></title></head>
21
+ <body>
22
+ <h1><%= page_title %></h1>
23
+ <%= yield %>
24
+ </body>
25
+ </html>
26
+
27
+ So in your view, you'd do this:
28
+
29
+ <% title "Some Page" %>
30
+ <p>Some content</p>
31
+
32
+ And now when your layout is rendered, it will be this:
33
+
34
+ <html>
35
+ <head><title>Some Page</title></head>
36
+ <body>
37
+ <h1>Some Page</h1>
38
+ <p>Some content</p>
39
+ </body>
40
+ </html>
41
+
42
+ #### Window title with prefix
43
+
44
+ Nothing special required here, just put it in your layout:
45
+
46
+ <html>
47
+ <head><title>My Site - <%= window_title %></title></head>
48
+ <body>
49
+ <h1><%= page_title %></h1>
50
+ <%= yield %>
51
+ </body>
52
+ </html>
53
+
54
+ And when the page is rendered:
55
+
56
+ <html>
57
+ <head><title>My Site - Some Page</title></head>
58
+ <body>
59
+ <h1>Some Page</h1>
60
+ <p>Some content</p>
61
+ </body>
62
+ </html>
63
+
64
+ #### Hiding page title for certain pages
65
+
66
+ #### A page hierarchy
67
+
68
+ Controllers:
69
+
70
+ <pre>
71
+ <code>
72
+ class ApplicationController < ActionController::Base
73
+ window_title "My Site Name"
74
+ end
75
+ class SupportController < ApplicationController
76
+ window_title "Support"
77
+ def billing; end
78
+ end
79
+ </code>
80
+ </pre>
81
+
82
+ Views:
83
+
84
+ <pre>
85
+ <code>
86
+ #==== app/views/layouts/application.html.erb ====
87
+ <html>
88
+ <head><title><%= window_title %></title></head>
89
+ <body>
90
+ <h2><%= page_title %></h2>
91
+ </body>
92
+ </html>
93
+ #==== app/views/support/billing.html.erb ====
94
+ # this adds to the window title and the page title at the same time
95
+ <% title "Billing FAQs" %>
96
+ </code>
97
+ </pre>
98
+
99
+ When the billing view is rendered:
100
+
101
+ * The window title will be "My Site Name - Support - Billing FAQs"
102
+ * The page title (in the &lt;h2&gt; tag) will be "Billing FAQs"
103
+
104
+ ### How is it different from XYZ?
105
+
106
+ There are several related gems/plugins:
107
+
108
+ * **dynamic-page-title**: Provides options to `title` which are unnecessary. No support for hierarchical (controller-level) titles. Isn't compatible with Rails 3.
109
+ * **entitled**: Interesting, but has totally different requirements.
110
+ * **happy-titles**: The idea here is that you specify a template which I think is inflexible. No support for hierarchical titles, only supports two levels.
111
+ * **headliner**: A good design for simple requirements, and compatible with Rails 3. Again, no support for hierarchy, though. Plus, the `t` shortcut conflicts with i18n, which is bad. Not a gem.
112
+ * **page_title_helper**: Good if you need i18n, otherwise is a bit overkill. Again, no support for hierarchy, though.
113
+ * **smart_titles**: Stupid simple, and provides i18n. Again, no support for hierarchy, though.
114
+ * **title_helper**: Not sure, Github project seems to be missing.
115
+ * [**title_helpers**](https://github.com/henrik/title_helpers): Supports a page prefix, but doesn't support full hierarchy. Plus, I don't really like the API, and it isn't a gem.
116
+
117
+ ### How do I install it?
118
+
119
+ gem install hierarchical_page_titles
120
+
121
+ ### How do I contribute?
122
+
123
+ ### Who wrote this?
124
+
125
+ (c) 2009 Elliot Winkler (elliot dot winkler at gmail dot com).
126
+ Released under the MIT license.
data/README.textile ADDED
@@ -0,0 +1,69 @@
1
+ h2. title_helpers
2
+
3
+ h3. Summary
4
+
5
+ Rails plugin that provides controller and view methods to make displaying of
6
+ window/page titles DRYer.
7
+
8
+ h3. Rationale
9
+
10
+ Most web sites you'll build have some sort of hierarchy. And most times this
11
+ hierarchy will correspond to your controllers. For instance, let's say you have a
12
+ support section on your website (@SupportController@), and under that you have FAQs
13
+ about billing (the @billing@ action). Now, I think it's important for the window
14
+ title to be informative -- in fact, it should come directly from the page hierarchy.
15
+ So, going back to our billing FAQs page, you might want the window title to be
16
+ "Foo.com - Support - Billing FAQs". But that's going to be a pattern, isn't it?
17
+ "Foo.com - #{some controller} - #{some action}". You're not really going to define
18
+ the full window title for every action in your code, are you? I'm certainly not going
19
+ to. Wouldn't it be better if you could set the window title on a controller basis,
20
+ and then, in your view, set the window title for just the action?
21
+
22
+ h3. Example
23
+
24
+ Controllers:
25
+
26
+ <pre>
27
+ <code>
28
+ class ApplicationController < ActionController::Base
29
+ window_title "My Site Name"
30
+ end
31
+ class SupportController < ApplicationController
32
+ window_title "Support"
33
+ def billing; end
34
+ end
35
+ </code>
36
+ </pre>
37
+
38
+ Views:
39
+
40
+ <pre>
41
+ <code>
42
+ #==== app/views/layouts/application.html.erb ====
43
+ <html>
44
+ <head><title><%= window_title %></title></head>
45
+ <body>
46
+ <h2><%= page_title %></h2>
47
+ </body>
48
+ </html>
49
+ #==== app/views/support/billing.html.erb ====
50
+ # this adds to the window title and the page title at the same time
51
+ <% title "Billing FAQs" %>
52
+ </code>
53
+ </pre>
54
+
55
+ When the billing view is rendered:
56
+
57
+ * The window title will be "My Site Name - Support - Billing FAQs"
58
+ * The page title (in the &lt;h2&gt; tag) will be "Billing FAQs"
59
+
60
+ h3. Installation
61
+
62
+ <pre>
63
+ script/plugin install git://github.com/mcmire/title_helpers.git
64
+ </pre>
65
+
66
+ h3. Author
67
+
68
+ (c) 2009 Elliot Winkler (elliot dot winkler at gmail dot com).
69
+ Released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "hierarchical_page_titles/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "hierarchical_page_titles"
7
+ s.version = HierarchicalPageTitles::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Elliot Winkler"]
10
+ s.email = ["elliot.winkler@gmail.com"]
11
+ s.homepage = "http://github.com/mcmire/hierarchical_page_titles"
12
+ s.summary = %q{A gem that provides controller and view methods to make displaying of window/page titles DRYer.}
13
+ s.description = %q{A gem that provides controller and view methods to make displaying of window/page titles DRYer.}
14
+
15
+ #s.rubyforge_project = "title_helpers"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,46 @@
1
+ require 'hierarchical_page_titles/shared_methods'
2
+
3
+ module HierarchicalPageTitles
4
+ module ControllerHelpers
5
+ extend ActiveSupport::Concern
6
+
7
+ include SharedMethods
8
+
9
+ module ClassMethods
10
+ # Call this in the body of your controller with a string to add the string to
11
+ # the window title before each action in your controller. This will work
12
+ # for subcontrollers too -- so if you call window_title in a supercontroller
13
+ # and also in a subcontroller, there will be two strings when you go to output
14
+ # the window title. You can pass :only and :except to limit this to certain actions.
15
+ def window_title(*args, &block)
16
+ options = Hash === args.last ? args.last : {}
17
+ before_filter(options) {|c| c.window_title(*args, &block) }
18
+ end
19
+
20
+ # Call this in the body of your controller with a string to set the page title
21
+ # globally for each action in your controller. Unlike window_title, this will not
22
+ # work for subcontrollers -- so if you call page_title in a supercontroller and
23
+ # also in a subcontroller, the subcontroller's title will override the supercontroller's.
24
+ # You can pass :only and :except to limit this to certain actions.
25
+ def page_title(*args, &block)
26
+ options = Hash === args.last ? args.last : {}
27
+ before_filter(options) {|c| c.page_title(*args, &block) }
28
+ end
29
+
30
+ # Call this in the body of your controller with a string to add it to the
31
+ # window title AND set the page title at the same time.
32
+ # See +window_title+ and +page_title+ for more.
33
+ # You can pass :only and :except to limit this to certain actions.
34
+ def title(*args, &block)
35
+ options = Hash === args.last ? args.last : {}
36
+ before_filter(options) {|c| c.title(*args, &block) }
37
+ end
38
+
39
+ # Call this in the body of your controller to tell page_title to not show anything.
40
+ # You can pass :only and :except to limit this to certain actions.
41
+ def hide_page_title(options={})
42
+ before_filter(options) {|c| c.page_title(nil) }
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,14 @@
1
+ require 'rails'
2
+
3
+ require 'hierarchical_page_titles/controller_helpers'
4
+ require 'hierarchical_page_titles/view_helpers'
5
+
6
+ module HierarchicalPageTitles
7
+ class Railtie < Rails::Railtie
8
+ # copied from formtastic
9
+ initializer "title-helpers.initialize", :after => :after_initialize do
10
+ ActionController::Base.class_eval { include HierarchicalPageTitles::ControllerHelpers }
11
+ ActionView::Base.class_eval { include HierarchicalPageTitles::ViewHelpers }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,40 @@
1
+ module HierarchicalPageTitles
2
+ module SharedMethods
3
+ # Call this in your view with a string to add the string to the array of window titles.
4
+ # Call this in your layout to output the window title. You can specify a separator
5
+ # that should be put between the titles by passing <tt>:separator => " - "</tt>.
6
+ def window_title(*args, &block)
7
+ options = args.extract_options!
8
+ titles = args.flatten
9
+ @_window_titles ||= []
10
+ if block_given?
11
+ @_window_titles << block.call
12
+ else
13
+ @_window_titles += titles
14
+ end
15
+ @_window_title_set = true
16
+ end
17
+
18
+ # Call this in your view with a string to set the page title to that string.
19
+ # Call this in your layout with no arguments to output the page title.
20
+ def page_title(*args, &block)
21
+ options = args.extract_options!
22
+ title = args.first
23
+ @_page_title = (block_given? ? block.call : title)
24
+ @_page_title_set = true
25
+ end
26
+
27
+ # Call this in your view to set the window title and the page title at the same time.
28
+ # See +window_title+ and +page_title+ for more.
29
+ def title(*args, &block)
30
+ options = args.extract_options!
31
+ window_title(*args, &block)
32
+ page_title(args.last, &block)
33
+ @_title_set = true
34
+ end
35
+
36
+ def titles
37
+ @_window_titles
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module HierarchicalPageTitles
2
+ VERSION = "0.1.0.pre"
3
+ end
@@ -0,0 +1,53 @@
1
+ require 'hierarchical_page_titles/shared_methods'
2
+
3
+ module HierarchicalPageTitles
4
+ module ViewHelpers
5
+ include SharedMethods
6
+
7
+ # Call this in your view with a string to add the string to the array of window titles.
8
+ # Call this in your layout to output the window title. You can specify a separator
9
+ # that should be put between the titles by passing <tt>:separator => " - "</tt>.
10
+ def window_title(*args, &block)
11
+ options = args.extract_options!
12
+ if args.empty?
13
+ options[:separator] ||= " - "
14
+ @_window_titles ||= []
15
+ prefix = Array(options[:prefix]) || []
16
+ (prefix + @_window_titles).join(options[:separator])
17
+ else
18
+ super
19
+ end
20
+ end
21
+
22
+ # Call this in your view with a string to set the page title to that string.
23
+ # Call this in your layout with no arguments to output the page title.
24
+ def page_title(*args)
25
+ options = args.extract_options!
26
+ if args.empty?
27
+ @_page_title
28
+ else
29
+ super
30
+ end
31
+ end
32
+
33
+ # TODO: Document
34
+ def page_title?
35
+ @_page_title.present?
36
+ end
37
+
38
+ # TODO: Document
39
+ def window_title_set?
40
+ @_window_title_set
41
+ end
42
+
43
+ # TODO: Document
44
+ def page_title_set?
45
+ @_window_title_set
46
+ end
47
+
48
+ # TODO: Document
49
+ def title_set?
50
+ @_title_set
51
+ end
52
+ end
53
+ end
@@ -0,0 +1 @@
1
+ require 'hierarchical_page_titles/railtie'
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hierarchical_page_titles
3
+ version: !ruby/object:Gem::Version
4
+ hash: 961915980
5
+ prerelease: true
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ - pre
11
+ version: 0.1.0.pre
12
+ platform: ruby
13
+ authors:
14
+ - Elliot Winkler
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-12-08 00:00:00 -07:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: A gem that provides controller and view methods to make displaying of window/page titles DRYer.
24
+ email:
25
+ - elliot.winkler@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - .gitignore
34
+ - .rvmrc
35
+ - Gemfile
36
+ - README.md
37
+ - README.textile
38
+ - Rakefile
39
+ - hierarchical_page_titles.gemspec
40
+ - lib/hierarchical_page_titles.rb
41
+ - lib/hierarchical_page_titles/controller_helpers.rb
42
+ - lib/hierarchical_page_titles/railtie.rb
43
+ - lib/hierarchical_page_titles/shared_methods.rb
44
+ - lib/hierarchical_page_titles/version.rb
45
+ - lib/hierarchical_page_titles/view_helpers.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/mcmire/hierarchical_page_titles
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">"
68
+ - !ruby/object:Gem::Version
69
+ hash: 25
70
+ segments:
71
+ - 1
72
+ - 3
73
+ - 1
74
+ version: 1.3.1
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.3.7
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: A gem that provides controller and view methods to make displaying of window/page titles DRYer.
82
+ test_files: []
83
+