titlefy 0.1 → 0.3.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: e2d959eb9c1ad291b7512726b2ac4278b48c362f
4
+ data.tar.gz: a5d8503f6e1821ea8e2250852c59cbea2b7f1169
5
+ SHA512:
6
+ metadata.gz: 18b86637f93e3f96894ce51f62a7e720aa65295e70832b5ea46f29a85c117d6dceaa59e7e19717e06b0fb9c52a01fca16edd3e5b7daaacb4b88b45514690e8e3
7
+ data.tar.gz: 1ad2278a76780a5ef2cdd2cd7b91218047b5a1408cb3ca327920e0236d198dc7388b8e898354ad13bc414a882cf0a8c8bedcc4b1dced05d7c22f9ace680da17d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in titlefy.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,112 @@
1
+ # Titlefy
2
+
3
+ Awesome title tag magic with I18n support, passing variables and default title.
4
+ No more unnecessary lines in each controller method, no more setting title tags out of a view.
5
+ Avoid messy code - be slim and dry - kepp just one central place to control all your title tags!
6
+
7
+ ## Usage
8
+ Define your tiltetags within one structured file. Just simply do the following:
9
+
10
+ ```ruby
11
+ gem "titlefy", git: 'git://github.com/krtschmr/titlefy.git'
12
+ ```
13
+
14
+ Using @page_title into your HTML
15
+ ````
16
+ <title><%=@page_title %></title>
17
+ ````
18
+
19
+
20
+
21
+ Create an YML file and add the :title_tags scope. Define what you need
22
+
23
+
24
+ #### Default TilteTag
25
+ en:
26
+ title_tags:
27
+ default: My Awesome Webpage
28
+
29
+ ##### TitleTag by route_name
30
+ en:
31
+ title_tags:
32
+ routes:
33
+ list_items_path: Index of Items
34
+ order_path: Order at our Webshop
35
+
36
+ #### TitleTag by Controller/Action
37
+ en:
38
+ title_tags:
39
+ files_controller:
40
+ index: List of all Files
41
+ new: Create a new File
42
+
43
+ #### Titletag by Controller in Namespace
44
+ For Admin::DashboardController
45
+
46
+ en:
47
+ title_tags:
48
+ admin:
49
+ dashboard_conroller:
50
+ index: Overview
51
+ stats: Detailed Stats
52
+
53
+
54
+
55
+ #### Using variables in title tag
56
+
57
+ en:
58
+ title_tags:
59
+ users_controller:
60
+ show: Details of user: {{@user.name}}
61
+ videos:
62
+ index: Videos of user: {{@user.name}}
63
+ show: Video "{{@video.name}} from {{@user.name}} posted on {{@video.time}}"
64
+
65
+
66
+ It also supports any kind of resource-controller where the object is called "resource"
67
+
68
+ en:
69
+ title_tags:
70
+ routes:
71
+ dogs_path: "{{resource.funky_dog_name}} - my-petwebsite"
72
+ cats_path: "{{resource.funky_cat_name}} - my-petwebsite"
73
+
74
+ **Important!**
75
+ If your title-tag starts with a placeholder its neccessary to **start with quotes** to keep valid YML.
76
+
77
+
78
+
79
+ Lookup order is Namespace/Controller/Action, Controller/Action, RouteName, Default, RaillsAppName.
80
+ Simply set the titletag from your controller like this
81
+ ````
82
+ def index
83
+ set_title("MY special title")
84
+ end
85
+ ````
86
+
87
+
88
+ # TODO
89
+ - Multi Namespacing Support
90
+
91
+ ### Changelog
92
+
93
+ 0.3.0
94
+ - supports passing of variables
95
+ - changed lookup path of routes
96
+ - changed @title to @page_title
97
+
98
+ 0.2.0 rewritten code from 2012
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+ ###### misc
108
+ This project is licenced under the MIT license.
109
+
110
+
111
+
112
+ thanks to [phillipp](https://github.com/phillipp)
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "titlefy"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module Titlefy
2
+ VERSION = "0.3.0"
3
+ end
data/lib/titlefy.rb CHANGED
@@ -1,45 +1,67 @@
1
- class Titlefy
2
- module ClassMethods
3
-
1
+ require "titlefy/version"
2
+ require 'active_support/concern'
3
+
4
+ module Titlefy
5
+ extend ActiveSupport::Concern
6
+
7
+ def titlefy
8
+ send :include, InstanceMethods
9
+ end
10
+
11
+ included do
12
+
4
13
  def render(*args)
5
- set_title_tag if (!!!@title || @title == "")
14
+ set_title_tag unless @page_title
6
15
  super
7
16
  end
8
-
17
+
9
18
  def set_title_tag
10
- yml = YAML::load(ERB.new((IO.read("#{::Rails.root.to_s}/config/title_tags/#{I18n.locale}.yml"))).result)
11
- namespace = ""
12
- namespace = self.class.to_s.split("::").first.downcase if self.class.to_s.split("::").size > 1
13
- begin
14
- if namespace == ""
15
- title_tag = yml[controller_name][action_name]
16
- else
17
- title_tag = yml[namespace][controller_name][action_name]
19
+ title = I18n.t "title_tags.#{namespace}.#{controller_name}.#{action_name}", default: ""
20
+ title = I18n.t "title_tags.#{controller_name}.#{action_name}", default: "" if title.empty?
21
+ title = I18n.t route_name, scope: [:title_tags, :routes], default: "" if title.empty? and route_name != ""
22
+ title = default_title if title == ""
23
+
24
+ title.scan(/\{\{.*?\}\}/).each do |replace|
25
+ #{{@game.name}} => @game.name
26
+ variable = replace.gsub(/\{|\}/, "")
27
+
28
+ # catch resource_controller or similar
29
+ # variable named resource is an AR object
30
+ # rename to @tempresource so we can handle it
31
+ if variable.starts_with? "resource"
32
+ @tempresource = resource
33
+ variable.gsub!("resource", "@tempresource")
18
34
  end
19
- rescue
20
- end
21
- title_tag = yml["default"] unless title_tag
22
- raise "No Title Tag defined" unless title_tag
23
- begin
24
- @title = eval(title_tag)
25
- rescue
26
- begin
27
- @title = title_tag
28
- rescue
29
- raise "MISSING TITLE TAG"
35
+
36
+ # make "@variable.name" or "@variable" accessable without eval (!)
37
+ # and replace the {{placeholder}} with @variables value
38
+ if variable.starts_with?("@")
39
+ content = variable.split('.').inject(nil){|clazz, method| clazz.nil? ? instance_variable_get(method) : clazz.send(method)}
40
+ title.gsub! replace, content
30
41
  end
31
42
  end
43
+ set_title( title )
32
44
  end
33
-
34
- def add_title(title)
35
- @title = title
45
+
46
+ def set_title(title)
47
+ @page_title = title unless title.empty?
36
48
  end
37
- end
38
- class Engine < Rails::Engine
39
- initializer "including" do |app|
40
- ActionController::Base.send :include, Titlefy::ClassMethods
49
+
50
+ def namespace
51
+ self.class.parent.to_s.downcase
52
+ end
53
+
54
+ def default_title
55
+ I18n.t :default, scope: :title_tags, default: Rails.application.class.parent_name.humanize
56
+ end
57
+
58
+ def route_name
59
+ Rails.application.routes.router.recognize(request) do |route, _|
60
+ return route.name.to_s.split("_")[0..1].join("_")
61
+ end
41
62
  end
42
63
  end
43
-
64
+
44
65
  end
45
66
 
67
+ ActionController::Base.include Titlefy
data/titlefy.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'titlefy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "titlefy"
8
+ spec.version = Titlefy::VERSION
9
+ spec.authors = ["Tim Kretschmer"]
10
+ spec.email = ["github@krtschmr.de"]
11
+
12
+ spec.summary = %q{Extract your titletags from I18n backend.}
13
+ spec.description = %q{Set your title-tag content based on I18n locales. Just add title_tag key to your YML File and the magic begins. Lookup for Namespace|Controller|ActionName or Controller|Actionname or RouteName or Default. Also supports passing variables into the title tag}
14
+ spec.homepage = ""
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ #spec.metadata['allowed_push_host'] = "http://mygemserver.com'"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.10"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ end
metadata CHANGED
@@ -1,54 +1,83 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: titlefy
3
- version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: "0.1"
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
6
5
  platform: ruby
7
- authors:
8
- - Huan Son
9
- autorequire: builder
10
- bindir: bin
6
+ authors:
7
+ - Tim Kretschmer
8
+ autorequire:
9
+ bindir: exe
11
10
  cert_chain: []
12
-
13
- date: 2011-11-12 00:00:00 Z
14
- dependencies: []
15
-
16
- description: Simple TitleTag Helper. Reads from Multilingual YML Files.
17
- email: info@fu-media.de
11
+ date: 2015-08-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Set your title-tag content based on I18n locales. Just add title_tag
42
+ key to your YML File and the magic begins. Lookup for Namespace|Controller|ActionName
43
+ or Controller|Actionname or RouteName or Default. Also supports passing variables
44
+ into the title tag
45
+ email:
46
+ - github@krtschmr.de
18
47
  executables: []
19
-
20
48
  extensions: []
21
-
22
49
  extra_rdoc_files: []
23
-
24
- files:
50
+ files:
51
+ - ".gitignore"
52
+ - Gemfile
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
25
57
  - lib/titlefy.rb
26
- homepage:
58
+ - lib/titlefy/version.rb
59
+ - titlefy.gemspec
60
+ homepage: ''
27
61
  licenses: []
28
-
62
+ metadata: {}
29
63
  post_install_message:
30
64
  rdoc_options: []
31
-
32
- require_paths:
65
+ require_paths:
33
66
  - lib
34
- required_ruby_version: !ruby/object:Gem::Requirement
35
- none: false
36
- requirements:
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
37
69
  - - ">="
38
- - !ruby/object:Gem::Version
39
- version: "0"
40
- required_rubygems_version: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
43
74
  - - ">="
44
- - !ruby/object:Gem::Version
45
- version: "0"
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
46
77
  requirements: []
47
-
48
78
  rubyforge_project:
49
- rubygems_version: 1.8.11
79
+ rubygems_version: 2.4.8
50
80
  signing_key:
51
- specification_version: 3
52
- summary: Multilingual TitleTag Helper. Reads and Set TitleTags from a YML file.
81
+ specification_version: 4
82
+ summary: Extract your titletags from I18n backend.
53
83
  test_files: []
54
-