lwe-page_title_helper 0.5.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,7 +1,7 @@
1
1
  = Page title helper
2
2
 
3
3
  Ever wondered if there was an easier and DRY-way to set your page titles (and/or headings). Backed
4
- byRails (>= 2.3) new support for <i>I18n</i> the solution is a simple helper method.
4
+ by Rails (only tested on 2.3.x) and it's new <tt>I18n</tt>-class the solution is a simple helper method.
5
5
 
6
6
  In your layout add this to your <tt><head>-section</tt>:
7
7
 
@@ -98,10 +98,13 @@ Note, currently it only makes sense to set <tt>:format</tt> and/or <tt>:default<
98
98
 
99
99
  == Some (maybe useful) interpolations
100
100
 
101
- # internationalized controller names, with fallback
101
+ The internationalized controller name, with fallback to just display the humanized name:
102
+
102
103
  PageTitleHelper.interpolates :controller do |env|
103
104
  I18n.t env.controller.controller_path.tr('/','.') + '.controller', :default => env.controller.controller_name.humanize
104
105
  end
105
106
 
107
+ <b>Note:</b> Where should I put these, as well as the default options? I'd put them in a new file at <tt>config/initializers/page_title.rb</tt> or someting like that.
108
+
106
109
  == Licence and copyright
107
110
  Copyright (c) 2009 Lukas Westermann (Zurich, Switzerland), released under the MIT license
data/Rakefile CHANGED
@@ -35,6 +35,9 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
35
35
  end
36
36
 
37
37
  namespace :metrics do
38
+ desc 'Report all metrics, i.e. stats and code coverage.'
39
+ task :all => [:stats, :coverage]
40
+
38
41
  desc 'Report code statistics for library and tests to shell.'
39
42
  task :stats do |t|
40
43
  require 'code_statistics'
@@ -54,10 +57,10 @@ namespace :metrics do
54
57
  end
55
58
  end
56
59
 
57
- desc 'Start IRB console with loaded test/test_helper.rb and sqlite db.'
60
+ desc 'Start IRB console with loaded test/test_helper.rb and PageTitleHelper.'
58
61
  task :console do |t|
59
62
  chdir File.dirname(__FILE__)
60
- exec 'irb -Ilib/ -r page_title_helper'
63
+ exec 'irb -Ilib -r test/test_helper.rb -r page_title_helper'
61
64
  end
62
65
 
63
66
  desc 'Clean up generated files.'
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
+ :minor: 6
3
+ :patch: 1
2
4
  :major: 0
3
- :minor: 5
4
- :patch: 0
@@ -1,3 +1,12 @@
1
+ # PageTitleHelper provides an +ActionView+ helper method to simplify adding
2
+ # custom titles to pages.
3
+ #
4
+ # Author:: Lukas Westermann
5
+ # Copyright:: Copyright (c) 2009 Lukas Westermann (Zurich, Switzerland)
6
+ # Licence:: MIT-Licence (http://www.opensource.org/licenses/mit-license.php)
7
+ #
8
+ # See documentation for +page_title+ for usage examples and more informations.
9
+
1
10
  require 'ostruct'
2
11
 
3
12
  # PageTitleHelper
@@ -5,13 +14,8 @@ module PageTitleHelper
5
14
  module Interpolations
6
15
  extend self
7
16
 
8
- # Returns a sorted list of all interpolations.
9
- def self.all
10
- self.instance_methods(false).sort
11
- end
12
-
13
17
  def self.interpolate(pattern, *args)
14
- all.reverse.inject(pattern.dup) do |result, tag|
18
+ instance_methods(false).sort.reverse.inject(pattern.dup) do |result, tag|
15
19
  result.gsub(/:#{tag}/) do |match|
16
20
  send(tag, *args)
17
21
  end
@@ -43,29 +47,28 @@ module PageTitleHelper
43
47
  end
44
48
 
45
49
  def page_title(options = nil, &block)
46
- if block_given?
50
+ if block_given? # define title
47
51
  content_for(:page_title) { yield }
48
52
  return read_page_title_content_block
49
53
  end
50
54
 
51
- options = PageTitleHelper.options.merge(options || {})
55
+ options = PageTitleHelper.options.merge(options || {}).symbolize_keys!
52
56
  options.assert_valid_keys(:app, :suffix, :default, :format)
53
57
 
54
- # construct basic env
58
+ # construct basic env to pass around
55
59
  env = OpenStruct.new(:options => options, :view => self, :controller => self.controller)
56
60
 
57
61
  # just return the applications name
58
62
  return Interpolations.app(env) if options[:format] == :app
59
63
 
60
64
  # read page title
61
- page_title = read_page_title_content_block
62
- page_title = I18n.translate(i18n_template_key(options[:suffix]), :default => options[:default]) if page_title.blank?
65
+ env.title = read_page_title_content_block
66
+ env.title = I18n.translate(i18n_template_key(options[:suffix]), :default => options[:default]) if env.title.blank?
63
67
 
64
68
  # return page title if format is set explicitly set to false
65
- return page_title if options[:format] == false
69
+ return env.title if options[:format] == false
66
70
 
67
- # else -> interpolate
68
- env.title = page_title
71
+ # interpolate title
69
72
  Interpolations.interpolate options[:format], env
70
73
  end
71
74
 
@@ -86,12 +89,15 @@ module PageTitleHelper
86
89
  # and also kind of a hack, because this really seems to be some sort if
87
90
  # internal variable, that's why it's "abstracted" away as well.
88
91
  #
89
- def read_first_render_path
90
- @_first_render.template_path
92
+ # Also ensure that the extensions (like <tt>.html.erb</tt> or
93
+ # <tt>.html.haml</tt>) have been stripped of.
94
+ def first_render_path_without_extension
95
+ p = @_first_render.template_path
96
+ p[0,p.index('.')]
91
97
  end
92
98
 
93
99
  def i18n_template_key(suffix = nil)
94
- ikey = read_first_render_path.gsub(/\.html\.erb$/, '').tr('/', '.')
100
+ ikey = first_render_path_without_extension.tr('/', '.')
95
101
  ikey = ikey + "." + suffix.to_s unless suffix.nil?
96
102
  ikey
97
103
  end
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{page_title_helper}
5
- s.version = "0.5.0"
5
+ s.version = "0.6.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Lukas Westermann"]
9
- s.date = %q{2009-07-07}
9
+ s.date = %q{2009-09-10}
10
10
  s.email = %q{lukas.westermann@gmail.com}
