dub_thee 1.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 934690dd04c86383e636b7a32ae526de47acc1db
4
+ data.tar.gz: a4070ef08d619e323763a4b0c5f81a582467657c
5
+ SHA512:
6
+ metadata.gz: 61be7efc186a27a6cf2c3ee197f79bed02a3e9da671bc4a9973a3b3978c5e8d501337141e28baaaa4d23c5ba1672b07f8c5e3ebae285118f631b6b9573d1537f
7
+ data.tar.gz: e002fc8c0e57d3bba551ef4a46072a4b0763ad2a41be578386b393f4a11a5ac65d6198a1cfdc26af569757227b385f00d87a6eb8702e7bb6299f0980609a0a16
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,136 @@
1
+ # dub_thee
2
+
3
+ Rails page titles via I18n. For example, given the following
4
+ application layout:
5
+
6
+ ```erb
7
+ <!-- app/views/layouts/application.html.erb -->
8
+ <html>
9
+ <head>
10
+ <title><%= page_title %> | My Web Site</title>
11
+ </head>
12
+ ...
13
+ </html>
14
+ ```
15
+
16
+ And the following translations:
17
+
18
+ ```yaml
19
+ # config/locales/page_title.en.yml
20
+ en:
21
+ page_title:
22
+ home:
23
+ index: "Welcome"
24
+ ```
25
+
26
+ The "app/views/home/index.html.erb" view will automatically be titled
27
+ "Welcome | My Web Site".
28
+
29
+ The `page_title` helper fetches the title via `I18n.t`, using the
30
+ controller name and action name to make up the translation key. Any
31
+ action name is allowed, however "create", "update", and "delete" are
32
+ special-cased to instead fetch the titles of their render-on-failure
33
+ counterparts: "new", "edit", and "show", respectively.
34
+
35
+
36
+ ## Variable interpolation
37
+
38
+ For more dynamic titles, view assignment variables are made available
39
+ for interpolation. Additionally, the [i18n-interpolate_nested] gem
40
+ is included to enable attribute interpolation. For example, given the
41
+ following translations:
42
+
43
+ ```yaml
44
+ # config/locales/page_title.en.yml
45
+ en:
46
+ page_title:
47
+ products:
48
+ show: "%{product.name} (%{product.brand})"
49
+ ```
50
+
51
+ The value of `page_title` for "app/views/products/show.html.erb" will be
52
+ equivalent to `"#{@product[:name]} (#{@product[:brand]})"`.
53
+
54
+ [i18n-interpolate_nested]: https://rubygems.org/gems/i18n-interpolate_nested
55
+
56
+
57
+ ## Generic titles
58
+
59
+ Generic action titles (i.e. not controller-specific) are also supported.
60
+ To allow such titles to be slightly more dynamic, two additional values
61
+ are provided for interpolation: `plural` is the `String#titleize`'d name
62
+ of the controller, and `singular` is the `String#singularize`'d form of
63
+ `plural`. For example, given the following translations:
64
+
65
+ ```yaml
66
+ # config/locales/page_title.en.yml
67
+ en:
68
+ page_title:
69
+ index: "%{plural}"
70
+ new: "New %{singular}"
71
+ ```
72
+
73
+ The value of `page_title` for "app/views/part_orders/index.html.erb"
74
+ will be `"Part Orders"`, and for "app/views/locations/index.html.erb"
75
+ will be `"Locations"`. Likewise, the value of `page_title` for
76
+ "app/views/part_orders/new.html.erb" will be `"New Part Order"`, and for
77
+ "app/views/locations/new.html.erb" will be `"New Location"`.
78
+
79
+
80
+ ## Additional logic
81
+
82
+ Additional logic can be incorporated by using the `@page_title`
83
+ variable. If `@page_title` is set, the `page_title` helper will simply
84
+ return `@page_title`. Thus, individual views can implement more nuanced
85
+ title logic. For example, given the following translations:
86
+
87
+ ```yaml
88
+ # config/locales/page_title.en.yml
89
+ en:
90
+ page_title:
91
+ users:
92
+ show: "%{user.name}"
93
+ ```
94
+
95
+ And the following view:
96
+
97
+ ```erb
98
+ <!-- app/views/users/show.html.erb -->
99
+ <% @page_title = "Your Profile" if current_user == @user %>
100
+ ...
101
+ ```
102
+
103
+ The value of `page_title` in "app/views/layouts/application.html.erb"
104
+ will be `"Your Profile"` if the current logged-in user is viewing their
105
+ own profile. Else, it will be equivalent to `"#{@user[:name]}"`.
106
+
107
+
108
+ ## Installation
109
+
110
+ Add this line to your application's Gemfile:
111
+
112
+ ```ruby
113
+ gem "dub_thee"
114
+ ```
115
+
116
+ Then execute:
117
+
118
+ ```bash
119
+ $ bundle install
120
+ ```
121
+
122
+ And finally, run the install generator:
123
+
124
+ ```bash
125
+ $ rails generate dub_thee:install
126
+ ```
127
+
128
+
129
+ ## Contributing
130
+
131
+ Run `rake test` to run the tests.
132
+
133
+
134
+ ## License
135
+
136
+ [MIT License](https://opensource.org/licenses/MIT)
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'yard'
8
+
9
+ YARD::Rake::YardocTask.new(:doc) do |t|
10
+ end
11
+
12
+
13
+ require 'bundler/gem_tasks'
14
+
15
+ require 'rake/testtask'
16
+
17
+ Rake::TestTask.new(:test) do |t|
18
+ t.libs << 'test'
19
+ t.pattern = 'test/**/*_test.rb'
20
+ t.verbose = false
21
+ end
22
+
23
+ task default: :test
@@ -0,0 +1,8 @@
1
+ module DubThee
2
+ # @!visibility private
3
+ class Railtie < ::Rails::Railtie
4
+ initializer "dub_thee" do
5
+ ActionView::Base.send(:include, DubThee::PageTitleHelper)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module DubThee
2
+ VERSION = "1.0.0"
3
+ end
data/lib/dub_thee.rb ADDED
@@ -0,0 +1,51 @@
1
+ require "i18n/interpolate_nested"
2
+ require "dub_thee/version"
3
+ require "dub_thee/railtie"
4
+
5
+
6
+ module DubThee::PageTitleHelper
7
+
8
+ # @!visibility private
9
+ ACTION_TRANSLATION_PARTS = {
10
+ "create" => "new",
11
+ "update" => "edit",
12
+ "destroy" => "show",
13
+ }
14
+
15
+ # @!visibility private
16
+ I18N_RESERVED_OPTIONS = I18n::RESERVED_KEYS.index_by(&:itself)
17
+
18
+ # Fetches the current page's title via +I18n.t+. The current
19
+ # controller and action are combined to form the translation key.
20
+ # Specifically, the +"page_title.#{controller_name}.#{action_name}"+
21
+ # and +"page_title.#{action_name}"+ keys are tried. For namespaced
22
+ # controllers, the namespace is included as part of +controller_name+
23
+ # (dot-separated).
24
+ #
25
+ # I18n string interpolation is supported using the view's assignment
26
+ # variables. Additionally, the +titleize+'d controller name in both
27
+ # singular and plural form is available via the interpolation keys
28
+ # +singular+ and +plural+, respectively.
29
+ #
30
+ # This method is memoized to +@page_title+, and can be invoked
31
+ # multiple times without additional cost. This also allows views to
32
+ # manipulate the page title outside of I18n, if necessary, by
33
+ # assigning to the +@page_title+ variable.
34
+ #
35
+ # @return [String]
36
+ def page_title
37
+ return @page_title if defined?(@page_title)
38
+
39
+ controller_part = controller_path.tr("/", I18n.default_separator)
40
+ action_part = ACTION_TRANSLATION_PARTS[action_name] || action_name
41
+
42
+ options = controller.view_assigns.symbolize_keys
43
+ options.reject!{|key| I18N_RESERVED_OPTIONS[key] }
44
+ options[:plural] = controller_name.titleize
45
+ options[:singular] = options[:plural].singularize
46
+ options[:default] = :"page_title.#{action_part}"
47
+
48
+ @page_title = I18n.t("page_title.#{controller_part}.#{action_part}", options)
49
+ end
50
+
51
+ end
@@ -0,0 +1,14 @@
1
+ require "rails/generators/base"
2
+
3
+ module DubThee
4
+ # @!visibility private
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ source_root File.join(__dir__, "templates")
8
+
9
+ def copy_locales
10
+ copy_file "config/locales/page_title.en.yml"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ en:
2
+ page_title:
3
+ index: "%{plural}"
4
+ show: "%{singular}"
5
+ new: "New %{singular}"
6
+ edit: "Edit %{singular}"
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dub_thee
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Hefner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: i18n-interpolate_nested
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
69
+ description:
70
+ email:
71
+ - jonathan.hefner@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - lib/dub_thee.rb
80
+ - lib/dub_thee/railtie.rb
81
+ - lib/dub_thee/version.rb
82
+ - lib/generators/dub_thee/install/install_generator.rb
83
+ - lib/generators/dub_thee/install/templates/config/locales/page_title.en.yml
84
+ homepage: https://github.com/jonathanhefner/dub_thee
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.5.2.1
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Rails page titles via I18n
108
+ test_files: []