titlezilla 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4bb897c70e2c0289ff7978c420b95a9656ef1472
4
+ data.tar.gz: 87bb55a741c35528769cb38c5cae2cb984141035
5
+ SHA512:
6
+ metadata.gz: 41c15998a6d8bc290254e72db117471feb931171be73cbf8babb4d11b910cacfb39a452fde362dd8fcd0427c78f6e30f0ff61a3248b3ab2de30d7ee4f5b75335
7
+ data.tar.gz: f303535e0bae066c0fa43a75b21a285c3ebf2e4c16bdf37ea13d40379bd5158d69be6517c09b1b889d30b340b3ad99c93df6645b555436fcef479017356d0476
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Evgeny Likholetov <bsboris@gmail.com>
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,120 @@
1
+ # Titlezilla
2
+
3
+ Ultimate solution for dealing with titles in Rails.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'titlezilla'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Run the generator:
16
+
17
+ rails generate titlezilla:install
18
+
19
+ ## Usage
20
+
21
+ ### Basics
22
+
23
+ Put your titles in generated .yml file:
24
+
25
+ en:
26
+ titles:
27
+ application: My Application
28
+ welcome:
29
+ index: Welcome page title
30
+ users:
31
+ show: User: %{user}
32
+
33
+ Use convinient methods in your helpers:
34
+
35
+ # Assuming you are on welcome#index page:
36
+ application_title # => My Application
37
+ title # => Welcome page title
38
+ meta_title # => Welcome page title | My Application
39
+ title_tag # => <title>Welcome page title | My Application</title>
40
+
41
+ ### Passing variables to translations
42
+
43
+ All defined instance variables passed to translations.
44
+
45
+ YAML:
46
+
47
+ en:
48
+ titles:
49
+ users:
50
+ show: User: %{user}
51
+
52
+ Model:
53
+
54
+ class User < ActiveRecord::Base
55
+ def to_s
56
+ user.full_name
57
+ end
58
+ end
59
+
60
+ Controller:
61
+
62
+ def show
63
+ @user = User.find(params[:id])
64
+
65
+ View:
66
+
67
+ title # => User: John Doe
68
+
69
+ ### Namespacing
70
+
71
+ Namespaced controllers/views are supported, so you can have separate set of titles for different parts of your application.
72
+
73
+ YAML:
74
+
75
+ en:
76
+ titles:
77
+ application: My Application
78
+ ...
79
+ admin:
80
+ application: Admin Panel
81
+ dashboard:
82
+ index: Dashboard
83
+
84
+ View:
85
+
86
+ # Assuming you are on admin/welcome#index page:
87
+ application_title # => Admin Panel
88
+ title # => Dashboard
89
+ meta_title # => Dashboard | Admin Panel
90
+
91
+ ### Action aliasing (:create and :update)
92
+
93
+ Titleziila uses name of the current action for resolving titles, so when you submit a form and it fails to save, your rendered `new` template inside the `create` action. To avoid setting duplicated titles for both `new` and `create` (and `edit` and `update`), this actions is mapped in gem configuration.
94
+ If your application have similar non-RESTful pair of actions, your can add them to mapping:
95
+
96
+ config/initializers/titlezilla.rb:
97
+
98
+ Titlezilla.configure do |config|
99
+ config.action_map.merge!({perform_parse: :parse})
100
+ end
101
+
102
+ Now you can define title only for `parse` action, `perform_parse` whil use it automagically.
103
+
104
+ ### Custom meta title separator
105
+
106
+ If you want your title tag content to look like this: `My Application > My cool page`, tell Titlezilla to use custom separator.
107
+
108
+ config/initializers/titlezilla.rb:
109
+
110
+ Titlezilla.configure do |config|
111
+ config.separator = ' > '
112
+ end
113
+
114
+ ## Contributing
115
+
116
+ 1. Fork it
117
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
118
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
119
+ 4. Push to the branch (`git push origin my-new-feature`)
120
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = 'test/*_test.rb'
7
+ end
@@ -0,0 +1,12 @@
1
+ module Titlezilla
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "Copy Titlezilla default files"
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ def copy_config
8
+ directory 'config/locales'
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ en:
2
+ titles:
3
+ application: My Application
4
+ main:
5
+ index: Main index
6
+ users:
7
+ show: 'User: %{user}'
@@ -0,0 +1,29 @@
1
+ require 'titlezilla/translator'
2
+
3
+ module Titlezilla
4
+ module Helpers
5
+ module Title
6
+ def title
7
+ translator.title
8
+ end
9
+
10
+ def application_title
11
+ translator.application_title
12
+ end
13
+
14
+ def meta_title
15
+ translator.meta_title
16
+ end
17
+
18
+ def title_tag
19
+ content_tag :title, meta_title
20
+ end
21
+
22
+ private
23
+
24
+ def translator
25
+ @translator ||= Translator.new(controller_path, action_name, controller.view_assigns.symbolize_keys)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,10 @@
1
+ require 'action_view'
2
+ require 'titlezilla/helpers/title'
3
+
4
+ module Titlezilla
5
+ module Helpers
6
+ include Title
7
+ end
8
+ end
9
+
10
+ ActionView::Base.send :include, Titlezilla::Helpers
@@ -0,0 +1,45 @@
1
+ require 'active_support/core_ext/hash/reverse_merge'
2
+
3
+ module Titlezilla
4
+ class Translator
5
+ attr_reader :namespace, :controller_name, :action_name, :context
6
+
7
+ def initialize(controller_path, action_name, context = {})
8
+ @namespace, @controller_name = parse_namespace_and_controller_name(controller_path)
9
+ @action_name = action_name.to_s.freeze
10
+ @context = context
11
+ end
12
+
13
+ def title
14
+ lookup(controller_name, map(action_name))
15
+ end
16
+
17
+ def application_title
18
+ lookup('application')
19
+ end
20
+
21
+ def meta_title
22
+ [title, application_title].compact.join(::Titlezilla.configuration.separator)
23
+ end
24
+
25
+ private
26
+
27
+ def parse_namespace_and_controller_name(controller_path)
28
+ parts = controller_path.to_s.split('/')
29
+ parts.size > 1 ? parts.map(&:to_s).map(&:freeze) : [nil, parts[0].to_s.freeze]
30
+ end
31
+
32
+ def lookup(*key)
33
+ t_key = [:titles, namespace, key].flatten.compact.join('.')
34
+ i18n_set?(t_key) ? I18n.t(t_key, context) : nil
35
+ end
36
+
37
+ def map(action_name)
38
+ (::Titlezilla.configuration.action_map[action_name.to_sym] || action_name).to_s.freeze
39
+ end
40
+
41
+ def i18n_set?(key)
42
+ I18n.t(key, raise: true) rescue false
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module Titlezilla
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/titlezilla.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'titlezilla/version'
2
+ require 'titlezilla/helpers'
3
+
4
+ module Titlezilla
5
+ def self.configure
6
+ yield(configuration) if block_given?
7
+ end
8
+
9
+ def self.configuration
10
+ @configuration ||= Configuration.new
11
+ end
12
+
13
+ def self.reset_configuration
14
+ @configuration = Configuration.new
15
+ end
16
+
17
+ class Configuration
18
+ attr_accessor :action_map, :separator
19
+
20
+ def initialize
21
+ @action_map = { create: :new, update: :edit }
22
+ @separator = ' | '
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+ require 'rails/generators/test_case'
3
+ require 'generators/titlezilla/install_generator'
4
+
5
+ class GeneratorTest < Rails::Generators::TestCase
6
+ tests Titlezilla::Generators::InstallGenerator
7
+ destination File.expand_path('../../tmp', __FILE__)
8
+ setup :prepare_destination
9
+ teardown { rm_rf(destination_root) }
10
+
11
+ test 'generates example locale file' do
12
+ run_generator
13
+ assert_file 'config/locales/titles.en.yml'
14
+ end
15
+ end
@@ -0,0 +1,79 @@
1
+ require 'test_helper'
2
+
3
+ class ActionView::TestCase; include Titlezilla::Helpers::Title; end
4
+
5
+ module Titlezilla
6
+ module Helpers
7
+ describe Title do
8
+ describe 'integration' do
9
+ before do
10
+ load_translations({ application: 'App', main: { new: 'New', edit: 'Edit' } })
11
+ end
12
+
13
+ it '#title' do
14
+ @controller.controller_path = :main
15
+ @controller.action_name = :new
16
+ title.must_equal 'New'
17
+ end
18
+
19
+ it '#title using view assigns' do
20
+ load_translations({ main: { new: 'New: %{name}' } })
21
+ @controller.controller_path = :main
22
+ @controller.action_name = :new
23
+ @controller.view_assigns = { name: 'John' }
24
+ title.must_equal 'New: John'
25
+ end
26
+
27
+ it '#application_title' do
28
+ @controller.controller_path = :main
29
+ @controller.action_name = :new
30
+ application_title.must_equal 'App'
31
+ end
32
+
33
+ it '#meta_title' do
34
+ @controller.controller_path = :main
35
+ @controller.action_name = :new
36
+ meta_title.must_equal 'New | App'
37
+ end
38
+
39
+ it 'should render title tag' do
40
+ @controller.controller_path = :main
41
+ @controller.action_name = :new
42
+ title_tag.must_equal '<title>New | App</title>'
43
+ end
44
+ end
45
+
46
+ describe 'configurable' do
47
+ describe 'defaults' do
48
+ before do
49
+ load_translations({ main: { new: 'New', edit: 'Edit' } })
50
+ @controller.controller_path = :main
51
+ end
52
+
53
+ it '#new' do
54
+ @controller.action_name = :create
55
+ title.must_equal 'New'
56
+ end
57
+
58
+ it '#create' do
59
+ @controller.action_name = :update
60
+ title.must_equal 'Edit'
61
+ end
62
+ end
63
+
64
+ describe 'custom' do
65
+ before do
66
+ ::Titlezilla.configure { |c| c.action_map.merge!({ do_parse: :parse }) }
67
+ load_translations({ main: { parse: 'Parse' } })
68
+ @controller.controller_path = :main
69
+ end
70
+
71
+ it '#parse' do
72
+ @controller.action_name = :do_parse
73
+ title.must_equal 'Parse'
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,45 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/spec'
3
+ require 'i18n'
4
+ require 'active_support/test_case'
5
+ require 'action_view/test_case'
6
+ require 'titlezilla'
7
+
8
+ class ActiveSupport::TestCase
9
+ class << self
10
+ remove_method :describe
11
+ end if self.respond_to? :describe
12
+
13
+ extend MiniTest::Spec::DSL
14
+ end
15
+
16
+ class MockController < Struct.new(:action_name, :controller_path)
17
+ attr_accessor :view_assigns
18
+
19
+ def initialize
20
+ @view_assigns = {}
21
+ end
22
+ end
23
+
24
+ class ActionView::TestCase
25
+ register_spec_type(/Helper( ?Test)?\z/i, self)
26
+
27
+ setup :set_controller
28
+
29
+ def set_controller
30
+ @controller = MockController.new
31
+ end
32
+ end
33
+
34
+ I18n.enforce_available_locales = false
35
+
36
+ def load_translations(titles)
37
+ I18n.backend.store_translations(:en, { titles: titles })
38
+ end
39
+
40
+ class Minitest::Spec
41
+ after do
42
+ ::Titlezilla.reset_configuration
43
+ I18n.reload!
44
+ end
45
+ end
@@ -0,0 +1,90 @@
1
+ require 'test_helper'
2
+
3
+ module Titlezilla
4
+ describe Translator do
5
+ describe '#initialize' do
6
+ it 'should initialize controller_name' do
7
+ t = Translator.new('main', 'index')
8
+ t.controller_name.must_equal 'main'
9
+ end
10
+
11
+ it 'should initialize action_name' do
12
+ t = Translator.new('main', 'index')
13
+ t.action_name.must_equal 'index'
14
+ end
15
+
16
+ it 'should initialize namespace' do
17
+ t = Translator.new('admin/main', 'index')
18
+ t.namespace.must_equal 'admin'
19
+ end
20
+
21
+ it 'should initialize from symbols' do
22
+ t = Translator.new(:'admin/main', :index)
23
+ t.namespace.must_equal 'admin'
24
+ t.controller_name.must_equal 'main'
25
+ t.action_name.must_equal 'index'
26
+ end
27
+ end
28
+
29
+ describe '#title' do
30
+ it 'should lookup action title' do
31
+ load_translations({ main: { index: 'Index' } })
32
+ t = Translator.new(:main, :index)
33
+ t.title.must_equal 'Index'
34
+ end
35
+
36
+ it 'should lookup action title with namespace' do
37
+ load_translations({ admin: { main: { index: 'Index' } } })
38
+ t = Translator.new('admin/main', :index)
39
+ t.title.must_equal 'Index'
40
+ end
41
+
42
+ it 'should lookup mapped actions title' do
43
+ load_translations({ main: { new: 'New' } })
44
+ t = Translator.new(:main, :create)
45
+ t.title.must_equal 'New'
46
+ end
47
+
48
+ it 'should allow using context' do
49
+ load_translations({ main: { new: 'New: %{name}' } })
50
+ t = Translator.new(:main, :create, name: 'John')
51
+ t.title.must_equal 'New: John'
52
+ end
53
+ end
54
+
55
+ describe '#application_title' do
56
+ it 'should lookup application title' do
57
+ load_translations({ application: 'App' })
58
+ t = Translator.new(:main, :index)
59
+ t.application_title.must_equal 'App'
60
+ end
61
+
62
+ it 'should lookup application title when namespace provided' do
63
+ load_translations({ admin: { application: 'App' } })
64
+ t = Translator.new('admin/main', :index)
65
+ t.application_title.must_equal 'App'
66
+ end
67
+ end
68
+
69
+ describe '#meta_title' do
70
+ it 'should return app title if action title not provided' do
71
+ load_translations({ application: 'App' })
72
+ t = Translator.new(:main, :index)
73
+ t.meta_title.must_equal 'App'
74
+ end
75
+
76
+ it 'should return joined title' do
77
+ load_translations({ application: 'App', main: { index: 'Index' } })
78
+ t = Translator.new(:main, :index)
79
+ t.meta_title.must_equal 'Index | App'
80
+ end
81
+
82
+ it 'should return joined title with custom separator' do
83
+ load_translations({ application: 'App', main: { index: 'Index' } })
84
+ ::Titlezilla.configure { |c| c.separator = ' > ' }
85
+ t = Translator.new(:main, :index)
86
+ t.meta_title.must_equal 'Index > App'
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'titlezilla/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'titlezilla'
9
+ spec.version = Titlezilla::VERSION
10
+ spec.authors = ['Evgeny Likholetov']
11
+ spec.email = ['bsboris@gmail.com']
12
+ spec.description = 'Ultimate solution for dealing with titles in Rails.'
13
+ spec.summary = 'Ultimate solution for dealing with titles in Rails.'
14
+ spec.homepage = 'https://github.com/bsboris/titlezilla'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.test_files = spec.files.grep(%r{^(test)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'actionpack', '>= 4.0'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'minitest', '~> 4.2'
25
+ spec.add_development_dependency 'railties'
26
+ spec.add_development_dependency 'rake'
27
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: titlezilla
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Evgeny Likholetov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.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.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: railties
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: rake
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
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Ultimate solution for dealing with titles in Rails.
84
+ email:
85
+ - bsboris@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/generators/titlezilla/install_generator.rb
96
+ - lib/generators/titlezilla/templates/config/locales/titles.en.yml
97
+ - lib/titlezilla.rb
98
+ - lib/titlezilla/helpers.rb
99
+ - lib/titlezilla/helpers/title.rb
100
+ - lib/titlezilla/translator.rb
101
+ - lib/titlezilla/version.rb
102
+ - test/generator_test.rb
103
+ - test/helpers/title_test.rb
104
+ - test/test_helper.rb
105
+ - test/translator_test.rb
106
+ - titlezilla.gemspec
107
+ homepage: https://github.com/bsboris/titlezilla
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.2.0
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Ultimate solution for dealing with titles in Rails.
131
+ test_files:
132
+ - test/generator_test.rb
133
+ - test/helpers/title_test.rb
134
+ - test/test_helper.rb
135
+ - test/translator_test.rb