title 0.0.3 → 0.0.8
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.
- checksums.yaml +5 -5
- data/README.md +19 -0
- data/Rakefile +6 -0
- data/app/helpers/title/title_helper.rb +34 -4
- data/lib/title.rb +3 -0
- data/lib/title/version.rb +1 -1
- data/spec/helpers/title_helper_spec.rb +36 -7
- data/title.gemspec +2 -0
- metadata +44 -17
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: a5223d4bc45bb6bde8e268cb533e675a4a77628b85518151e10d1033f20ef341
|
|
4
|
+
data.tar.gz: 9fc368e00d9242fad23bba51657b34e46c54b436c9cc48a54de4abd8c304ad3a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f1ea8d9d9b5eb0cfc566a497e06d09e3bfcfa7cd8a617134c7da91bb84e67b3bf6c57bcdd1b2654a52f2dcb9eea34e7e8b358eee0cd7e26131ef5b8cf8b22052
|
|
7
|
+
data.tar.gz: 1546ca41e1d44809a45685d846f6de98c42aea2a510218e3aae726a3508fa71209c0fce39006d3cec5910940788aeafc886dcef79405532f0628c142c9b01f3f
|
data/README.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# Title
|
|
2
2
|
|
|
3
|
+
[](https://travis-ci.org/calebthompson/title)
|
|
4
|
+
[](https://codeclimate.com/github/calebthompson/title)
|
|
5
|
+
[](https://coveralls.io/r/calebthompson/title)
|
|
6
|
+
|
|
3
7
|
Translations for \<title\>s!
|
|
4
8
|
|
|
5
9
|
## Usage
|
|
@@ -9,6 +13,8 @@ Add to your translations:
|
|
|
9
13
|
```yaml
|
|
10
14
|
en:
|
|
11
15
|
titles:
|
|
16
|
+
# titles.application defaults to the sigficant portion of
|
|
17
|
+
# AppName::Application, which would be:
|
|
12
18
|
application: AppName
|
|
13
19
|
dashboards:
|
|
14
20
|
show: Dashboard
|
|
@@ -31,6 +37,19 @@ def to_s
|
|
|
31
37
|
end
|
|
32
38
|
```
|
|
33
39
|
|
|
40
|
+
You can pass additional values to the `#title` helper, which can be referenced
|
|
41
|
+
in your translations:
|
|
42
|
+
|
|
43
|
+
```erb
|
|
44
|
+
<title><%= title(user_name: current_user.name) %></title>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
```yaml
|
|
48
|
+
en:
|
|
49
|
+
titles:
|
|
50
|
+
application: '%{user_name} - AppName'
|
|
51
|
+
```
|
|
52
|
+
|
|
34
53
|
## Acknowledgement
|
|
35
54
|
|
|
36
55
|
Though the idea of translating titles was arrived at seperately, [Brandon
|
data/Rakefile
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
module Title
|
|
2
2
|
module TitleHelper
|
|
3
|
-
def title
|
|
4
|
-
|
|
3
|
+
def title(additional_context = {})
|
|
4
|
+
context = controller.view_assigns.merge(additional_context).symbolize_keys
|
|
5
|
+
PageTitle.new(controller_path, action_name, context).to_s
|
|
5
6
|
end
|
|
6
7
|
|
|
7
8
|
class PageTitle
|
|
8
9
|
def initialize(controller_path, action_name, context)
|
|
9
10
|
@controller_path = controller_path
|
|
10
|
-
@action_name = action_name
|
|
11
|
+
@action_name = adjusted_action_name(action_name)
|
|
11
12
|
@context = context
|
|
12
13
|
end
|
|
13
14
|
|
|
14
15
|
def to_s
|
|
15
16
|
I18n.t(
|
|
16
17
|
[:titles, controller_name, action_name].join('.'),
|
|
17
|
-
|
|
18
|
+
**safe_context.merge(default: defaults)
|
|
18
19
|
)
|
|
19
20
|
end
|
|
20
21
|
|
|
@@ -30,9 +31,38 @@ module Title
|
|
|
30
31
|
Rails.application.class.to_s.split('::').first
|
|
31
32
|
end
|
|
32
33
|
|
|
34
|
+
def defaults
|
|
35
|
+
default_keys_in_lookup_path + [application_title, guess_title]
|
|
36
|
+
end
|
|
37
|
+
|
|
33
38
|
def controller_name
|
|
34
39
|
controller_path.gsub('/', '.')
|
|
35
40
|
end
|
|
41
|
+
|
|
42
|
+
def default_keys_in_lookup_path
|
|
43
|
+
defaults = []
|
|
44
|
+
lookup_path = controller_name.split('.')
|
|
45
|
+
while lookup_path.length > 0
|
|
46
|
+
defaults << ['titles', *lookup_path, 'default'].join('.').to_sym
|
|
47
|
+
lookup_path.pop
|
|
48
|
+
end
|
|
49
|
+
defaults.reverse
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def adjusted_action_name(action_name)
|
|
53
|
+
case action_name
|
|
54
|
+
when 'create'
|
|
55
|
+
'new'
|
|
56
|
+
when 'update'
|
|
57
|
+
'edit'
|
|
58
|
+
else
|
|
59
|
+
action_name
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def safe_context
|
|
64
|
+
context.except(*I18n::RESERVED_KEYS)
|
|
65
|
+
end
|
|
36
66
|
end
|
|
37
67
|
end
|
|
38
68
|
end
|
data/lib/title.rb
CHANGED
data/lib/title/version.rb
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
require '
|
|
1
|
+
require 'coveralls'
|
|
2
|
+
Coveralls.wear!
|
|
3
|
+
|
|
2
4
|
require 'title'
|
|
3
5
|
|
|
4
6
|
describe Title::TitleHelper do
|
|
@@ -15,6 +17,14 @@ describe Title::TitleHelper do
|
|
|
15
17
|
expect(helper.title).to eq('Not Dummy')
|
|
16
18
|
end
|
|
17
19
|
|
|
20
|
+
it 'uses a :default key at an arbitrary point in the lookup path' do
|
|
21
|
+
stub_rails
|
|
22
|
+
stub_controller_and_action('engine/users', :show)
|
|
23
|
+
load_translations(engine: { default: 'Engine name' })
|
|
24
|
+
|
|
25
|
+
expect(helper.title).to eq 'Engine name'
|
|
26
|
+
end
|
|
27
|
+
|
|
18
28
|
it 'matches controller/action to translation and uses that title' do
|
|
19
29
|
stub_rails
|
|
20
30
|
stub_controller_and_action(:dashboards, :show)
|
|
@@ -27,20 +37,39 @@ describe Title::TitleHelper do
|
|
|
27
37
|
stub_rails
|
|
28
38
|
stub_controller_and_action(:users, :show)
|
|
29
39
|
load_translations(users: { show: '%{name}' })
|
|
30
|
-
helper.
|
|
40
|
+
allow(helper).to receive_message_chain(:controller, :view_assigns).and_return('name' => 'Caleb')
|
|
31
41
|
|
|
32
42
|
expect(helper.title).to eq('Caleb')
|
|
33
43
|
end
|
|
34
44
|
|
|
45
|
+
it 'can accept a hash of extra context in addition to the view assigns' do
|
|
46
|
+
stub_rails
|
|
47
|
+
stub_controller_and_action(:users, :show)
|
|
48
|
+
load_translations(users: { show: '%{greeting} %{name}' })
|
|
49
|
+
allow(helper).to receive_message_chain(:controller, :view_assigns).and_return('name' => 'Caleb')
|
|
50
|
+
|
|
51
|
+
expect(helper.title(greeting: 'Hello')).to eq('Hello Caleb')
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it 'makes context safe to be passed as interpolation options' do
|
|
55
|
+
stub_rails
|
|
56
|
+
stub_controller_and_action(:users, :show)
|
|
57
|
+
load_translations(users: { show: 'User' })
|
|
58
|
+
allow(helper).to receive_message_chain(:controller, :view_assigns).and_return('scope' => 'Foo')
|
|
59
|
+
|
|
60
|
+
expect(helper.title).to eq('User')
|
|
61
|
+
end
|
|
62
|
+
|
|
35
63
|
def stub_rails
|
|
36
|
-
helper.
|
|
37
|
-
helper.
|
|
38
|
-
helper.
|
|
39
|
-
Rails.
|
|
64
|
+
allow(helper).to receive(:controller_path).and_return('dashboards')
|
|
65
|
+
allow(helper).to receive(:action_name)
|
|
66
|
+
allow(helper).to receive_message_chain(:controller, :view_assigns).and_return({})
|
|
67
|
+
allow(Rails).to receive_message_chain(:application, :class).and_return('Dummy::Application')
|
|
40
68
|
end
|
|
41
69
|
|
|
42
70
|
def stub_controller_and_action(controller, action)
|
|
43
|
-
helper.
|
|
71
|
+
allow(helper).to receive(:controller_path).and_return(controller.to_s)
|
|
72
|
+
allow(helper).to receive(:action_name).and_return(action.to_s)
|
|
44
73
|
end
|
|
45
74
|
|
|
46
75
|
def helper
|
data/title.gemspec
CHANGED
|
@@ -21,6 +21,8 @@ Gem::Specification.new do |spec|
|
|
|
21
21
|
spec.add_development_dependency 'bundler', '~> 1.3'
|
|
22
22
|
spec.add_development_dependency 'rake'
|
|
23
23
|
spec.add_development_dependency 'rspec'
|
|
24
|
+
spec.add_development_dependency 'coveralls'
|
|
25
|
+
spec.add_development_dependency 'pry'
|
|
24
26
|
|
|
25
27
|
spec.add_dependency 'rails', '>= 3.1'
|
|
26
28
|
spec.add_dependency 'i18n'
|
metadata
CHANGED
|
@@ -1,83 +1,111 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: title
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.8
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Caleb Thompson
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2020-08-20 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
|
16
16
|
requirements:
|
|
17
|
-
- - ~>
|
|
17
|
+
- - "~>"
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
19
|
version: '1.3'
|
|
20
20
|
type: :development
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
|
-
- - ~>
|
|
24
|
+
- - "~>"
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
26
|
version: '1.3'
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: rake
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
|
31
|
-
- -
|
|
31
|
+
- - ">="
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
33
|
version: '0'
|
|
34
34
|
type: :development
|
|
35
35
|
prerelease: false
|
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
37
|
requirements:
|
|
38
|
-
- -
|
|
38
|
+
- - ">="
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
40
|
version: '0'
|
|
41
41
|
- !ruby/object:Gem::Dependency
|
|
42
42
|
name: rspec
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
44
44
|
requirements:
|
|
45
|
-
- -
|
|
45
|
+
- - ">="
|
|
46
46
|
- !ruby/object:Gem::Version
|
|
47
47
|
version: '0'
|
|
48
48
|
type: :development
|
|
49
49
|
prerelease: false
|
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
|
51
51
|
requirements:
|
|
52
|
-
- -
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: coveralls
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: pry
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
53
81
|
- !ruby/object:Gem::Version
|
|
54
82
|
version: '0'
|
|
55
83
|
- !ruby/object:Gem::Dependency
|
|
56
84
|
name: rails
|
|
57
85
|
requirement: !ruby/object:Gem::Requirement
|
|
58
86
|
requirements:
|
|
59
|
-
- -
|
|
87
|
+
- - ">="
|
|
60
88
|
- !ruby/object:Gem::Version
|
|
61
89
|
version: '3.1'
|
|
62
90
|
type: :runtime
|
|
63
91
|
prerelease: false
|
|
64
92
|
version_requirements: !ruby/object:Gem::Requirement
|
|
65
93
|
requirements:
|
|
66
|
-
- -
|
|
94
|
+
- - ">="
|
|
67
95
|
- !ruby/object:Gem::Version
|
|
68
96
|
version: '3.1'
|
|
69
97
|
- !ruby/object:Gem::Dependency
|
|
70
98
|
name: i18n
|
|
71
99
|
requirement: !ruby/object:Gem::Requirement
|
|
72
100
|
requirements:
|
|
73
|
-
- -
|
|
101
|
+
- - ">="
|
|
74
102
|
- !ruby/object:Gem::Version
|
|
75
103
|
version: '0'
|
|
76
104
|
type: :runtime
|
|
77
105
|
prerelease: false
|
|
78
106
|
version_requirements: !ruby/object:Gem::Requirement
|
|
79
107
|
requirements:
|
|
80
|
-
- -
|
|
108
|
+
- - ">="
|
|
81
109
|
- !ruby/object:Gem::Version
|
|
82
110
|
version: '0'
|
|
83
111
|
description: Abuses I18n to set HTML <title>s
|
|
@@ -87,7 +115,7 @@ executables: []
|
|
|
87
115
|
extensions: []
|
|
88
116
|
extra_rdoc_files: []
|
|
89
117
|
files:
|
|
90
|
-
- .gitignore
|
|
118
|
+
- ".gitignore"
|
|
91
119
|
- CONTRIBUTING.md
|
|
92
120
|
- Gemfile
|
|
93
121
|
- LICENSE.txt
|
|
@@ -110,17 +138,16 @@ require_paths:
|
|
|
110
138
|
- app
|
|
111
139
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
112
140
|
requirements:
|
|
113
|
-
- -
|
|
141
|
+
- - ">="
|
|
114
142
|
- !ruby/object:Gem::Version
|
|
115
143
|
version: '0'
|
|
116
144
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
145
|
requirements:
|
|
118
|
-
- -
|
|
146
|
+
- - ">="
|
|
119
147
|
- !ruby/object:Gem::Version
|
|
120
148
|
version: '0'
|
|
121
149
|
requirements: []
|
|
122
|
-
|
|
123
|
-
rubygems_version: 2.0.3
|
|
150
|
+
rubygems_version: 3.1.2
|
|
124
151
|
signing_key:
|
|
125
152
|
specification_version: 4
|
|
126
153
|
summary: I18n your <title>s
|