page_title 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +3 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +77 -0
  8. data/Rakefile +6 -0
  9. data/lib/page_title.rb +48 -0
  10. data/lib/page_title/engine.rb +8 -0
  11. data/lib/page_title/helpers.rb +8 -0
  12. data/lib/page_title/version.rb +3 -0
  13. data/page_title.gemspec +25 -0
  14. data/spec/controllers/application_controller_spec.rb +85 -0
  15. data/spec/dummy/README.rdoc +28 -0
  16. data/spec/dummy/Rakefile +6 -0
  17. data/spec/dummy/app/assets/images/.keep +0 -0
  18. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  19. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  20. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  21. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  22. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  23. data/spec/dummy/app/mailers/.keep +0 -0
  24. data/spec/dummy/app/models/.keep +0 -0
  25. data/spec/dummy/app/models/concerns/.keep +0 -0
  26. data/spec/dummy/app/views/application/index.html.erb +0 -0
  27. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  28. data/spec/dummy/bin/bundle +3 -0
  29. data/spec/dummy/bin/rails +4 -0
  30. data/spec/dummy/bin/rake +4 -0
  31. data/spec/dummy/config.ru +4 -0
  32. data/spec/dummy/config/application.rb +13 -0
  33. data/spec/dummy/config/boot.rb +5 -0
  34. data/spec/dummy/config/database.yml +25 -0
  35. data/spec/dummy/config/environment.rb +5 -0
  36. data/spec/dummy/config/environments/development.rb +29 -0
  37. data/spec/dummy/config/environments/production.rb +80 -0
  38. data/spec/dummy/config/environments/test.rb +31 -0
  39. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  40. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  41. data/spec/dummy/config/initializers/inflections.rb +16 -0
  42. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  43. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  44. data/spec/dummy/config/initializers/session_store.rb +3 -0
  45. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  46. data/spec/dummy/config/locales/en.yml +23 -0
  47. data/spec/dummy/config/routes.rb +56 -0
  48. data/spec/dummy/lib/assets/.keep +0 -0
  49. data/spec/dummy/log/.keep +0 -0
  50. data/spec/dummy/log/test.log +81 -0
  51. data/spec/dummy/public/404.html +58 -0
  52. data/spec/dummy/public/422.html +58 -0
  53. data/spec/dummy/public/500.html +57 -0
  54. data/spec/dummy/public/favicon.ico +0 -0
  55. data/spec/page_title_spec.rb +32 -0
  56. data/spec/spec_helper.rb +16 -0
  57. data/spec/support/helpers.rb +35 -0
  58. metadata +228 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1b31f68b4a317190bae0c5ddd9f12d6952bbcfdf
4
+ data.tar.gz: 6abe236198afc5ec21516fe2aaed2b146e956d78
5
+ SHA512:
6
+ metadata.gz: 607f015ff61a8f3bae0fa9b77b8e022728de7fdd5fa2e4cf39e47eea277df3b00d5c909dc899bb2e28cb61860152e0d6e1e70e0c2b086075c0d27fc0d8934ec8
7
+ data.tar.gz: 0d85dc430bd5016a616cbcfd0a9d90288e6561da9a08154f8006f497d286e1195ded1ef54dc2040bc90bb631d6e73bbee95b1c4152bbe4b611375186a299a92b
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in page_title.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nando Vieira
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,77 @@
1
+ # page_title
2
+
3
+ Set the page title on Rails apps.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem "page_title"
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install page_title
18
+
19
+ ## Usage
20
+
21
+ You can use this on your layout file to display the page title:
22
+
23
+ ```erb
24
+ <%= page_title %>
25
+ ```
26
+
27
+ The page title will be inferred from the controller and action names. For an action `SiteController#index` you'll the following translate scope:
28
+
29
+ ```yaml
30
+ en:
31
+ titles:
32
+ base: "%{title} • MyCompany"
33
+ site:
34
+ index: "Welcome to our site"
35
+ ```
36
+
37
+ The `titles.base` scope is required and will be used to set a general title information, like your company's name.
38
+
39
+ You can also set the page title for namespaced controllers. For an action `Admin::Site#index` you'll need the following translate scope:
40
+
41
+ ```yaml
42
+ en:
43
+ titles:
44
+ base: "%{title} • MyCompany"
45
+ admin:
46
+ site:
47
+ index: "Welcome to our site"
48
+ ```
49
+
50
+ Sometimes you need to render some dynamic value. In this case, you can use the I18n placeholders.
51
+
52
+ ```yaml
53
+ en:
54
+ titles:
55
+ base: "%{title} • MyCompany"
56
+ workshops:
57
+ show: "%{name}"
58
+ ```
59
+
60
+ You can set dynamic values using the `PageSite::Base#[]=`.
61
+
62
+ ```ruby
63
+ class WorkshopsController < ApplicationController
64
+ def show
65
+ @workshop = Workshop.find_by_permalink!(params[:permalink])
66
+ page_title[:name] = @workshop.name
67
+ end
68
+ end
69
+ ```
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/lib/page_title.rb ADDED
@@ -0,0 +1,48 @@
1
+ require "i18n"
2
+ require "forwardable"
3
+ require "page_title/engine"
4
+ require "page_title/helpers"
5
+ require "page_title/version"
6
+
7
+ module PageTitle
8
+ class Base
9
+ # Set the controller instance. It must implement
10
+ # the methods controller_name and action_name.
11
+ attr_reader :controller
12
+
13
+ # Set the main translation options.
14
+ attr_reader :options
15
+
16
+ extend Forwardable
17
+ def_delegators :options, :[], :[]=, :merge!
18
+
19
+ def initialize(controller)
20
+ @controller = controller
21
+ @options = {}
22
+ end
23
+
24
+ def to_s
25
+ base_translation
26
+ end
27
+
28
+ private
29
+ def title_translation
30
+ I18n.t(
31
+ title_scope,
32
+ options.merge(scope: "titles")
33
+ )
34
+ end
35
+
36
+ def base_translation
37
+ I18n.t("titles.base", title: title_translation)
38
+ end
39
+
40
+ def normalized_controller_name
41
+ controller.controller_name.gsub(%r[/], ".")
42
+ end
43
+
44
+ def title_scope
45
+ [normalized_controller_name, controller.action_name].join(".")
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,8 @@
1
+ module PageTitle
2
+ class Engine < Rails::Engine
3
+ config.to_prepare do
4
+ ApplicationController.send :include, Helpers
5
+ ApplicationController.helper_method :page_title
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module PageTitle
2
+ module Helpers
3
+ private
4
+ def page_title
5
+ @page_title ||= PageTitle::Base.new(self)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module PageTitle
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ require "./lib/page_title/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "page_title"
5
+ spec.version = PageTitle::VERSION
6
+ spec.authors = ["Nando Vieira"]
7
+ spec.email = ["fnando.vieira@gmail.com"]
8
+ spec.description = "Set the page title on Rails apps"
9
+ spec.summary = spec.description
10
+ spec.homepage = "http://github.com/fnando/page_title"
11
+ spec.license = "MIT"
12
+
13
+ spec.files = `git ls-files`.split($/)
14
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_dependency "rails"
19
+
20
+ spec.add_development_dependency "bundler"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "rspec-rails"
24
+ spec.add_development_dependency "pry-meta"
25
+ end
@@ -0,0 +1,85 @@
1
+ require "spec_helper"
2
+
3
+ describe ApplicationController do
4
+ context "simple controller name" do
5
+ set_translations "titles.base" => "%{title} - SITE",
6
+ "titles.site.index" => "TITLE"
7
+
8
+ controller do
9
+ def controller_name
10
+ "site"
11
+ end
12
+
13
+ def index
14
+ render text: page_title
15
+ end
16
+ end
17
+
18
+ it do
19
+ get :index
20
+ expect(response.body).to eql("TITLE - SITE")
21
+ end
22
+ end
23
+
24
+ context "namespaced controller name" do
25
+ set_translations "titles.base" => "%{title} - SITE",
26
+ "titles.admin.site.index" => "TITLE"
27
+
28
+ controller do
29
+ def controller_name
30
+ "admin/site"
31
+ end
32
+
33
+ def index
34
+ render text: page_title
35
+ end
36
+ end
37
+
38
+ it do
39
+ get :index
40
+ expect(response.body).to eql("TITLE - SITE")
41
+ end
42
+ end
43
+
44
+ context "using placeholders" do
45
+ set_translations "titles.base" => "%{title} - SITE",
46
+ "titles.site.index" => "%{name}"
47
+
48
+ controller do
49
+ def controller_name
50
+ "site"
51
+ end
52
+
53
+ def index
54
+ page_title[:name] = "NAME"
55
+ render text: page_title
56
+ end
57
+ end
58
+
59
+ it do
60
+ get :index
61
+ expect(response.body).to eql("NAME - SITE")
62
+ end
63
+ end
64
+
65
+ context "rendering template" do
66
+ render_views
67
+
68
+ set_translations "titles.base" => "%{title} - SITE",
69
+ "titles.site.index" => "TITLE"
70
+
71
+ controller do
72
+ def controller_name
73
+ "site"
74
+ end
75
+
76
+ def index
77
+ end
78
+ end
79
+
80
+ it do
81
+ get :index
82
+ expect(response.body).to match(%r[<title>TITLE - SITE</title>])
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
File without changes
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
File without changes
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title><%= page_title %></title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>