automatic_page_titles 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0a39cb0450cacb5887765f2e3316446a801da4626acc0ceaa08fd9820fe59a01
4
+ data.tar.gz: 55e3a96f76c81b75c98aeb6fa63e61878640442bac801fbf6bcd0333a7040a9d
5
+ SHA512:
6
+ metadata.gz: 6c4e0c6386b42468507ff63847b75e034b4cfdef4305e58daa79b74a1d6e39b9e888ae3d0b8c9729ddb92f235b8ba0a4ebee3d7d4e8165619355e29e6b7711a2
7
+ data.tar.gz: 747a69200b065bae10f377269149991de11ca73cd9f6be5a75af6228a6ec5830bd3dc628c2ba196806c8f60eab1fd29b9517f3beb21a9f41fdd0089f3110925c
@@ -0,0 +1,20 @@
1
+ Copyright 2015 neilang
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.
@@ -0,0 +1,34 @@
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 = "AutomaticPageTitles"
12
+ rdoc.options << "--line-numbers"
13
+ rdoc.rdoc_files.include("README.rdoc")
14
+ rdoc.rdoc_files.include("lib/**/*.rb")
15
+ end
16
+
17
+ Bundler::GemHelper.install_tasks
18
+
19
+ require "rspec/core/rake_task"
20
+ RSpec::Core::RakeTask.new(:spec)
21
+
22
+ task test: :spec
23
+
24
+ begin
25
+ require "rubocop/rake_task"
26
+ RuboCop::RakeTask.new
27
+ rescue LoadError
28
+ desc "Run RuboCop"
29
+ task :rubocop do
30
+ $stderr.puts "Rubocop is disabled"
31
+ end
32
+ end
33
+
34
+ task default: [:spec, :rubocop]
@@ -0,0 +1,3 @@
1
+ module AutomaticPageTitles
2
+ require "automatic_page_titles/railtie" if defined?(Rails)
3
+ end
@@ -0,0 +1,83 @@
1
+ module AutomaticPageTitles
2
+ module PageTitleHelper
3
+ def page_title(page_title = nil)
4
+ "#{admin_prefix}#{page_title_no_admin_prefix(page_title)}"
5
+ end
6
+
7
+ def page_title_no_admin_prefix(page_title = nil)
8
+ @page_title = page_title unless page_title.nil?
9
+ @page_title || restful_page_title || custom_action_page_title
10
+ end
11
+
12
+ def restful_page_title
13
+ case action_name
14
+ when "index"
15
+ controller_title
16
+ when "show"
17
+ "#{controller_title_singular}#{padded_automatic_variable_title}"
18
+ when "edit", "update"
19
+ "Edit #{controller_title_singular}#{padded_automatic_variable_title}"
20
+ when "new", "create"
21
+ "New #{controller_title_singular}"
22
+ end
23
+ end
24
+
25
+ def custom_action_page_title
26
+ if params[:id].present?
27
+ "#{action_name.titleize} #{controller_title_singular}#{padded_automatic_variable_title}"
28
+ else
29
+ "#{action_name.titleize} #{controller_title}"
30
+ end
31
+ end
32
+
33
+ def controller_title
34
+ controller_name.titleize
35
+ end
36
+
37
+ def controller_title_singular
38
+ controller_title.singularize
39
+ end
40
+
41
+
42
+ def path_to_page_title(path = current_path)
43
+ title = path.to_s.split("/").last
44
+ return nil if title.nil?
45
+
46
+ title = title.gsub(/[-_]/, " ")
47
+ title = title.gsub(/\s+/, " ").strip
48
+ title.humanize
49
+ end
50
+
51
+ def default_page_title
52
+ Rails.application.class.to_s.split("::").first.underscore.humanize
53
+ end
54
+
55
+ def current_path
56
+ request.try :path
57
+ end
58
+
59
+ def path_without_last_segment(path)
60
+ path = path.to_s
61
+ index = path.rindex("/")
62
+ index ? path[0, index] : nil
63
+ end
64
+
65
+ def automatic_variable_title
66
+ return unless controller && controller_name
67
+ var_name = "@" + controller_name.singularize
68
+ variable = controller.instance_variable_get(var_name)
69
+ variable.try(:title) || variable.try(:name)
70
+ end
71
+
72
+ def padded_automatic_variable_title
73
+ return "" if automatic_variable_title.blank?
74
+ ": #{automatic_variable_title}"
75
+ end
76
+
77
+ def admin_prefix
78
+ current_path = request.try :path
79
+ return '' unless current_path.to_s.match?(%r{\/admin\/})
80
+ "Administrator - "
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,9 @@
1
+ require "automatic_page_titles/page_title_helper"
2
+
3
+ module AutomaticPageTitles
4
+ class Railtie < Rails::Railtie
5
+ initializer "page_title.helper" do
6
+ ActionView::Base.send :include, PageTitleHelper
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module AutomaticPageTitles
2
+ VERSION = "1.0.0".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: automatic_page_titles
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - neilang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-26 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.0'
20
+ type: :development
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: sqlite3
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: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.30'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.30'
69
+ - !ruby/object:Gem::Dependency
70
+ name: capybara
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.4'
83
+ description: Allows you to easily set page titles per view in your Rails 4 app, but
84
+ also follows Ruby on Rails naming conventions to set sensible default title if none
85
+ is specified.
86
+ email:
87
+ - neilang@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - MIT-LICENSE
93
+ - Rakefile
94
+ - lib/automatic_page_titles.rb
95
+ - lib/automatic_page_titles/page_title_helper.rb
96
+ - lib/automatic_page_titles/railtie.rb
97
+ - lib/automatic_page_titles/version.rb
98
+ homepage: https://github.com/neilang/automatic_page_titles
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.7.6
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Smart page title defaults for your Rails app.
122
+ test_files: []