titler 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/README.md +162 -0
- data/Rakefile +6 -0
- data/app/helpers/titler/titler_helper.rb +119 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/config/initializers/titler.rb +8 -0
- data/config/locales/titler.en.yml +6 -0
- data/lib/generators/titler/install/install_generator.rb +14 -0
- data/lib/generators/titler/install/templates/titler.rb +8 -0
- data/lib/generators/titler/locales/locales_generator.rb +29 -0
- data/lib/titler/configuration.rb +37 -0
- data/lib/titler/engine.rb +3 -0
- data/lib/titler/version.rb +3 -0
- data/lib/titler.rb +8 -0
- data/spec/configure_spec.rb +30 -0
- data/spec/helpers/titler_helper_spec.rb +172 -0
- data/spec/spec_helper.rb +15 -0
- data/titler-0.0.1.gem +0 -0
- data/titler.gemspec +53 -0
- metadata +273 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ec5bde8aacb1e9937df0ba2fcd4d019c3ed41aac
|
4
|
+
data.tar.gz: 46707d2984b2abf23db5de83afdff6c159b33e89
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2da0b72621220286557b51ccf3b1336a03ac8ba5acb458a7cfde788fd8aa602b8326e1a75281899ee4499e94b50f4bbdbb5dcd5bf5a70571134ac596f1b8ed3f
|
7
|
+
data.tar.gz: 1a4cd1f97192812aa171a8b0d9fd5146f40507a5f74dbfde90274734dd73f707979a91874dc71970b4abb97b04059ae753ab74c0ffc8ba97e75b062a074599f6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
titler
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.3.1
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
# ![titler-icon](https://user-images.githubusercontent.com/1078402/27513069-a1b89f32-591e-11e7-8e82-07f991b07b31.png) Titler
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/roberttravispierce/titler.svg?branch=master)](https://travis-ci.org/roberttravispierce/titler)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/roberttravispierce/titler/badges/gpa.svg)](https://codeclimate.com/github/roberttravispierce/titler)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/github/roberttravispierce/titler/badge.svg?branch=master)](https://coveralls.io/github/roberttravispierce/titler?branch=master)
|
6
|
+
|
7
|
+
![Titler is not quite ready for use yet; in case you stumbled across this somehow. Coming soon! —RTP](http://messages.hellobits.com/warning.svg?message=Titler%20is%20not%20quite%20ready%20for%20use%20yet%3B%20in%20case%20you%20stumbled%20across%20this%20somehow.%20Coming%20Soon!%20%E2%80%94RTP)
|
8
|
+
|
9
|
+
1. [What is it?](#what-is-it?)
|
10
|
+
2. [How do I get up and running?](#how-do-i-get-up-and-running?)
|
11
|
+
3. [How do I use it?](#how-do-i-use-it?)
|
12
|
+
4. [So why the fuss about titles anyway?](#so-why-the-fuss-about-titles-anyway?)
|
13
|
+
5. [Who built this?](#who-built-this?)
|
14
|
+
6. [How can I help improve it?](#how-can-i-help-improve-it?)
|
15
|
+
|
16
|
+
## What is it?
|
17
|
+
|
18
|
+
Titler is a ruby gem that automaticaly gives you useful and consistent page titles for your Rails application.
|
19
|
+
|
20
|
+
![titler-string-elements](https://user-images.githubusercontent.com/1078402/27513269-9194340c-5925-11e7-8e7c-726a2d45e041.png)
|
21
|
+
|
22
|
+
There are lots of options and configurations available, all with sensible fallbacks and defaults, including reading from your i18n files. See the [How do I use it?](#how-do-i-use-it?) section for more information.
|
23
|
+
|
24
|
+
## How do I get up and running?
|
25
|
+
|
26
|
+
1. Install the titler gem to your Gemfile and install it:
|
27
|
+
```ruby
|
28
|
+
gem 'titler'
|
29
|
+
```
|
30
|
+
```console
|
31
|
+
$ bundle install
|
32
|
+
```
|
33
|
+
|
34
|
+
2. {Not Yet Implemented} Run the generator. This will create an initializer with config values and an i18n file:
|
35
|
+
```console
|
36
|
+
$ rails generate titler:install
|
37
|
+
```
|
38
|
+
|
39
|
+
3. Add the page_title and set_page_title helper methods to your application helper:
|
40
|
+
|
41
|
+
*app/helpers/application_helper.rb*
|
42
|
+
```ruby
|
43
|
+
module ApplicationHelper
|
44
|
+
|
45
|
+
def page_title(page_title)
|
46
|
+
content_for(:page_title) {page_title}
|
47
|
+
end
|
48
|
+
|
49
|
+
def set_page_title
|
50
|
+
Titler::Title.new(controller: self, i18n: I18n, title_as_set: content_for(:page_title) || @page_title).title
|
51
|
+
end
|
52
|
+
...
|
53
|
+
```
|
54
|
+
|
55
|
+
4. Change your application layout title tag to:
|
56
|
+
|
57
|
+
*app/views/layouts/application.html.erb*
|
58
|
+
```html
|
59
|
+
<title><%= set_page_title %></title>
|
60
|
+
```
|
61
|
+
|
62
|
+
5. Set specific page titles as desired in your controllers or views:
|
63
|
+
```ruby
|
64
|
+
# Example of setting the title in a controller method:
|
65
|
+
helpers.page_title "Legal and Privacy Notices"
|
66
|
+
```
|
67
|
+
```ruby
|
68
|
+
# Example of setting the title in a view (haml version):
|
69
|
+
= page_title "Legal and Privacy Notices"
|
70
|
+
```
|
71
|
+
|
72
|
+
This will get you basic, consistent page titles. You can further customize universal and specific page title behavior. See the [How do I use it?](#usage) section.
|
73
|
+
|
74
|
+
## How do I use it?
|
75
|
+
|
76
|
+
![titler-string-elements](https://user-images.githubusercontent.com/1078402/27513269-9194340c-5925-11e7-8e7c-726a2d45e041.png)
|
77
|
+
|
78
|
+
With Titler, a page title consists of the following elements:
|
79
|
+
|
80
|
+
### Environment Prefix
|
81
|
+
(env_prefix)
|
82
|
+
- A one letter prefix in parentheses showing the rails environment. Example "(D) Title here..." for Development environment, or "(S) Title here..." for Staging environment. **This aids in quickly scanning and locating browser tabs during development and testing**. It is omitted for production environments.
|
83
|
+
|
84
|
+
### Admin Namespace
|
85
|
+
(admin_namespace)
|
86
|
+
- The title will be prefixed with "Admin" (admin_namespace i18n value) if the page controller is within an Admin namespace.
|
87
|
+
|
88
|
+
### Element Delimiter
|
89
|
+
(delimiter)
|
90
|
+
- Elements within the built title string will be delimited by this string. Developer can set it in the i18n file. The default is " - "
|
91
|
+
|
92
|
+
### Title Body
|
93
|
+
(title_body)
|
94
|
+
|
95
|
+
This is the core of the individual page title, which is set by the developer throughout their app. In order of preference:
|
96
|
+
|
97
|
+
- content_for :page_title if found
|
98
|
+
- @page_title instance variable if found
|
99
|
+
- The Controller and Action (method) name is used if none of the above are found
|
100
|
+
|
101
|
+
### App Tagline
|
102
|
+
(app_tagline)
|
103
|
+
|
104
|
+
This allows for an additional marketing tagline to be in every title. Set in the "app_tagline" i18n value and ignored if not found.
|
105
|
+
|
106
|
+
### App Name
|
107
|
+
(app_name)
|
108
|
+
|
109
|
+
- The built title string is appended with the name of the application. This is set in the "app_name" i18n value. Default fallback is the Rails.application.class name.
|
110
|
+
|
111
|
+
### _Examples_
|
112
|
+
|
113
|
+
- (D) Privacy Policy | Mom App
|
114
|
+
- About - Best App for Busy Moms - Mom App
|
115
|
+
- (S) Admin - User Profile - Mom App
|
116
|
+
- Recipe of the Week / Best App for Busy Mom's / Mom App
|
117
|
+
- Mom App | Posts Index
|
118
|
+
|
119
|
+
|
120
|
+
### Demonstration App with Live Versions
|
121
|
+
|
122
|
+
{{Insert screenshot of app}}
|
123
|
+
|
124
|
+
There is a [titler_demo](https://github.com/roberttravispierce/titler_demo) demonstration app that shows how the titler gem works in a Rails app in many of the different combinations & situations. You can view live versions of it here:
|
125
|
+
|
126
|
+
- In a Staging Environment: https://titler-demo-staging.herokuapp.com/
|
127
|
+
- In a Production Environment: https://titler-demo-production.herokuapp.com/
|
128
|
+
|
129
|
+
## So why the fuss about titles anyway?
|
130
|
+
|
131
|
+
Here are some resources to learn more about the importance of well-crafted and consistent page titles:
|
132
|
+
|
133
|
+
-
|
134
|
+
|
135
|
+
## Who built this?
|
136
|
+
|
137
|
+
Titler was conceived and developed by Robert Travis Pierce ([roberttravispierce](https://github.com/roberttravispierce)), to scratch his own itch and further his education in Ruby and Rails.
|
138
|
+
|
139
|
+
Invaluable guidance and encouragment was provided along the way by Jonathan Allured ([jonallured](https://github.com/jonallured)). Thank you Jon, for agreeing to be a mentor and give back to the community.
|
140
|
+
|
141
|
+
In addition, the [title gem](https://github.com/calebthompson/title) by [calebthompson](https://github.com/calebthompson), helped greatly with figuring out how to structure the gem as a rails engine, and served as a guide for several improvements.
|
142
|
+
|
143
|
+
## How can I help improve it?
|
144
|
+
|
145
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/roberttravispierce/titler.
|
146
|
+
|
147
|
+
Here's the most direct way to get your work merged into the project.
|
148
|
+
|
149
|
+
- Fork the project
|
150
|
+
- Clone down your fork
|
151
|
+
- Create a feature branch
|
152
|
+
- Hack away and add tests, not necessarily in that order
|
153
|
+
- Make sure everything still passes by running tests
|
154
|
+
- If necessary, rebase your commits into logical chunks without errors
|
155
|
+
- Push the branch up to your fork
|
156
|
+
- Send a pull request for your branch
|
157
|
+
|
158
|
+
### Developing locally
|
159
|
+
|
160
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
161
|
+
|
162
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
data/Rakefile
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
module Titler
|
2
|
+
module TitlerHelper
|
3
|
+
def page_title(page_title)
|
4
|
+
content_for(:page_title) {page_title}
|
5
|
+
end
|
6
|
+
|
7
|
+
def titler(custom_vars = {})
|
8
|
+
context = controller.view_assigns.merge(custom_vars).symbolize_keys
|
9
|
+
Title.new(
|
10
|
+
controller: controller,
|
11
|
+
i18n: I18n,
|
12
|
+
title_as_set: content_for(:page_title) || @page_title,
|
13
|
+
context: context
|
14
|
+
).to_s
|
15
|
+
end
|
16
|
+
|
17
|
+
class Title
|
18
|
+
def initialize(controller:, i18n:, title_as_set:, context:)
|
19
|
+
@controller = controller
|
20
|
+
@i18n = i18n
|
21
|
+
@title_as_set = title_as_set
|
22
|
+
@context = context
|
23
|
+
@configuration = Configuration.new
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_s
|
27
|
+
th = title_hash = Hash.new
|
28
|
+
th[:env_prefix] = env_prefix
|
29
|
+
th[:admin_namespace] = admin_namespace
|
30
|
+
th[:title_body] = title_body
|
31
|
+
th[:app_name] = app_name
|
32
|
+
th[:app_tagline] = app_tagline
|
33
|
+
build_title_str(th)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def env_prefix
|
39
|
+
if @configuration.use_env_prefix
|
40
|
+
Rails.env.production? ? '' : "(#{Rails.env[0,1].upcase}) "
|
41
|
+
else
|
42
|
+
''
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def admin_namespace
|
47
|
+
admin_namespace? ? admin_default_name + delimiter : ''
|
48
|
+
end
|
49
|
+
|
50
|
+
def admin_namespace?
|
51
|
+
@controller.class.ancestors.include?(@configuration.admin_controller)
|
52
|
+
end
|
53
|
+
|
54
|
+
def admin_default_name
|
55
|
+
@configuration.admin_name
|
56
|
+
end
|
57
|
+
|
58
|
+
def delimiter
|
59
|
+
@i18n.exists?('titler.delimiter') ? @i18n.t('titler.delimiter') : @configuration.delimiter
|
60
|
+
end
|
61
|
+
|
62
|
+
def app_name
|
63
|
+
name = @i18n.exists?('titler.app_name') ? @i18n.t('titler.app_name') : Rails.application.class.to_s.split("::").first
|
64
|
+
end
|
65
|
+
|
66
|
+
def app_tagline
|
67
|
+
tagline = @i18n.exists?('titler.app_tagline') ? @i18n.t('titler.app_tagline') : ''
|
68
|
+
end
|
69
|
+
|
70
|
+
def title_body
|
71
|
+
if @title_as_set.present?
|
72
|
+
@title_as_set
|
73
|
+
else
|
74
|
+
@controller.controller_name.titleize + ' ' + @controller.action_name.titleize rescue ''
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def build_title_str(th)
|
79
|
+
case @configuration.app_name_position
|
80
|
+
when 'append'
|
81
|
+
th[:env_prefix] + th[:admin_namespace] + th[:title_body] + app_tagline_str(th) + app_name_str(th)
|
82
|
+
when 'prepend'
|
83
|
+
th[:env_prefix] + app_name_str(th) + app_tagline_str(th) + th[:admin_namespace] + th[:title_body]
|
84
|
+
else
|
85
|
+
th[:env_prefix] + th[:admin_namespace] + th[:title_body] + app_tagline_str(th)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def app_name_str(th)
|
90
|
+
if title_body.blank?
|
91
|
+
th[:app_name]
|
92
|
+
else
|
93
|
+
case @configuration.app_name_position
|
94
|
+
when 'append'
|
95
|
+
delimiter + th[:app_name]
|
96
|
+
when 'prepend'
|
97
|
+
th[:app_name] + delimiter
|
98
|
+
else
|
99
|
+
''
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def app_tagline_str(th)
|
105
|
+
tagline = th[:app_tagline]
|
106
|
+
if tagline.blank? || !@configuration.use_app_tagline
|
107
|
+
''
|
108
|
+
else
|
109
|
+
case @configuration.app_name_position
|
110
|
+
when 'append'
|
111
|
+
delimiter + tagline
|
112
|
+
when 'prepend'
|
113
|
+
tagline + delimiter
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "titler"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
|
3
|
+
module Titler
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
|
8
|
+
def create_titler_initializer
|
9
|
+
copy_file 'titler.rb', 'config/initializers/titler.rb'
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
|
3
|
+
module Titler
|
4
|
+
module Generators
|
5
|
+
class LocalesGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path("../../../../..", __FILE__)
|
7
|
+
|
8
|
+
def create_locales
|
9
|
+
locales.each do |locale|
|
10
|
+
copy_file locale
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def locales
|
17
|
+
files_within_root('.', 'config/locales/**/*.*')
|
18
|
+
end
|
19
|
+
|
20
|
+
def files_within_root(prefix, glob)
|
21
|
+
root = "#{self.class.source_root}/#{prefix}"
|
22
|
+
|
23
|
+
Dir["#{root}/#{glob}"].sort.map do |full_path|
|
24
|
+
full_path.sub(root, '.').gsub('/./', '/')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class AdminController; end
|
2
|
+
|
3
|
+
module Titler
|
4
|
+
class Configuration
|
5
|
+
attr_accessor :admin_name,
|
6
|
+
:admin_controller,
|
7
|
+
:app_name_position,
|
8
|
+
:use_env_prefix,
|
9
|
+
:use_app_tagline,
|
10
|
+
:delimiter
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@admin_name = 'Admin'
|
14
|
+
@admin_controller = AdminController
|
15
|
+
@app_name_position = 'append' # append, prepend, none
|
16
|
+
@use_env_prefix = true
|
17
|
+
@use_app_tagline = true
|
18
|
+
@delimiter = ' - ' #Note: (Eventually) a 'titler.delimiter' i18n value will override this
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class << self
|
23
|
+
attr_accessor :configuration
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.configuration
|
27
|
+
@configuration ||= Configuration.new
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.reset
|
31
|
+
@configuration = Configuration.new
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.configure
|
35
|
+
yield(configuration)
|
36
|
+
end
|
37
|
+
end
|
data/lib/titler.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'titler'
|
3
|
+
|
4
|
+
module Titler
|
5
|
+
describe Configuration do
|
6
|
+
|
7
|
+
context 'with configuration block' do
|
8
|
+
it 'returns the use_env_prefix' do
|
9
|
+
expect(Titler.configuration.use_env_prefix).to be true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#configure" do
|
14
|
+
it "sets use_env_prefix to true" do
|
15
|
+
config = Configuration.new
|
16
|
+
expect(config.use_env_prefix).to be true
|
17
|
+
end
|
18
|
+
|
19
|
+
it "can set use_env_prefix to false" do
|
20
|
+
config = Configuration.new
|
21
|
+
allow(config).to receive(:use_env_prefix).and_return(false)
|
22
|
+
expect(config.use_env_prefix).to be false
|
23
|
+
end
|
24
|
+
|
25
|
+
after :each do
|
26
|
+
Titler.reset
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,172 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'titler'
|
3
|
+
|
4
|
+
describe Titler::TitlerHelper do
|
5
|
+
|
6
|
+
# Defaults Only Context ----------------------------------------------------
|
7
|
+
context 'when no title info is set' do
|
8
|
+
it '(1) uses defaults' do
|
9
|
+
controller = MockController.new
|
10
|
+
stub_rails(controller, 'production', nil)
|
11
|
+
load_translations({})
|
12
|
+
|
13
|
+
expected_title = "#{env_prefix}#{controller.controller_name.titleize} #{controller.action_name.titleize} - #{app_name}"
|
14
|
+
expect(titler_helper.titler).to eq(expected_title)
|
15
|
+
end
|
16
|
+
|
17
|
+
it '(2) prefixes defaults in development' do
|
18
|
+
controller = MockController.new
|
19
|
+
stub_rails(controller, 'development', nil)
|
20
|
+
load_translations({})
|
21
|
+
|
22
|
+
expected_title = "#{env_prefix}#{controller.controller_name.titleize} #{controller.action_name.titleize} - #{app_name}"
|
23
|
+
expect(titler_helper.titler).to eq(expected_title)
|
24
|
+
end
|
25
|
+
|
26
|
+
it '(3) adds admin namespace to defaults' do
|
27
|
+
controller = MockAdminController.new
|
28
|
+
stub_rails(controller, 'production', nil)
|
29
|
+
load_translations({})
|
30
|
+
|
31
|
+
expected_title = "#{env_prefix}Admin - #{controller.controller_name.titleize} #{controller.action_name.titleize} - #{app_name}"
|
32
|
+
expect(titler_helper.titler).to eq(expected_title)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# i18n Values Context ----------------------------------------------------
|
37
|
+
context 'when titler i18n values exist' do
|
38
|
+
it '(1) adds delimiter to defaults' do
|
39
|
+
controller = MockController.new
|
40
|
+
stub_rails(controller, 'production', nil)
|
41
|
+
load_translations({ delimiter: ' | ' })
|
42
|
+
|
43
|
+
expected_title = "#{env_prefix}#{controller.controller_name.titleize} #{controller.action_name.titleize} | #{app_name}"
|
44
|
+
expect(titler_helper.titler).to eq(expected_title)
|
45
|
+
end
|
46
|
+
|
47
|
+
it '(2) adds app name to defaults' do
|
48
|
+
controller = MockController.new
|
49
|
+
stub_rails(controller, 'production', nil)
|
50
|
+
load_translations({ app_name: 'Test App' })
|
51
|
+
|
52
|
+
expected_title = "#{env_prefix}#{controller.controller_name.titleize} #{controller.action_name.titleize} - Test App"
|
53
|
+
expect(titler_helper.titler).to eq(expected_title)
|
54
|
+
end
|
55
|
+
|
56
|
+
it '(3) adds tagline to defaults' do
|
57
|
+
controller = MockController.new
|
58
|
+
stub_rails(controller, 'production', nil)
|
59
|
+
load_translations({ app_tagline: 'All the News' })
|
60
|
+
|
61
|
+
expected_title = "#{env_prefix}#{controller.controller_name.titleize} #{controller.action_name.titleize} - All the News - #{app_name}"
|
62
|
+
expect(titler_helper.titler).to eq(expected_title)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Specific Page Title Context ----------------------------------------------------
|
67
|
+
context 'when specific page title exists' do
|
68
|
+
# it '(1) uses content_for when present' do
|
69
|
+
# controller = MockController.new
|
70
|
+
# stub_rails(controller, 'production', nil)
|
71
|
+
# #TODO setting the content_for is not working for some reason
|
72
|
+
# titler_helper.content_for(:page_title, 'Test Page')
|
73
|
+
# expected_title = "#{env_prefix}Test Page - #{app_name}"
|
74
|
+
# expect(titler_helper.titler).to eq(expected_title)
|
75
|
+
# end
|
76
|
+
|
77
|
+
it '(2) uses page_title var when present' do
|
78
|
+
controller = MockController.new
|
79
|
+
stub_rails(controller, 'production', nil)
|
80
|
+
titler_helper.instance_variable_set(:@page_title, 'Var Test Page')
|
81
|
+
expected_title = "#{env_prefix}Var Test Page - #{app_name}"
|
82
|
+
expect(titler_helper.titler).to eq(expected_title)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Configuration Values Context ----------------------------------------------------
|
87
|
+
context 'when titler configuration values exist' do
|
88
|
+
end
|
89
|
+
|
90
|
+
def stub_rails(controller, env_str, content_for)
|
91
|
+
allow(Rails).to receive_message_chain(:application, :class).and_return('TitlerTest::Application')
|
92
|
+
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new(env_str))
|
93
|
+
allow(Rails).to receive(:controller).and_return(controller)
|
94
|
+
|
95
|
+
allow(titler_helper).to receive_message_chain(:content_for).and_return(content_for)
|
96
|
+
allow(titler_helper).to receive_message_chain(:controller, :view_assigns).and_return({})
|
97
|
+
allow(titler_helper).to receive_message_chain(:controller).and_return(controller)
|
98
|
+
end
|
99
|
+
|
100
|
+
def app_name
|
101
|
+
Rails.application.class.to_s.split("::").first
|
102
|
+
end
|
103
|
+
|
104
|
+
def load_translations(titles)
|
105
|
+
I18n.backend.reload!
|
106
|
+
I18n.backend.store_translations(:en, { titler: titles })
|
107
|
+
end
|
108
|
+
|
109
|
+
# TODO: Can this be right? I'm using what is essentially a private method from inside titler to
|
110
|
+
# set up tests for titler. Just for the sake of saving some lines of code.
|
111
|
+
def env_prefix
|
112
|
+
Rails.env.production? ? '' : "(#{Rails.env[0,1].upcase}) "
|
113
|
+
end
|
114
|
+
|
115
|
+
# TODO: I think there's a way to do away with this stub style and use something like:
|
116
|
+
# allow(helper).to receive(:controller).and_return('MockController') but I'm not sure.
|
117
|
+
class MockController
|
118
|
+
def controller_name
|
119
|
+
"mock"
|
120
|
+
end
|
121
|
+
|
122
|
+
def action_name
|
123
|
+
"action"
|
124
|
+
end
|
125
|
+
|
126
|
+
def view_assigns
|
127
|
+
{}
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
class AdminController; end
|
132
|
+
class MockAdminController < AdminController
|
133
|
+
def controller_name
|
134
|
+
"mock_admin"
|
135
|
+
end
|
136
|
+
|
137
|
+
def action_name
|
138
|
+
"action"
|
139
|
+
end
|
140
|
+
|
141
|
+
def view_assigns
|
142
|
+
{}
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
class MyAdminController < AdminController
|
147
|
+
def controller_name
|
148
|
+
"my_admin"
|
149
|
+
end
|
150
|
+
|
151
|
+
def action_name
|
152
|
+
"action"
|
153
|
+
end
|
154
|
+
|
155
|
+
def view_assigns
|
156
|
+
{}
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def titler_helper
|
161
|
+
@titler_helper ||= Class.new { include Titler::TitlerHelper }.new
|
162
|
+
end
|
163
|
+
|
164
|
+
# def helper
|
165
|
+
# Helper.new
|
166
|
+
# end
|
167
|
+
#
|
168
|
+
# class Helper
|
169
|
+
# include Singleton
|
170
|
+
# include ActionView::Helpers::CaptureHelper
|
171
|
+
# end
|
172
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
require 'pry'
|
3
|
+
require "titler"
|
4
|
+
require 'rails'
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
# Enable flags like --only-failures and --next-failure
|
8
|
+
config.example_status_persistence_file_path = ".rspec_status"
|
9
|
+
|
10
|
+
config.after(:each) { I18n.locale = :en }
|
11
|
+
|
12
|
+
config.expect_with :rspec do |c|
|
13
|
+
c.syntax = :expect
|
14
|
+
end
|
15
|
+
end
|
data/titler-0.0.1.gem
ADDED
Binary file
|
data/titler.gemspec
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'titler/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "titler"
|
8
|
+
spec.version = Titler::VERSION
|
9
|
+
spec.authors = ["Robert Travis Pierce", "Jonathan Allured"]
|
10
|
+
spec.email = ["robert@roberttravispierce.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A gem that make titling your Rails app pages dead simple but still allows you flexibility and control.}
|
13
|
+
spec.description = %q{Titler is a ruby gem that make titling your Rails app pages dead simple but still allows you flexibility and control. Set specific page titles in the controller or view with fallbacks for all situations. You can add environment and namespace prefixes (e.g. "Admin | <Title of page>") automatically.}
|
14
|
+
spec.homepage = "https://github.com/roberttravispierce/titler"
|
15
|
+
spec.license = "MIT"
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.test_files = spec.files.grep(/\Aspec\//)
|
18
|
+
spec.require_path = "lib"
|
19
|
+
|
20
|
+
spec.required_ruby_version = "~> 2.0"
|
21
|
+
|
22
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
23
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
24
|
+
# if spec.respond_to?(:metadata)
|
25
|
+
# spec.metadata['allowed_push_host'] = 'http://mygemserver.com'
|
26
|
+
# else
|
27
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
28
|
+
# "public gem pushes."
|
29
|
+
# end
|
30
|
+
|
31
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
32
|
+
f.match(%r{^(test|spec|features)/})
|
33
|
+
end
|
34
|
+
spec.bindir = "exe"
|
35
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
36
|
+
spec.require_paths = ["lib"]
|
37
|
+
|
38
|
+
spec.add_development_dependency 'rails', '~> 5.0.3'
|
39
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
40
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
41
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
42
|
+
spec.add_development_dependency "rspec-nc"
|
43
|
+
spec.add_development_dependency "guard"
|
44
|
+
spec.add_development_dependency "guard-rspec"
|
45
|
+
spec.add_development_dependency "pry"
|
46
|
+
spec.add_development_dependency "pry-remote"
|
47
|
+
spec.add_development_dependency "pry-nav"
|
48
|
+
spec.add_development_dependency 'rb-readline'
|
49
|
+
spec.add_development_dependency 'rspec-rails'
|
50
|
+
|
51
|
+
spec.add_dependency 'rails', '>= 3.1'
|
52
|
+
spec.add_dependency 'i18n'
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,273 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: titler
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Travis Pierce
|
8
|
+
- Jonathan Allured
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2017-07-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 5.0.3
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 5.0.3
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '1.14'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.14'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '10.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '10.0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rspec
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '3.0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '3.0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rspec-nc
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: guard
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: guard-rspec
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
name: pry
|
114
|
+
requirement: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
type: :development
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: pry-remote
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
type: :development
|
134
|
+
prerelease: false
|
135
|
+
version_requirements: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
- !ruby/object:Gem::Dependency
|
141
|
+
name: pry-nav
|
142
|
+
requirement: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
type: :development
|
148
|
+
prerelease: false
|
149
|
+
version_requirements: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - ">="
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
- !ruby/object:Gem::Dependency
|
155
|
+
name: rb-readline
|
156
|
+
requirement: !ruby/object:Gem::Requirement
|
157
|
+
requirements:
|
158
|
+
- - ">="
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: '0'
|
161
|
+
type: :development
|
162
|
+
prerelease: false
|
163
|
+
version_requirements: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: '0'
|
168
|
+
- !ruby/object:Gem::Dependency
|
169
|
+
name: rspec-rails
|
170
|
+
requirement: !ruby/object:Gem::Requirement
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: '0'
|
175
|
+
type: :development
|
176
|
+
prerelease: false
|
177
|
+
version_requirements: !ruby/object:Gem::Requirement
|
178
|
+
requirements:
|
179
|
+
- - ">="
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: '0'
|
182
|
+
- !ruby/object:Gem::Dependency
|
183
|
+
name: rails
|
184
|
+
requirement: !ruby/object:Gem::Requirement
|
185
|
+
requirements:
|
186
|
+
- - ">="
|
187
|
+
- !ruby/object:Gem::Version
|
188
|
+
version: '3.1'
|
189
|
+
type: :runtime
|
190
|
+
prerelease: false
|
191
|
+
version_requirements: !ruby/object:Gem::Requirement
|
192
|
+
requirements:
|
193
|
+
- - ">="
|
194
|
+
- !ruby/object:Gem::Version
|
195
|
+
version: '3.1'
|
196
|
+
- !ruby/object:Gem::Dependency
|
197
|
+
name: i18n
|
198
|
+
requirement: !ruby/object:Gem::Requirement
|
199
|
+
requirements:
|
200
|
+
- - ">="
|
201
|
+
- !ruby/object:Gem::Version
|
202
|
+
version: '0'
|
203
|
+
type: :runtime
|
204
|
+
prerelease: false
|
205
|
+
version_requirements: !ruby/object:Gem::Requirement
|
206
|
+
requirements:
|
207
|
+
- - ">="
|
208
|
+
- !ruby/object:Gem::Version
|
209
|
+
version: '0'
|
210
|
+
description: Titler is a ruby gem that make titling your Rails app pages dead simple
|
211
|
+
but still allows you flexibility and control. Set specific page titles in the controller
|
212
|
+
or view with fallbacks for all situations. You can add environment and namespace
|
213
|
+
prefixes (e.g. "Admin | <Title of page>") automatically.
|
214
|
+
email:
|
215
|
+
- robert@roberttravispierce.com
|
216
|
+
executables: []
|
217
|
+
extensions: []
|
218
|
+
extra_rdoc_files: []
|
219
|
+
files:
|
220
|
+
- ".gitignore"
|
221
|
+
- ".rspec"
|
222
|
+
- ".ruby-gemset"
|
223
|
+
- ".ruby-version"
|
224
|
+
- ".travis.yml"
|
225
|
+
- Gemfile
|
226
|
+
- README.md
|
227
|
+
- Rakefile
|
228
|
+
- app/helpers/titler/titler_helper.rb
|
229
|
+
- bin/console
|
230
|
+
- bin/setup
|
231
|
+
- config/initializers/titler.rb
|
232
|
+
- config/locales/titler.en.yml
|
233
|
+
- lib/generators/titler/install/install_generator.rb
|
234
|
+
- lib/generators/titler/install/templates/titler.rb
|
235
|
+
- lib/generators/titler/locales/locales_generator.rb
|
236
|
+
- lib/titler.rb
|
237
|
+
- lib/titler/configuration.rb
|
238
|
+
- lib/titler/engine.rb
|
239
|
+
- lib/titler/version.rb
|
240
|
+
- spec/configure_spec.rb
|
241
|
+
- spec/helpers/titler_helper_spec.rb
|
242
|
+
- spec/spec_helper.rb
|
243
|
+
- titler-0.0.1.gem
|
244
|
+
- titler.gemspec
|
245
|
+
homepage: https://github.com/roberttravispierce/titler
|
246
|
+
licenses:
|
247
|
+
- MIT
|
248
|
+
metadata: {}
|
249
|
+
post_install_message:
|
250
|
+
rdoc_options: []
|
251
|
+
require_paths:
|
252
|
+
- lib
|
253
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
254
|
+
requirements:
|
255
|
+
- - "~>"
|
256
|
+
- !ruby/object:Gem::Version
|
257
|
+
version: '2.0'
|
258
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
259
|
+
requirements:
|
260
|
+
- - ">="
|
261
|
+
- !ruby/object:Gem::Version
|
262
|
+
version: '0'
|
263
|
+
requirements: []
|
264
|
+
rubyforge_project:
|
265
|
+
rubygems_version: 2.6.8
|
266
|
+
signing_key:
|
267
|
+
specification_version: 4
|
268
|
+
summary: A gem that make titling your Rails app pages dead simple but still allows
|
269
|
+
you flexibility and control.
|
270
|
+
test_files:
|
271
|
+
- spec/configure_spec.rb
|
272
|
+
- spec/helpers/titler_helper_spec.rb
|
273
|
+
- spec/spec_helper.rb
|