titleizer 0.1.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: 600b65ada6325aa5972bde0164bd426f867b43a3
4
+ data.tar.gz: 98c76e0fa8daa2948f7745143c4513257c5e34d8
5
+ SHA512:
6
+ metadata.gz: bee3b9dafdc490624bf452fcda6cddc0586a294225b6c30fce9a5c7ae620e7f4a8984f592623d568d722b2160c9040af9711f0e59fe30e49fa43a5ac9c39e3c7
7
+ data.tar.gz: efae50a846516f73b4d20291dc2841d37880fad517550923e0878d7b9cdeeba4891a21df513b6882d38adac065bcedd5f65d33353069e30cec9ae8a7fa59acab
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 kami
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,102 @@
1
+ # Titleizer
2
+
3
+ [![Build Status](https://travis-ci.org/kami30k/titleizer.svg)](https://travis-ci.org/kami30k/titleizer)
4
+ [![Gem Version](https://badge.fury.io/rb/titleizer.svg)](http://badge.fury.io/rb/titleizer)
5
+
6
+ Titleizer sets page title to Rails application using I18n.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'titleizer'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ ```
19
+ $ bundle
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ### Basic usage
25
+
26
+ #### 1. Add to locales
27
+
28
+ Add page titles to your `config/locales/*.yml`:
29
+
30
+ ```yaml
31
+ en:
32
+ title:
33
+ application: My Blog
34
+ description: This is my blog.
35
+ posts:
36
+ index: All posts
37
+ show: "%{subject}"
38
+ new: New post
39
+ edit: Edit post
40
+ ```
41
+
42
+ #### 2. Modify title tag
43
+
44
+ Modify title tag in `app/views/layouts/application.html.erb`:
45
+
46
+ ```erb
47
+ <!DOCTYPE html>
48
+ <html>
49
+ <head>
50
+ <title><%= title(yield :title) %></title>
51
+ :
52
+ </head>
53
+ ```
54
+
55
+ You should restart your application after changing locales file.
56
+ Then you will see the page title as you set.
57
+
58
+ ### Optional usage
59
+
60
+ #### Title with variable
61
+
62
+ If you want to set page title using variable, you can set `@title_params` in your action:
63
+
64
+ ```ruby
65
+ class PostsController < ApplicationController
66
+ def show
67
+ @post = Post.find(params[:id])
68
+
69
+ @title_params = { subject: @post.subject }
70
+ end
71
+ end
72
+ ```
73
+
74
+ When `@post.subject` is "My first post", `posts#show` title is as follows:
75
+
76
+ ```erb
77
+ <!DOCTYPE html>
78
+ <html>
79
+ <head>
80
+ <title>My first post | My Blog</title>
81
+ :
82
+ </head>
83
+ ```
84
+
85
+ #### Override title
86
+
87
+ If you want to override page title, you can set preferred title on view file.
88
+ This feature is useful in some case, such as rendering error page.
89
+
90
+ For example, if you set view file as follows, the page title become `404 Not Found | My Blog` even if you set title in locales.
91
+
92
+ ```erb
93
+ <% provide :title, '404 Not Found' %>
94
+ ```
95
+
96
+ ## Contributing
97
+
98
+ 1. Fork it ( https://github.com/kami30k/titleizer/fork )
99
+ 2. Create your feature branch (git checkout -b my-new-feature)
100
+ 3. Commit your changes (git commit -am 'Add some feature')
101
+ 4. Push to the branch (git push origin my-new-feature)
102
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,21 @@
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 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Titleizer'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'rspec/core/rake_task'
18
+ RSpec::Core::RakeTask.new(:spec)
19
+ task default: :spec
20
+
21
+ Bundler::GemHelper.install_tasks
data/lib/titleizer.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'titleizer/version'
2
+ require 'titleizer/base'
3
+ require 'titleizer/railtie'
@@ -0,0 +1,64 @@
1
+ module Titleizer
2
+ def title(preferred_title)
3
+ preferred_title = nil if preferred_title.empty?
4
+
5
+ title = Title.new(title_key, title_options, preferred_title, root_title?)
6
+ title.to_s
7
+ end
8
+
9
+ private
10
+
11
+ def title_key
12
+ "#{controller_name}.#{action_name}"
13
+ end
14
+
15
+ def title_options
16
+ controller.view_assigns.fetch('title_params', {}).merge(default: '')
17
+ end
18
+
19
+ def root_title?
20
+ current_page?(root_path)
21
+ end
22
+
23
+ class Title
24
+ def initialize(key, options, preferred_title, rootize = false)
25
+ @key = key
26
+ @options = options
27
+ @preferred_title = preferred_title
28
+ @rootize = rootize
29
+ end
30
+
31
+ def to_s
32
+ case mode
33
+ when :preferred
34
+ "#{@preferred_title} | #{t('application')}"
35
+ when :root
36
+ "#{t('description')} | #{t('application')}"
37
+ when :default
38
+ "#{t(@key)} | #{t('application')}"
39
+ else
40
+ t('application')
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def mode
47
+ if @preferred_title
48
+ :preferred
49
+ elsif @rootize
50
+ :root
51
+ elsif set?(@key)
52
+ :default
53
+ end
54
+ end
55
+
56
+ def set?(key)
57
+ !t(key).empty?
58
+ end
59
+
60
+ def t(key)
61
+ I18n.t("title.#{key}", @options)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,11 @@
1
+ require 'rails'
2
+
3
+ module Titleizer
4
+ class Railtie < ::Rails::Railtie
5
+ initializer 'titleizer' do
6
+ ActiveSupport.on_load(:action_view) do
7
+ ActionView::Base.send :include, Titleizer
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Titleizer
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: titleizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kami
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-21 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: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
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: capybara
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
+ description: Set page title to Rails application using I18n.
70
+ email:
71
+ - kami30k@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - lib/titleizer.rb
80
+ - lib/titleizer/base.rb
81
+ - lib/titleizer/railtie.rb
82
+ - lib/titleizer/version.rb
83
+ homepage: https://github.com/kami30k/titleizer
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.2.2
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Set page title to Rails application using I18n.
107
+ test_files: []