merb_meta 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Cory ODaniel
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 ADDED
@@ -0,0 +1,66 @@
1
+ merb_meta
2
+ ========
3
+
4
+ A plugin for merb that allows super easy manipulation of Meta tags on a controller by controller,
5
+ page-by-page basis
6
+
7
+ Merb::Controller#meta
8
+
9
+
10
+ ### ... from app/controllers/application.rb
11
+
12
+ class Application < Merb::Controller
13
+ meta :title => "My default page title",
14
+ :description => "My default page description",
15
+ :keywords => "cool, default keywords, web 2.0"
16
+ end
17
+
18
+ ### ... from app/views/layout/application.html.erb
19
+ # meta tags can be output via 'meta' or 'meta :tagname'
20
+
21
+ # using 'meta' will output all the meta data in html tags like
22
+ <html>
23
+ <head>
24
+ <%= meta %>
25
+ <!--
26
+ Would render;
27
+ <title>My default page title</title>
28
+ <meta name="keywords" content="cool, default keywords, web 2.0" />
29
+ <meta name="description" content="My default page description" />
30
+ -->
31
+ </head>
32
+ ...
33
+ </html>
34
+
35
+ # optionally you can get the value of a particular tag like so:
36
+ <html>
37
+ <head>
38
+ <title><%= meta :title %></title>
39
+ <meta name="keywords" content="<%= meta :keywords %>" />
40
+ <meta name="description" content="<%= meta :description %>" />
41
+ </head>
42
+ <body>
43
+ ...
44
+ </body>
45
+ </html>
46
+
47
+ ### ... the tags can be overriden on a per controller or per action basis.
48
+ class ControllerA < Application
49
+ meta :title => "This is controller a"
50
+
51
+ def action_a
52
+ meta :title => "this is action a", :keywords => "action a, action"
53
+ render
54
+ # :title => "this is action a"
55
+ # :keywords => "action a, action"
56
+ # :description => "My default page description"
57
+ end
58
+
59
+ def action_b
60
+ render
61
+ # :title => "This is controller a"
62
+ # :description => "My default page description"
63
+ # :keywords => "cool, default keywords, web 2.0"
64
+ end
65
+
66
+ end
data/Rakefile ADDED
@@ -0,0 +1,76 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems/specification'
4
+ require 'date'
5
+ require 'merb-core/version'
6
+ require 'merb-core/tasks/merb_rake_helper'
7
+ require "extlib"
8
+ require "spec/rake/spectask"
9
+
10
+ NAME = "merb_meta"
11
+ GEM_VERSION = "0.0.4"
12
+ AUTHOR = "Cory ODaniel"
13
+ EMAIL = "merb-meta@coryodaniel.com"
14
+ HOMEPAGE = "http://merbivore.com/"
15
+ SUMMARY = "A plugin for the Merb framework that provides easy access to setting per-page meta tags."
16
+
17
+ spec = Gem::Specification.new do |s|
18
+ s.rubyforge_project = 'merb'
19
+ s.name = NAME
20
+ s.version = GEM_VERSION
21
+ s.platform = Gem::Platform::RUBY
22
+ s.has_rdoc = true
23
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
24
+ s.summary = SUMMARY
25
+ s.description = s.summary
26
+ s.author = AUTHOR
27
+ s.email = EMAIL
28
+ s.homepage = HOMEPAGE
29
+ #s.add_dependency('merb', '>= 0.0.0')
30
+ s.require_path = 'lib'
31
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
32
+
33
+ end
34
+
35
+ Rake::GemPackageTask.new(spec) do |pkg|
36
+ pkg.gem_spec = spec
37
+ end
38
+
39
+ desc "install the plugin locally"
40
+ task :install => [:package] do
41
+ sh %{sudo gem install #{install_home} pkg/#{NAME}-#{GEM_VERSION} --no-update-sources}
42
+ end
43
+
44
+ desc "create a gemspec file"
45
+ task :make_spec do
46
+ File.open("#{NAME}.gemspec", "w") do |file|
47
+ file.puts spec.to_ruby
48
+ end
49
+ end
50
+
51
+ namespace :jruby do
52
+
53
+ desc "Run :package and install the resulting .gem with jruby"
54
+ task :install => :package do
55
+ sh %{#{sudo} jruby -S gem install #{install_home} pkg/#{NAME}-#{GEM_VERSION}.gem --no-rdoc --no-ri}
56
+ end
57
+
58
+ end
59
+
60
+ ##############################################################################
61
+ # Specs
62
+ ##############################################################################
63
+ desc "Run all specs"
64
+ Spec::Rake::SpecTask.new("specs") do |t|
65
+ t.spec_opts = ["--format", "specdoc", "--colour"]
66
+ t.spec_files = Dir["spec/**/*_spec.rb"].sort
67
+ end
68
+
69
+ desc "Run all specs and generate an rcov report"
70
+ Spec::Rake::SpecTask.new('rcov') do |t|
71
+ t.spec_files = FileList['spec/**/*_spec.rb']
72
+ t.spec_opts = ["--format", "specdoc", "--colour"]
73
+ t.rcov = true
74
+ t.rcov_dir = 'coverage'
75
+ t.rcov_opts = ['--exclude', 'gems', '--exclude', 'spec']
76
+ end
data/TODO ADDED
@@ -0,0 +1 @@
1
+ # Im sure there is a sneakier less verbose method of doing this, I was in a rush :)
data/lib/merb_meta.rb ADDED
@@ -0,0 +1,78 @@
1
+ # make sure we're running inside Merb
2
+ if defined?(Merb::Plugins)
3
+ module Merb
4
+ class Controller
5
+ class << self
6
+ alias :orig_inherited :inherited
7
+ # Default any subclasses meta tags to that of the parent
8
+ def inherited(klass)
9
+ orig_inherited(klass)
10
+ klass.meta(self.meta)
11
+ end
12
+
13
+ # Setter/Getter for meta Tags
14
+ #
15
+ # @param tags [Hash|Symbol|Nil]
16
+ # Hash => Sets the given meta tags
17
+ # NilClass => Returns the full hash of meta tags
18
+ # Symbol => Returns the specific meta tag
19
+ #
20
+ # @api public
21
+ def meta(tags = nil)
22
+ @meta_tags ||={}
23
+
24
+ if tags.is_a? Hash
25
+ @meta_tags.merge!(tags)
26
+ elsif tags.is_a? Symbol
27
+ return @meta_tags[tags]
28
+ end
29
+
30
+ @meta_tags
31
+ end
32
+ end
33
+
34
+ # Getter/Setter for a controller instance's meta tags
35
+ #
36
+ # @param tags [Hash|NilClass|Symbol]
37
+ # Hash => Sets the given meta tags
38
+ # NilClass => Outputs the HTML
39
+ # Symbol => Returns the specific meta tag
40
+ #
41
+ # @returns [Hash|String]
42
+ #
43
+ # @api public
44
+ def meta(tags = nil)
45
+ @meta_tags ||= self.class.meta.clone
46
+
47
+ if tags.is_a? Hash
48
+ @meta_tags.merge!(tags)
49
+ @meta_tags
50
+ elsif tags.is_a? Symbol
51
+ return @meta_tags[tags]
52
+ else
53
+ output_meta_tags @meta_tags
54
+ end
55
+ end
56
+
57
+ protected
58
+
59
+ # Outputs meta tags
60
+ #
61
+ # @param meta_data [Hash]
62
+ # Meta Tags to output
63
+ #
64
+ # @api private
65
+ def output_meta_tags(meta_data)
66
+ _meta_data = meta_data.clone
67
+ meta_title = _meta_data.delete :title
68
+ markup = meta_title ? %{<title>#{meta_title}</title>} : ''
69
+
70
+ _meta_data.each{|name,content|
71
+ markup << %{<meta name="#{name}" content="#{content}" />}
72
+ }
73
+
74
+ markup
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,87 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "merb_meta" do
4
+ class Application < Merb::Controller
5
+ meta :title => 'My default title',
6
+ :description => 'An awesome description',
7
+ :keywords => 'awesome, test, controller'
8
+ end
9
+
10
+ class DefaultPerController < Application
11
+ meta :title => 'Woo, the Internet!!!',
12
+ :description => "its a fun place to make fun of people and find pr0n",
13
+ :keywords => "fun,place,people,pr0n"
14
+ end
15
+
16
+ class OverrideSomeController < Application
17
+ meta :title => "This is different, Arbys is different"
18
+ end
19
+
20
+ class OverridePerActionController < Application
21
+ meta :title => 'Override this plz.'
22
+ def index
23
+ meta :keywords => "something fantastic"
24
+ end
25
+ end
26
+
27
+ it "should be able to have default meta information for all controllers" do
28
+ Application.meta.class.should be(Hash)
29
+ Application.meta(:title).should == 'My default title'
30
+ end
31
+
32
+ it "should be able to have default meta information for a controller" do
33
+ Application.meta(:keywords).should == 'awesome, test, controller'
34
+ DefaultPerController.meta(:keywords).should == "fun,place,people,pr0n"
35
+ default_per_controller = DefaultPerController.new fake_request
36
+ default_per_controller.meta(:keywords).should == "fun,place,people,pr0n"
37
+ end
38
+
39
+ it 'should be able to override meta info on a tag-by-tag basis' do
40
+ Application.meta(:title).should == 'My default title'
41
+ OverrideSomeController.meta(:title).should == "This is different, Arbys is different"
42
+ #OverrideSomeController.meta(:keywords).should == Application.meta(:keywords)
43
+
44
+ override_some = OverrideSomeController.new fake_request
45
+ override_some.meta(:title).should == OverrideSomeController.meta(:title)
46
+ override_some.meta :keywords => "super, awesome, keywords"
47
+ override_some.meta(:keywords).should_not == OverrideSomeController.meta(:keywords)
48
+ end
49
+
50
+ it 'should default meta tags to that of the parent class' do
51
+ Application.meta(:keywords).should == 'awesome, test, controller'
52
+ OverrideSomeController.meta(:keywords).should == Application.meta(:keywords)
53
+ end
54
+
55
+ it "should be able to have meta information on an action-by-action basis" do
56
+ override_per_action = OverridePerActionController.new fake_request
57
+ override_per_action.index
58
+ override_per_action.meta(:keywords).should == "something fantastic"
59
+ end
60
+
61
+ it "should be able to output all meta data as HTML" do
62
+ override_some_meta_html = OverrideSomeController.new(fake_request).meta
63
+ index_not_called_meta_html = OverridePerActionController.new(fake_request).meta
64
+
65
+ opa_controller = OverridePerActionController.new fake_request
66
+ opa_controller.index
67
+ index_called_meta_html = opa_controller.meta
68
+
69
+ override_some_meta_html.class.should be(String)
70
+ index_not_called_meta_html.class.should be(String)
71
+ index_called_meta_html.class.should be(String)
72
+
73
+ override_some_meta_html.should == %{<title>This is different, Arbys is different</title><meta name="description" content="An awesome description" /><meta name="keywords" content="awesome, test, controller" />}
74
+ index_not_called_meta_html.should == %{<title>Override this plz.</title><meta name="description" content="An awesome description" /><meta name="keywords" content="awesome, test, controller" />}
75
+ index_called_meta_html.should == %{<title>Override this plz.</title><meta name="description" content="An awesome description" /><meta name="keywords" content="something fantastic" />}
76
+ end
77
+
78
+ it "should be able to retrieve meta data for a specific tag" do
79
+ DefaultPerController.meta(:keywords).class.should be(String)
80
+ DefaultPerController.meta(:title).class.should be(String)
81
+ DefaultPerController.meta(:description).class.should be(String)
82
+
83
+ DefaultPerController.meta(:title).should == 'Woo, the Internet!!!'
84
+ DefaultPerController.meta(:description).should == "its a fun place to make fun of people and find pr0n"
85
+ DefaultPerController.meta(:keywords).should == "fun,place,people,pr0n"
86
+ end
87
+ end
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ $TESTING=true
3
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
4
+ require 'merb-core'
5
+ require 'merb_meta'
6
+
7
+ Spec::Runner.configure do |config|
8
+ config.include(Merb::Test::ViewHelper)
9
+ config.include(Merb::Test::RouteHelper)
10
+ config.include(Merb::Test::ControllerHelper)
11
+ end
12
+
13
+ def new_controller(action = 'index', controller = nil, additional_params = {})
14
+ request = OpenStruct.new
15
+ request.params = {:action => action, :controller => (controller.to_s || "Test")}
16
+ request.params.update(additional_params)
17
+ request.cookies = {}
18
+ request.accept ||= '*/*'
19
+
20
+ yield request if block_given?
21
+
22
+ response = OpenStruct.new
23
+ response.read = ""
24
+ (controller || Merb::Controller).build(request, response)
25
+ end
26
+
27
+ class Merb::Controller
28
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: merb_meta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Cory ODaniel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-15 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A plugin for the Merb framework that provides easy access to setting per-page meta tags.
17
+ email: merb-meta@coryodaniel.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - LICENSE
25
+ - TODO
26
+ files:
27
+ - LICENSE
28
+ - README
29
+ - Rakefile
30
+ - TODO
31
+ - lib/merb_meta
32
+ - lib/merb_meta.rb
33
+ - spec/merb_meta_spec.rb
34
+ - spec/spec_helper.rb
35
+ has_rdoc: true
36
+ homepage: http://merbivore.com/
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project: merb
57
+ rubygems_version: 1.2.0
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: A plugin for the Merb framework that provides easy access to setting per-page meta tags.
61
+ test_files: []
62
+