smart-meta 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Jeffrey Chupp
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.rdoc ADDED
@@ -0,0 +1,76 @@
1
+ = SmartMeta
2
+
3
+ SmartMeta is a simple plugin to allow you to set titles, descriptions, and keywords easily on pages. You just need to define your controller and action pairs in your translation files and SmartMeta will handle the rest. It can make use of your instance variables, helper methods, and even go through relationships to access relevant variables for interpolations.
4
+
5
+ It isn't the most exciting thing ever, but I've found myself using this pattern enough that extracting it seems like it will save me time in the future.
6
+
7
+ There are likely other plugins that do the same thing (and possibly better). If you know of another one that works in a similar manner but is arguably better, message me on github and let me know.
8
+
9
+ == Installation
10
+
11
+ Install as a Rails plugin:
12
+
13
+ ./script/plugin install git://github.com/semanticart/smart-meta.git
14
+
15
+ == Usage Overview
16
+
17
+ Once you've installed, to get rolling you only have to do two things:
18
+
19
+ 1) Fill out your translation files with titles, descriptions, and keywords (as you wish)
20
+
21
+ 2) Use smart_title, smart_description, and smart_keywords where you want SmartMeta to work (probably in your application layout).
22
+
23
+
24
+ === Example
25
+
26
+ Fill out your translation files in #{RAILS_ROOT}/config/locales/ like the following example:
27
+
28
+ en:
29
+ users:
30
+ show:
31
+ title: "{{@user.name}} has {{@user.hair.color}} hair"
32
+ description: "{{@user.name}}'s page on my site"
33
+ keywords: "user, {{@user.name}}, {{page_keywords}}, etc. etc."
34
+
35
+
36
+ Here I've defined that my Users controller's #show method should have a title based off of the user's name and hair color. When I call the smart_title method, it will look for an instance variable named @user and pass it name to populate the first argument and the look to the user's hair for the color to populate the second argument.
37
+
38
+ The keywords makes use of page_keywords which is a method we'll define in either our ApplicationHelper or our UsersHelper.
39
+
40
+ === Failover
41
+
42
+ This isn't a catch-all solution so I recommend implementing it something like this in your application layout:
43
+
44
+ <title><%= yield(:html_title) || smart_title || t('default.title') %></title>
45
+
46
+ <meta name="description" content="<%= yield(:meta_description) || smart_description || t('default.description') %>" />
47
+
48
+ <meta name="keywords" content="<%= yield(:meta_keywords) || smart_keywords || t('default.keywords') %>" />
49
+
50
+
51
+ Here I'm telling the layout that if I've defined a :html_title via content_for elsewhere, use that. Otherwise use the smart_title. If both of those return nil available (for instance if we're on an action we haven't defined a title for), then use the site's default title.
52
+
53
+ You'll need to specify your own default title in your translations. Example:
54
+
55
+ en:
56
+ default:
57
+ title: "Some rad title for my site"
58
+
59
+
60
+ For completeness sake, here's an example of using content_for to set the title in a view:
61
+
62
+ <% content_for(:html_title, "Some title to be used instead of smart_title and default title") %>
63
+
64
+ === Other uses
65
+
66
+ You can use this plugin to handle things other than titles, descriptions, or keywords. Simply call smart_meta_for and pass what you want. To access "users.show.robots" you just need to define
67
+
68
+ en:
69
+ users:
70
+ show:
71
+ robots: "NOINDEX"
72
+
73
+ and then call smart_meta_for(:robots) in your view/layout/etc.
74
+
75
+
76
+ Copyright (c) 2010 Jeffrey Chupp, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gem|
4
+ gem.name = "smart-meta"
5
+ gem.summary = %Q{smart-meta}
6
+ gem.description = %Q{A plugin for simplifying view titles, descriptions, and keywords}
7
+ gem.email = "vad4msiu@gmail.com"
8
+ gem.homepage = "http://github.com/vad4msiu/smart-meta.git"
9
+ gem.authors = ["Sirupsen"]
10
+ end
11
+ Jeweler::GemcutterTasks.new
12
+ rescue LoadError
13
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
14
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ ApplicationHelper.send(:include, SmartMeta)
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
data/lib/smart_meta.rb ADDED
@@ -0,0 +1,47 @@
1
+ module SmartMeta
2
+ def smart_meta_for(kind)
3
+ key = [params[:controller], params[:action], kind].compact.join('.')
4
+ if (title_template = translation_for(key))
5
+ needed_args = title_template.scan(/\{\{(.*?)\}\}/).flatten
6
+ args = needed_args.inject({}) do |hash, arg|
7
+ hash[arg.to_sym] = value_from_arg(arg)
8
+ hash
9
+ end
10
+ I18n.t(key, args)
11
+ end
12
+ end
13
+
14
+ def smart_description
15
+ smart_meta_for(:description)
16
+ end
17
+
18
+ def smart_keywords
19
+ smart_meta_for(:keywords)
20
+ end
21
+
22
+ def smart_title
23
+ smart_meta_for(:title)
24
+ end
25
+
26
+ private
27
+
28
+ def translation_for(key)
29
+ key.present? && I18n.backend.send(:lookup, I18n.locale || I18n.default_locale, key)
30
+ end
31
+
32
+ def value_from_arg(arg)
33
+ parts = arg.split('.')
34
+ # see if we're dealing with an instance variable
35
+ if parts[0].starts_with?('@')
36
+ ret = instance_variable_get(parts.shift)
37
+ else
38
+ ret = send(parts.shift)
39
+ end
40
+
41
+ # send the rest of our messages
42
+ parts.each do |part|
43
+ ret = ret.send(part)
44
+ end
45
+ ret
46
+ end
47
+ end
@@ -0,0 +1,4 @@
1
+ en:
2
+ users:
3
+ show:
4
+ title: "{{@name}} has {{@color}} eyes"
@@ -0,0 +1,4 @@
1
+ koolaid:
2
+ users:
3
+ show:
4
+ title: "OH YEAH!!!! {{@name}} has {{@color}} eyes... OH YEAH!!!!"
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe SmartMeta do
4
+ include SmartMeta
5
+
6
+ before(:all) do
7
+ ['en', 'koolaid'].each do |lang|
8
+ I18n.load_path << File.expand_path(File.join(File.dirname(__FILE__), "languages/#{lang}.yml"))
9
+ end
10
+ end
11
+
12
+ describe 'smart_meta_for' do
13
+ it 'can handle instance variables' do
14
+ @name = "Jeff"
15
+ @color = "blue"
16
+
17
+ stub!(:params).and_return(:action => "show", :controller => "users")
18
+ smart_meta_for(:title).should == "Jeff has blue eyes"
19
+ end
20
+
21
+ it 'can handle using other accessible methods' do
22
+ def an_animal
23
+ "panda"
24
+ end
25
+
26
+ stub!(:params).and_return(:action => "show", :controller => "animals")
27
+ I18n.backend.stub!(:lookup, "animals.show.title").and_return("The {{an_animal.pluralize}} are cute!")
28
+
29
+ smart_meta_for(:title).should == "The pandas are cute!"
30
+ end
31
+
32
+ it "works with multiple locales" do
33
+ @name = "Jeff"
34
+ @color = "blue"
35
+
36
+ stub!(:params).and_return(:action => "show", :controller => "users")
37
+
38
+ I18n.locale = :koolaid
39
+ smart_meta_for(:title).should == "OH YEAH!!!! Jeff has blue eyes... OH YEAH!!!!"
40
+
41
+ I18n.locale = :en
42
+ smart_meta_for(:title).should == "Jeff has blue eyes"
43
+ end
44
+
45
+ it "can send multiple messages to an object" do
46
+ @controller = "dinosaurs"
47
+ mock_dino = mock(:name => "Grizzly-T-Rex")
48
+ @dinosaurs = [mock_dino, mock]
49
+
50
+ stub!(:params).and_return(:action => "index", :controller => "dinosaurs")
51
+ I18n.backend.stub!(:lookup, "dinosaurs.index.title").and_return("{{@dinosaurs.first.name}} is the scariest of all the {{@controller}}!")
52
+
53
+ smart_meta_for(:title).should == "Grizzly-T-Rex is the scariest of all the dinosaurs!"
54
+ end
55
+
56
+ it 'returns nil if it cannot match the translation' do
57
+ stub!(:params).and_return(:action => "something", :controller => "something else")
58
+ smart_meta_for(:title).should be_nil
59
+ end
60
+ end
61
+
62
+ [:title, :keywords, :description].each do |kind|
63
+ describe "smart_#{kind}" do
64
+ it "calls smart_meta_for with :#{kind}" do
65
+ should_receive(:smart_meta_for).with(kind)
66
+
67
+ self.send(:"smart_#{kind}")
68
+ end
69
+ end
70
+ end
71
+
72
+ it 'allows you to use custom kinds' do
73
+ stub!(:params).and_return(:action => "index", :controller => "dinosaurs")
74
+ should_receive(:translation_for).with('dinosaurs.index.robots')
75
+ smart_meta_for("robots")
76
+ end
77
+ end
@@ -0,0 +1,4 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "../lib/smart_meta.rb"))
2
+ require 'rubygems'
3
+ require 'spec'
4
+ require 'active_support'
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :smart_meta do
3
+ # # Task goes here
4
+ # end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smart-meta
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Sirupsen
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-22 00:00:00 +04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: A plugin for simplifying view titles, descriptions, and keywords
23
+ email: vad4msiu@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ files:
31
+ - MIT-LICENSE
32
+ - README.rdoc
33
+ - Rakefile
34
+ - VERSION
35
+ - init.rb
36
+ - install.rb
37
+ - lib/smart_meta.rb
38
+ - spec/languages/en.yml
39
+ - spec/languages/koolaid.yml
40
+ - spec/smart_meta_spec.rb
41
+ - spec/spec_helper.rb
42
+ - tasks/smart_titles_tasks.rake
43
+ - uninstall.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/vad4msiu/smart-meta.git
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.5.0
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: smart-meta
78
+ test_files: []
79
+