lwe-page_title_helper 0.4.0 → 0.5.0

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/README.rdoc CHANGED
@@ -61,8 +61,8 @@ prependor appended, or if it contains the account name etc. It uses a similar ap
61
61
  Adding custom interpolations is as easy as defining a block, for example to access the current
62
62
  controller:
63
63
 
64
- PageTitleHelper.interpolates :controller do |title,options|
65
- controller.class.humanize
64
+ PageTitleHelper.interpolates :controller do |env|
65
+ env.controller.controller_name.humanize
66
66
  end
67
67
 
68
68
  page_title :format => ':title / :controller / :app' # => "Welcome back / Dashboard / My cool app"
@@ -77,16 +77,31 @@ To access just the title, without any magic app stuff interpolated or appended,
77
77
  * <tt>page_title { ... }</tt> - not per se an option, but if a block is given the methods sets a
78
78
  custom title and overwrites any DRY-I18n title.
79
79
  * <tt>:app</tt> - specifiy the applications name, however it's recommended to define the
80
- translation key <tt>:'app.name'</tt>. If set to <tt>true</tt>, just returns the app name,
81
- as defined in the translations or based on the +RAILS_ROOT+ humanized basename.
80
+ translation key <tt>:'app.name'</tt> or use the the fallback behaviour for the humanzied
81
+ basename of +RAILS_ROOT+.
82
82
  * <tt>:default</tt> - string which is displayed when no translation exists and no custom title
83
83
  has been specified. Can also be set to a symbol or array to take advantage of
84
84
  <tt>I18n.translate</tt>s <tt>:default</tt> option. (Default is <tt>:'app.tagline'</tt>)
85
85
  * <tt>:format</tt> - defines the output format, accepts a string containing multiple interpolations,
86
86
  see <i>More fun with <tt>:format</tt></i>. If set to +false+, just the current title is returned.
87
+ If <tt>:format => :app</tt> then just the application name is returned.
87
88
  (Default is <tt>":app - :title"</tt>)
88
89
  * <tt>:suffix</tt> - not happy with the fact that the translations must be named like
89
90
  <tt>en -> contacts -> index -> title</tt>, but prefer e.g. them to be suffixed with
90
91
  <tt>page_title</tt>? Then just set <tt>:suffix => :page_title</tt>. (Default <tt>:title</tt>)
91
92
 
93
+ If an option should be set globally it's possible to change the default options hash as follows:
94
+
95
+ PageTitleHelper.options[:format] = ':title / :app'
96
+
97
+ Note, currently it only makes sense to set <tt>:format</tt> and/or <tt>:default</tt> globally.
98
+
99
+ == Some (maybe useful) interpolations
100
+
101
+ # internationalized controller names, with fallback
102
+ PageTitleHelper.interpolates :controller do |env|
103
+ I18n.t env.controller.controller_path.tr('/','.') + '.controller', :default => env.controller.controller_name.humanize
104
+ end
105
+
106
+ == Licence and copyright
92
107
  Copyright (c) 2009 Lukas Westermann (Zurich, Switzerland), released under the MIT license
data/Rakefile CHANGED
@@ -63,4 +63,5 @@ end
63
63
  desc 'Clean up generated files.'
64
64
  task :clean do |t|
65
65
  FileUtils.rm_rf "doc"
66
- end
66
+ FileUtils.rm_rf "pkg"
67
+ end
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 4
3
+ :minor: 5
4
4
  :patch: 0
@@ -1,3 +1,5 @@
1
+ require 'ostruct'
2
+
1
3
  # PageTitleHelper
2
4
  module PageTitleHelper
3
5
  module Interpolations
@@ -16,12 +18,12 @@ module PageTitleHelper
16
18
  end
17
19
  end
18
20
 
19
- def app(title, options)
20
- options[:app] || I18n.translate(:'app.name', :default => File.basename(RAILS_ROOT).humanize)
21
+ def app(env)
22
+ env.options[:app] || I18n.translate(:'app.name', :default => File.basename(RAILS_ROOT).humanize)
21
23
  end
22
24
 
23
- def title(title, options)
24
- title
25
+ def title(env)
26
+ env.title
25
27
  end
26
28
  end
27
29
 
@@ -45,21 +47,26 @@ module PageTitleHelper
45
47
  content_for(:page_title) { yield }
46
48
  return read_page_title_content_block
47
49
  end