11
11
  s.extra_rdoc_files = [
12
12
  "LICENSE",
@@ -24,11 +24,10 @@ Gem::Specification.new do |s|
24
24
  "test/page_title_helper_test.rb",
25
25
  "test/test_helper.rb"
26
26
  ]
27
- s.has_rdoc = true
28
27
  s.homepage = %q{http://github.com/lwe/page_title_helper}
29
28
  s.rdoc_options = ["--charset=UTF-8"]
30
29
  s.require_paths = ["lib"]
31
- s.rubygems_version = %q{1.3.2}
30
+ s.rubygems_version = %q{1.3.4}
32
31
  s.summary = %q{Simple, internationalized and DRY page titles and headings for rails.}
33
32
  s.test_files = [
34
33
  "test/page_title_helper_test.rb",
@@ -22,8 +22,12 @@ class MockView
22
22
  def controller; nil; end
23
23
  end
24
24
 
25
- I18n.backend.store_translations :en, :contacts => { :list => { :title => 'contacts.list.title' }}
25
+ # store translations
26
+ I18n.backend.store_translations :en, :contacts => {
27
+ :list => { :title => 'contacts.list.title', :page_title => 'custom contacts title' },
28
+ :myhaml => { :title => 'this is haml!' }}
26
29
  I18n.backend.store_translations :en, :placeholder => 'Displaying {{name}}'
30
+ I18n.backend.store_translations :en, :app => { :tagline => 'Default', :other_tagline => 'Other default' }
27
31
 
28
32
  class PageTitleHelperTest < ActiveSupport::TestCase
29
33
  test "interpolations" do
@@ -90,4 +94,29 @@ class PageTitleHelperTest < ActiveSupport::TestCase
90
94
  view.page_title { I18n.t :placeholder, :name => 'Bella' }
91
95
  assert_equal "Page title helper - Displaying Bella", view.page_title
92
96
  end
97
+
98
+ test "render translated :'app.tagline' if no title is available" do
99
+ view = MockView.new 'view/does/not_exist.html.erb'
100
+ assert_equal "Page title helper - Default", view.page_title
101
+ end
102
+
103
+ test "render custom 'default' string, if title is not available" do
104
+ view = MockView.new 'view/does/not_exist.html.erb'
105
+ assert_equal 'Page title helper - Some default', view.page_title(:default => 'Some default')
106
+ end
107
+
108
+ test "render custom default translation, if title is not available" do
109
+ view = MockView.new 'view/does/not_exist.html.erb'
110
+ assert_equal 'Page title helper - Other default', view.page_title(:default => :'app.other_tagline')
111
+ end
112
+
113
+ test "render auto-title using custom suffix 'page_title'" do
114
+ view = MockView.new 'contacts/list.html.erb'
115
+ assert_equal 'Page title helper - custom contacts title', view.page_title(:suffix => :page_title)
116
+ end
117
+
118
+ test "ensure that it works with other template engines, like .html.haml" do
119
+ view = MockView.new('contacts/myhaml.html.haml')
120
+ assert_equal 'Page title helper - this is haml!', view.page_title
121
+ end
93
122
  end
data/test/test_helper.rb CHANGED
@@ -1,7 +1,11 @@
1
1
  require 'rubygems'
2
- require 'test/unit'
3
2
  require 'active_support'
4
- require 'active_support/test_case'
3
+ require 'action_view'
4
+
5
+ unless Object.const_defined?('IRB')
6
+ require 'test/unit'
7
+ require 'active_support/test_case'
8
+ end
5
9
 
6
10
  ROOT = File.expand_path File.dirname(File.dirname(__FILE__))
7
11
  RAILS_ROOT = '/this/is/just/for/testing/page_title_helper'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lwe-page_title_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas Westermann
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-07 00:00:00 -07:00
12
+ date: 2009-09-10 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -33,7 +33,7 @@ files:
33
33
  - page_title_helper.gemspec
34
34
  - test/page_title_helper_test.rb
35
35
  - test/test_helper.rb
36
- has_rdoc: true
36
+ has_rdoc: false
37
37
  homepage: http://github.com/lwe/page_title_helper
38
38
  post_install_message:
39
39
  rdoc_options: