smart_titles 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in smart_titles.gemspec
4
4
  gemspec
5
+
6
+ group :guards do
7
+ gem 'guard-minitest'
8
+ end
data/Gemfile.lock CHANGED
@@ -1,66 +1,58 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- smart_titles (0.2.1)
4
+ smart_titles (0.3.0)
5
5
  actionpack
6
6
  activesupport
7
7
 
8
8
  GEM
9
9
  remote: http://rubygems.org/
10
10
  specs:
11
- actionpack (3.1.0)
12
- activemodel (= 3.1.0)
13
- activesupport (= 3.1.0)
11
+ actionpack (3.1.3)
12
+ activemodel (= 3.1.3)
13
+ activesupport (= 3.1.3)
14
14
  builder (~> 3.0.0)
15
15
  erubis (~> 2.7.0)
16
16
  i18n (~> 0.6)
17
- rack (~> 1.3.2)
18
- rack-cache (~> 1.0.3)
17
+ rack (~> 1.3.5)
18
+ rack-cache (~> 1.1)
19
19
  rack-mount (~> 0.8.2)
20
20
  rack-test (~> 0.6.1)
21
- sprockets (~> 2.0.0)
22
- activemodel (3.1.0)
23
- activesupport (= 3.1.0)
24
- bcrypt-ruby (~> 3.0.0)
21
+ sprockets (~> 2.0.3)
22
+ activemodel (3.1.3)
23
+ activesupport (= 3.1.3)
25
24
  builder (~> 3.0.0)
26
25
  i18n (~> 0.6)
27
- activesupport (3.1.0)
26
+ activesupport (3.1.3)
28
27
  multi_json (~> 1.0)
29
- bcrypt-ruby (3.0.0)
30
28
  builder (3.0.0)
31
- diff-lcs (1.1.3)
32
29
  erubis (2.7.0)
30
+ guard (0.8.8)
31
+ thor (~> 0.14.6)
32
+ guard-minitest (0.4.0)
33
+ guard (~> 0.4)
33
34
  hike (1.2.1)
34
35
  i18n (0.6.0)
35
- mocha (0.9.12)
36
- multi_json (1.0.3)
37
- rack (1.3.2)
38
- rack-cache (1.0.3)
36
+ multi_json (1.0.4)
37
+ rack (1.3.5)
38
+ rack-cache (1.1)
39
39
  rack (>= 0.4)
40
40
  rack-mount (0.8.3)
41
41
  rack (>= 1.0.0)
42
42
  rack-test (0.6.1)
43
43
  rack (>= 1.0)
44
- rake (0.9.2)
45
- rspec (2.6.0)
46
- rspec-core (~> 2.6.0)
47
- rspec-expectations (~> 2.6.0)
48
- rspec-mocks (~> 2.6.0)
49
- rspec-core (2.6.4)
50
- rspec-expectations (2.6.0)
51
- diff-lcs (~> 1.1.2)
52
- rspec-mocks (2.6.0)
53
- sprockets (2.0.0)
44
+ rake (0.9.2.2)
45
+ sprockets (2.0.3)
54
46
  hike (~> 1.2)
55
47
  rack (~> 1.0)
56
- tilt (!= 1.3.0, ~> 1.1)
48
+ tilt (~> 1.1, != 1.3.0)
49
+ thor (0.14.6)
57
50
  tilt (1.3.3)
58
51
 
59
52
  PLATFORMS
60
53
  ruby
61
54
 
62
55
  DEPENDENCIES
63
- mocha
56
+ guard-minitest
64
57
  rake
65
- rspec
66
58
  smart_titles!
data/Guardfile ADDED
@@ -0,0 +1,14 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'minitest' do
5
+ # with Minitest::Unit
6
+ watch(%r|^test/(.*)_test\.rb|)
7
+ watch(%r|^lib/(.*)\.rb|) { |m| "test/#{m[1]}_test.rb" }
8
+ watch(%r|^test/test_helper\.rb|) { "test" }
9
+
10
+ # with Minitest::Spec
11
+ # watch(%r|^spec/(.*)_spec\.rb|)
12
+ # watch(%r|^lib/(.*)\.rb|) { |m| "spec/#{m[1]}_spec.rb" }
13
+ # watch(%r|^spec/spec_helper\.rb|) { "spec" }
14
+ end
data/README.md CHANGED
@@ -46,7 +46,7 @@ And then you should just call `title` without any arguments.
46
46
 