48
-
50
+
49
51
  options = PageTitleHelper.options.merge(options || {})
50
52
  options.assert_valid_keys(:app, :suffix, :default, :format)
53
+
54
+ # construct basic env
55
+ env = OpenStruct.new(:options => options, :view => self, :controller => self.controller)
56
+
51
57
  # just return the applications name
52
- return Interpolations.app('', {}) if options[:app] == true
58
+ return Interpolations.app(env) if options[:format] == :app
53
59
 
54
60
  # read page title
55
61
  page_title = read_page_title_content_block
56
62
  page_title = I18n.translate(i18n_template_key(options[:suffix]), :default => options[:default]) if page_title.blank?
57
63
 
58
- # return page title if format is set explicitly to false
64
+ # return page title if format is set explicitly set to false
59
65
  return page_title if options[:format] == false
60
66
 
61
67
  # else -> interpolate
62
- Interpolations.interpolate options[:format] || ':app - :title', page_title, options
68
+ env.title = page_title
69
+ Interpolations.interpolate options[:format], env
63
70
  end
64
71
 
65
72
  protected
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{page_title_helper}
5
- s.version = "0.4.0"
5
+ s.version = "0.5.0"
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"]
@@ -18,27 +18,28 @@ class MockView
18
18
  def content_for(sym, &block)
19
19
  instance_variable_set('@content_for_' + sym.to_s, yield)
20
20
  end
21
+
22
+ def controller; nil; end
21
23
  end
22
24
 
23
25
  I18n.backend.store_translations :en, :contacts => { :list => { :title => 'contacts.list.title' }}
24
26
  I18n.backend.store_translations :en, :placeholder => 'Displaying {{name}}'
25
27
 
26
-
27
28
  class PageTitleHelperTest < ActiveSupport::TestCase
28
29
  test "interpolations" do
29
- assert_equal 'Page title helper', PageTitleHelper::Interpolations.app('untitled', {})
30
- assert_equal 'Appname', PageTitleHelper::Interpolations.app('untitled', { :app => 'Appname' })
31
- assert_equal 'untitled', PageTitleHelper::Interpolations.title('untitled', {})
30
+ assert_equal 'Page title helper', PageTitleHelper::Interpolations.app(OpenStruct.new(:options => {}))
31
+ assert_equal 'Appname', PageTitleHelper::Interpolations.app(OpenStruct.new(:options => { :app => 'Appname' }))
32
+ assert_equal 'untitled', PageTitleHelper::Interpolations.title(OpenStruct.new({:title => 'untitled'}))
32
33
  end
33
34
 
34
35
  test "adding custom interpolation" do
35
36
  # extend Interpolations
36
- PageTitleHelper.interpolates :app_reverse do |title, options|
37
- app(title,options).reverse.downcase
37
+ PageTitleHelper.interpolates :app_reverse do |env|
38
+ app(env).reverse.downcase
38
39
  end
39
40
 
40
- assert_equal "anna", PageTitleHelper::Interpolations.app_reverse('untitled', { :app => 'Anna' })
41
- assert_equal "ppa", PageTitleHelper::Interpolations.interpolate(':app_reverse', 'foo', { :app => 'app' })
41
+ assert_equal "anna", PageTitleHelper::Interpolations.app_reverse(OpenStruct.new(:options => { :app => 'Anna' }))
42
+ assert_equal "ppa", PageTitleHelper::Interpolations.interpolate(':app_reverse', OpenStruct.new(:options => { :app => 'app' }))
42
43
  end
43
44
 
44
45
  test "setting title to 'foo' returns 'foo'" do
@@ -52,9 +53,14 @@ class PageTitleHelperTest < ActiveSupport::TestCase
52
53
  assert_equal 'Page title helper - contacts.list.title', view.page_title
53
54
  end
54
55
 
55
- test "printing app name only if :app => true" do
56
+ test "printing app name only if :format => :app" do
57
+ view = MockView.new
58
+ assert_equal 'Page title helper', view.page_title(:format => :app)
59
+ end
60
+
61
+ test "printing custom app name if :app defined and :format => :app" do
56
62
  view = MockView.new
57
- assert_equal 'Page title helper', view.page_title(:app => true)
63
+ assert_equal "Some app", view.page_title(:app => 'Some app', :format => :app)
58
64
  end
59
65
 
60
66
  test "custom formatting options" do
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.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas Westermann