47
47
  <%= title %>
48
48
 
49
- Note that you don't have to call it if you don't output it.
49
+ Note that you _have to call it_ even if you don't output it.
50
50
 
51
51
  <% title %>
52
52
 
@@ -73,6 +73,15 @@ And now products#index page will have "Products from the Coolest Store" browser'
73
73
 
74
74
  Changelog
75
75
  ---
76
+ ### 0.3.0
77
+ * Important change in the documentation: you have to call `title` with no arguments when you want to use translated title.
78
+ * Fixed bug when `head_title` returned default title even if there was a custom title in locale.
79
+ * Added actual Railtie class for reliable integration. This fixes a problem when sometimes helper wasn't included for some reason.
80
+ * Internal changes that you probably don't care about.
81
+ * There are now only 2 public helper methods: `title` and `head_title`.
82
+ * All helper methods are documented.
83
+ * Missing translation is now detected more safely.
84
+
76
85
  ### 0.2.0
77
86
  * `page_title` renamed to `head_title`.
78
87
  * Added support for :title_template translation
data/Rakefile CHANGED
@@ -1,2 +1,11 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+
4
+ require "rake/testtask"
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = Dir['test/**/*_test.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ task :default => :test
@@ -1,37 +1,49 @@
1
1
  module SmartTitles
2
- module Helper
3
- MISSING_TRANSLATION = 0
4
-
5
- def head_title(*args)
6
- options = args.extract_options!
7
- options[:template] = true if options[:template].nil?
8
- custom_default_title = args.first
2
+ MISSING_TRANSLATION = Object.new
9
3
 
10
- if options[:template] && title = page_title
4
+ module Helper
5
+ # <title><%= head_title %></title>
6
+ # Will return title if it was set for the current page.
7
+ # Otherwise will return :title translation.
8
+ #
9
+ # <title><%= head_title "My Blog" %></title>
10
+ # The default title will be "My Blog" instead of :title translation.
11
+ #
12
+ # Example usage with title template:
13
+ # en:
14
+ # title: The Coolest Store
15
+ # title_template: %{title} from the Coolest Store
16
+ #
17
+ # head_title # => The Coolest Store
18
+ # title "Drinks" # => <h1>Drinks</h1>
19
+ # head_title # => Drinks from the Coolest Store
20
+ def head_title(default_title = t(:title))
21
+ if title = page_title
11
22
  t(:title_template, title: title, default: title)
12
23
  else
13
- page_title || custom_default_title || default_title
24
+ default_title
14
25
  end
15
26
  end
16
27
 
17
- attr_writer :page_title
18
-
19
- def page_title
20
- @page_title || default_page_title
21
- end
22
-
23
- def title(new_title = nil)
24
- self.page_title = new_title if new_title
28
+ # Convinient helper method that will:
29
+ # * Set custom title for the current page if it is passed. Otherwise the title will be automatically set
30
+ # * Return the title passed or looked up from locale wrapped into h1 tag
31
+ def title(custom_title = nil)
32
+ provide(:page_title, custom_title || page_title)
25
33
  content_tag(:h1, page_title) if page_title
26
34
  end
27
35
 
28
- def default_page_title
29
- translation = t('.title', default: MISSING_TRANSLATION)
30
- translation unless translation == MISSING_TRANSLATION
31
- end
36
+ private
32
37
 
33
- def default_title
34
- t(:title)
38
+ # This is a page title that was set by the #title helper method
39
+ # It defaults to ".title" translation
40
+ def page_title
41
+ if content_for?(:page_title)
42
+ content_for(:page_title)
43
+ else
44
+ translation = t('.title', default: MISSING_TRANSLATION)
45
+ translation unless translation.equal? MISSING_TRANSLATION
46
+ end
35
47
  end
36
48
  end
37
49
  end
@@ -1 +1,11 @@
1
- ActionView::Helpers.send :include, SmartTitles::Helper
1
+ module SmartTitles
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer "smart_titles.helper" do
5
+ ActiveSupport.on_load :action_view do
6
+ include SmartTitles::Helper
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module SmartTitles
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/smart_titles.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  require "smart_titles/helper"
2
- require "smart_titles/railtie"
2
+ require "smart_titles/railtie" if defined?(Rails)
data/smart_titles.gemspec CHANGED
@@ -10,13 +10,11 @@ Gem::Specification.new do |s|
10
10
  s.email = "sema@sema.in"
11
11
  s.homepage = "http://github.com/semaperepelitsa/smart_titles"
12
12
  s.license = "MIT"
13
- s.summary = %q{Really convenient way to set up page titles in Rails application.}
13
+ s.summary = %q{Really convenient way to set up page titles in a Rails application.}
14
14
 
15
15
  s.add_dependency 'activesupport'
16
16
  s.add_dependency 'actionpack'
17
- s.add_development_dependency 'rspec'
18
17
  s.add_development_dependency 'rake'
19
- s.add_development_dependency 'mocha'
20
18
 
21
19
  s.files = `git ls-files`.split("\n")
22
20
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -0,0 +1,136 @@
1
+ require "test_helper"
2
+
3
+ class SmartTitlesHelperTest < ActionView::TestCase
4
+ include SmartTitles::Helper
5
+
6
+ def setup
7
+ super
8
+ @av = ActionView::Base.new
9
+ @view_flow = ActionView::OutputFlow.new
10
+ end
11
+
12
+ def inside_view
13
+ @virtual_path = 'posts/new'
14
+ end
15
+
16
+ def inside_layout
17
+ @virtual_path = 'layouts/application'
18
+ end
19
+
20
+ def title(*args)
21
+ inside_view
22
+ super
23
+ end
24
+
25
+ def head_title(*args)
26
+ inside_layout
27
+ super
28
+ end
29
+
30
+ def teardown
31
+ I18n.backend.reload!
32
+ end
33
+
34
+
35
+ def test_custom_title
36
+ store_global_title
37
+ store_page_title
38
+ assert_equal "<h1>Hi</h1>", title("Hi")
39
+ end
40
+
41
+ def test_translated_title
42
+ store_global_title
43
+ store_page_title
44
+ assert_equal "<h1>New post</h1>", title
45
+ end
46
+
47
+ def test_no_page_title
48
+ store_global_title
49
+ assert_nil title
50
+ end
51
+
52
+ def test_no_title
53
+ assert_nil title
54
+ end
55
+
56
+
57
+ def test_head_title_with_custom_title
58
+ store_global_title
59
+ store_page_title
60
+ title("Hi")
61
+ assert_equal "Hi", head_title
62
+ end
63
+
64
+ def test_head_title_with_translated_title
65
+ store_global_title
66
+ store_page_title
67
+ title
68
+ assert_equal "New post", head_title
69
+ end
70
+
71
+ def test_head_title_with_translated_global_title
72
+ store_global_title
73
+ assert_equal "My Website", head_title
74
+ end
75
+
76
+ def test_head_title_with_no_title
77
+ assert_includes head_title, "translation missing: en.title"
78
+ end
79
+
80
+
81
+ def test_head_title_with_custom_global_title
82
+ assert_equal "Default", head_title("Default")
83
+ end
84
+
85
+ def test_head_title_with_custom_global_title_and_custom_title
86
+ title("Hi")
87
+ assert_equal "Hi", head_title("Default")
88
+ end
89
+
90
+
91
+ def test_head_title_with_template_and_translated_titles
92
+ store_global_title
93
+ store_page_title
94
+ store_title_template
95
+ title
96
+ assert_equal "d New post b", head_title
97
+ end
98
+
99
+ def test_head_title_with_template_and_custom_title
100
+ store_global_title
101
+ store_page_title
102
+ store_title_template
103
+ title("Hi")
104
+ assert_equal "d Hi b", head_title
105
+ end
106
+
107
+ def test_head_title_with_template_and_translated_title
108
+ store_global_title
109
+ store_title_template
110
+ assert_equal "My Website", head_title
111
+ end
112
+
113
+ private
114
+
115
+ def store_translations(*args)
116
+ I18n.backend.store_translations(:en, *args)
117
+ end
118
+
119
+ def store_page_title
120
+ store_translations posts: { new: { title: "New post" } }
121
+ end
122
+
123
+ def store_titles
124
+ store_global_title
125
+ store_page_title
126
+ end
127
+
128
+ def store_global_title
129
+ store_translations title: "My Website"
130
+ end
131
+
132
+ def store_title_template
133
+ store_translations title_template: "d %{title} b"
134
+ end
135
+
136
+ end
@@ -0,0 +1,8 @@
1
+ require "bundler/setup"
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ require 'minitest/autorun'
6
+ require 'active_support/all'
7
+ require 'action_view'
8
+ require 'smart_titles'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_titles
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-03 00:00:00.000000000Z
12
+ date: 2011-12-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70107579604720 !ruby/object:Gem::Requirement
16
+ requirement: &70340769140560 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70107579604720
24
+ version_requirements: *70340769140560
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: actionpack
27
- requirement: &70107579603980 !ruby/object:Gem::Requirement
27
+ requirement: &70340769124440 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,32 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70107579603980
36
- - !ruby/object:Gem::Dependency
37
- name: rspec
38
- requirement: &70107579603080 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ! '>='
42
- - !ruby/object:Gem::Version
43
- version: '0'
44
- type: :development
45
- prerelease: false
46
- version_requirements: *70107579603080
35
+ version_requirements: *70340769124440
47
36
  - !ruby/object:Gem::Dependency
48
37
  name: rake
49
- requirement: &70107579602360 !ruby/object:Gem::Requirement
50
- none: false
51
- requirements:
52
- - - ! '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- type: :development
56
- prerelease: false
57
- version_requirements: *70107579602360
58
- - !ruby/object:Gem::Dependency
59
- name: mocha
60
- requirement: &70107579601760 !ruby/object:Gem::Requirement
38
+ requirement: &70340769123460 !ruby/object:Gem::Requirement
61
39
  none: false
62
40
  requirements:
63
41
  - - ! '>='
@@ -65,7 +43,7 @@ dependencies:
65
43
  version: '0'
66
44
  type: :development
67
45
  prerelease: false
68
- version_requirements: *70107579601760
46
+ version_requirements: *70340769123460
69
47
  description:
70
48
  email: sema@sema.in
71
49
  executables: []
@@ -77,6 +55,7 @@ files:
77
55
  - .rspec
78
56
  - Gemfile
79
57
  - Gemfile.lock
58
+ - Guardfile
80
59
  - LICENSE.txt
81
60
  - README.md
82
61
  - Rakefile
@@ -86,8 +65,8 @@ files:
86
65
  - lib/smart_titles/railtie.rb
87
66
  - lib/smart_titles/version.rb
88
67
  - smart_titles.gemspec
89
- - spec/smart_titles_helper_spec.rb
90
- - spec/spec_helper.rb
68
+ - test/smart_titles/helper_test.rb
69
+ - test/test_helper.rb
91
70
  homepage: http://github.com/semaperepelitsa/smart_titles
92
71
  licenses:
93
72
  - MIT
@@ -101,24 +80,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
80
  - - ! '>='
102
81
  - !ruby/object:Gem::Version
103
82
  version: '0'
104
- segments:
105
- - 0
106
- hash: -4104704374070627387
107
83
  required_rubygems_version: !ruby/object:Gem::Requirement
108
84
  none: false
109
85
  requirements:
110
86
  - - ! '>='
111
87
  - !ruby/object:Gem::Version
112
88
  version: '0'
113
- segments:
114
- - 0
115
- hash: -4104704374070627387
116
89
  requirements: []
117
90
  rubyforge_project:
118
- rubygems_version: 1.8.10
91
+ rubygems_version: 1.8.11
119
92
  signing_key:
120
93
  specification_version: 3
121
- summary: Really convenient way to set up page titles in Rails application.
94
+ summary: Really convenient way to set up page titles in a Rails application.
122
95
  test_files:
123
- - spec/smart_titles_helper_spec.rb
124
- - spec/spec_helper.rb
96
+ - test/smart_titles/helper_test.rb
97
+ - test/test_helper.rb
98
+ has_rdoc:
@@ -1,138 +0,0 @@
1
- require File.expand_path('spec_helper', File.dirname(__FILE__))
2
-
3
- describe SmartTitles::Helper do
4
- include ActionView::Helpers
5
-
6
- before do
7
- @virtual_path = 'posts/new'
8
- I18n.backend.reload!
9
- end
10
-
11
- def store_translations(*args)
12
- I18n.backend.store_translations(:en, *args)
13
- end
14
-
15
- describe 'no i18n' do
16
- describe '#title' do
17
- it "sets #page_title" do
18
- title("Hi")
19
- page_title.should == "Hi"
20
- end
21
-
22
- it "returns h1-wrapped title" do
23
- title("Hi").should == "<h1>Hi</h1>"
24
- end
25
- end
26
-
27
- describe '#page_title' do
28
- it "returns nil if no title was set" do
29
- page_title.should == nil
30
- end
31
- end
32
-
33
- describe '#head_title' do
34
- it 'returns #page_title if it was set' do
35
- title("Hi")
36
- head_title.should == "Hi"
37
- end
38
-
39
- it "returns missing translation if no title was set" do
40
- head_title.should include("translation missing: en.title")
41
- end
42
-
43
- it "returns title passed if no title was set" do
44
- head_title("Default").should == "Default"
45
- end
46
-
47
- describe 'with :title_template' do
48
- it "used for page_title" do
49
- store_translations title_template: "d %{title} b"
50
- title("New post")
51
- head_title.should == "d New post b"
52
- end
53
-
54
- it "is skipped for custom title" do
55
- store_translations title_template: "d %{title} b"
56
- head_title("Default").should == "Default"
57
- end
58
-
59
- it "is skipped if template: false" do
60
- store_translations title_template: "d %{title} b"
61
- title("New post")
62
- head_title("Default", template: false).should == "New post"
63
- end
64
- end
65
- end
66
- end
67
-
68
- describe 'i18n' do
69
- describe '#title' do
70
- it "returns .title wrapped in h1 tag if there are :title and .title" do
71
- store_translations title: "My Website", posts: { new: { title: "New post" } }
72
- title.should == "<h1>New post</h1>"
73
- end
74
-
75
- it "returns .title wrapped in h1 tag if there is only .title" do
76
- store_translations posts: { new: { title: "New post" } }
77
- title.should == "<h1>New post</h1>"
78
- end
79
-
80
- it "returns nothing if there is no .title" do
81
- store_translations title: "My Website"
82
- title.should == nil
83
- end
84
-
85
- it "returns nothing if there are no translations" do
86
- title.should == nil
87
- end
88
- end
89
-
90
- describe '#head_title' do
91
- it "returns .title if there are :title and .title" do
92
- store_translations title: "My Website", posts: { new: { title: "New post" } }
93
- head_title.should == "New post"
94
- end
95
-
96
- it "returns .title if there is only .title" do
97
- store_translations posts: { new: { title: "New post" } }
98
- head_title.should == "New post"
99
- end
100
-
101
- it "returns :title if there is no .title" do
102
- store_translations title: "My Website"
103
- head_title.should == "My Website"
104
- end
105
-
106
- it "returns missing translation if there are no tranlations" do
107
- head_title.should include("translation missing: en.title")
108
- end
109
-
110
- it "returns custom title" do
111
- store_translations title: "My Website"
112
- head_title("Custom").should == "Custom"
113
- end
114
-
115
- it "returns .title even if custom default title is passed" do
116
- store_translations posts: { new: { title: "New post" } }
117
- head_title("Custom").should == "New post"
118
- end
119
-
120
- describe 'with :title_template' do
121
- it "used for page_title" do
122
- store_translations title_template: "d %{title} b", posts: { new: { title: "New post" } }
123
- head_title.should == "d New post b"
124
- end
125
-
126
- it "is skipped for default title" do
127
- store_translations title: "My Website", title_template: "d %{title} b"
128
- head_title.should == "My Website"
129
- end
130
-
131
- it "is skipped if template: false" do
132
- store_translations title_template: "d %{title} b", posts: { new: { title: "New post" } }
133
- head_title(template: false).should == "New post"
134
- end
135
- end
136
- end
137
- end
138
- end
data/spec/spec_helper.rb DELETED
@@ -1,13 +0,0 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $LOAD_PATH.unshift(File.dirname(__FILE__))
3
- require 'active_support/all'
4
- require 'action_view'
5
- require 'smart_titles'
6
-
7
- # Requires supporting files with custom matchers and macros, etc,
8
- # in ./support/ and its subdirectories.
9
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
-
11
- RSpec.configure do |config|
12
- config.mock_with :mocha
13
- end