tophat 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,19 @@
1
1
  require 'action_view'
2
- require 'tophat/version'
3
- require 'tophat/core_extensions'
4
2
  require 'tophat/title'
5
3
  require 'tophat/meta'
6
4
  require 'tophat/stylesheet'
5
+ require 'tophat/robots'
7
6
 
8
- Hash.send :include, TopHat::HashOnly unless Hash.instance_methods.include?("only")
7
+ module TopHat
8
+ autoload :TitleHelper, 'tophat/title'
9
+ autoload :MetaHelper, 'tophat/meta'
10
+ autoload :StylesheetHelper, 'tophat/stylesheet'
11
+ autoload :RobotsHelper, 'tophat/robots'
12
+ autoload :OpenGraphHelper, 'tophat/opengraph'
13
+ end
9
14
 
10
15
  ActionView::Base.send :include, TopHat::TitleHelper
11
16
  ActionView::Base.send :include, TopHat::MetaHelper
12
17
  ActionView::Base.send :include, TopHat::StylesheetHelper
18
+ ActionView::Base.send :include, TopHat::RobotsHelper
19
+ ActionView::Base.send :include, TopHat::OpenGraphHelper
@@ -1,16 +1,6 @@
1
1
  module TopHat
2
2
  module MetaHelper
3
3
 
4
- def meta_tag(options={})
5
- # tag :meta, :name => options[:name], :content => options[:content]
6
- if options[:content] && (options[:name] || options[:http_equiv])
7
- t = "<meta "
8
- t << "name=\"#{options[:name]}\"" if options[:name]
9
- t << "http-equiv=\"#{options[:http_equiv]}\"" if options[:http_equiv]
10
- t << " content=\"#{options[:content]}\" />"
11
- end
12
- end
13
-
14
4
  # page descriptions
15
5
  # <meta name="description" content="Description goes here." />
16
6
  def description(options=nil)
@@ -28,7 +18,7 @@ module TopHat
28
18
  options.merge!(:content => @tophat_description)
29
19
  end
30
20
 
31
- meta_tag(options) if options[:content]
21
+ tag(:meta, options) if options[:content]
32
22
  end
33
23
  end
34
24
 
@@ -56,7 +46,7 @@ module TopHat
56
46
  display_keywords += default_keywords if options.delete(:merge_default) == true
57
47
 
58
48
  options.merge!(:content => display_keywords.uniq.join(', '))
59
- meta_tag(options) if display_keywords.any?
49
+ tag(:meta, options) if display_keywords.any?
60
50
  end
61
51
  end
62
52
  end
@@ -0,0 +1,75 @@
1
+ module TopHat
2
+ module OpenGraphHelper
3
+
4
+ class OpenGraphGenerator
5
+ include ActionView::Helpers
6
+
7
+ def initialize(options={})
8
+ @app_id = options.delete(:app_id) if options && options.has_key?(:app_id)
9
+ @admins = options.delete(:admins) if options && options.has_key?(:admins)
10
+ @graph_data = {}
11
+ end
12
+
13
+ def merge(options={})
14
+ @app_id = options.delete(:app_id) if options && options.has_key?(:app_id)
15
+ @admins = options.delete(:admins) if options && options.has_key?(:admins)
16
+ end
17
+
18
+ def app_id
19
+ output = @app_id ? tag(:meta, :property => 'fb:app_id', :content => @app_id) : ""
20
+ output << '\n' unless output.blank?
21
+ output
22
+ end
23
+
24
+ def admins
25
+ output = @admins ? tag(:meta, :property => 'fb:admins', :content => [*@admins].join(',')) : ""
26
+ output << '\n' unless output.blank?
27
+ output
28
+ end
29
+
30
+ def render_graph_data
31
+ output = ""
32
+ @graph_data.each_pair do |key, value|
33
+ output << tag(:meta, :property => "og:#{key}", :content => value)
34
+ output << '\n' if @graph_data.size > 1
35
+ end
36
+ output
37
+ end
38
+
39
+ def type(t)
40
+ @graph_data ||= {}
41
+ @graph_data[:type] = t
42
+ end
43
+
44
+ def has_graph_data?
45
+ @graph_data
46
+ end
47
+
48
+ def method_missing(method, *args, &block) #:nodoc
49
+ @graph_data ||= {}
50
+ @graph_data[method] = args.shift
51
+ end
52
+
53
+ end
54
+
55
+ def opengraph(options=nil, &block)
56
+ if options.kind_of? Hash
57
+ @tophat_open_graph_defaults = options
58
+ end
59
+ if block_given?
60
+ @tophat_open_graph_generator = OpenGraphGenerator.new(@tophat_open_graph_defaults)
61
+ yield(@tophat_open_graph_generator)
62
+ else
63
+ @tophat_open_graph_generator ||= OpenGraphGenerator.new
64
+ @tophat_open_graph_generator.merge(@tophat_open_graph_defaults)
65
+ output = ""
66
+ output << @tophat_open_graph_generator.app_id
67
+ output << @tophat_open_graph_generator.admins
68
+ output << @tophat_open_graph_generator.render_graph_data if @tophat_open_graph_generator.has_graph_data?
69
+ output
70
+ end
71
+ end
72
+
73
+ end
74
+ end
75
+
@@ -0,0 +1,19 @@
1
+ module TopHat
2
+ module RobotsHelper
3
+
4
+ def noindex(descriptor='robots')
5
+ tag(:meta, :name => descriptor, :content => 'noindex')
6
+ end
7
+
8
+ def nofollow(descriptor='robots')
9
+ tag(:meta, :name => descriptor, :content => 'nofollow')
10
+ end
11
+
12
+ def canonical(path=nil)
13
+ if path
14
+ tag(:link, :rel => 'canonical', :href => path)
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -66,6 +66,7 @@ module TopHat
66
66
  def browser_conditional(browser, version = nil, operator = nil, &block)
67
67
  unless operator.blank?
68
68
  operator = operator.to_s
69
+ operator = '!' if operator == 'not'
69
70
  operator << " " unless operator == '!'
70
71
  end
71
72
 
@@ -1,3 +1,3 @@
1
1
  module TopHat
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -0,0 +1,88 @@
1
+ require 'helper'
2
+
3
+ class TopHatOpenGraphTestCase < Test::Unit::TestCase
4
+
5
+ context "when using the open graph helpers" do
6
+
7
+ setup do
8
+ @template = ActionView::Base.new
9
+ end
10
+
11
+ context "site admins when configured" do
12
+
13
+ context "as a string" do
14
+
15
+ should "generate a site admin tag" do
16
+ @template.opengraph(:admins => '123,124')
17
+ assert_equal @template.opengraph, '<meta content="123,124" property="fb:admins" />\n'
18
+ end
19
+
20
+ end
21
+
22
+ context "as an array" do
23
+
24
+ should "generate a site admin tag" do
25
+ @template.opengraph(:admins => [123, 124])
26
+ assert_equal @template.opengraph, '<meta content="123,124" property="fb:admins" />\n'
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
33
+ context "app_id when configured" do
34
+
35
+ should "generate an app_id meta tag" do
36
+ @template.opengraph(:app_id => 'MyApp')
37
+ assert_equal @template.opengraph, '<meta content="MyApp" property="fb:app_id" />\n'
38
+ end
39
+
40
+ end
41
+
42
+ context "additional open graph properties" do
43
+
44
+ should "generate tags" do
45
+ @template.opengraph do |graph|
46
+ graph.title 'The Great Gatsby'
47
+ end
48
+ assert_equal @template.opengraph, '<meta content="The Great Gatsby" property="og:title" />'
49
+ end
50
+
51
+ should "allow use of the tag 'type'" do
52
+ @template.opengraph do |graph|
53
+ graph.type 'sports_team'
54
+ end
55
+ assert_equal @template.opengraph, '<meta content="sports_team" property="og:type" />'
56
+ end
57
+
58
+ should "support multiple tags" do
59
+ @template.opengraph do |graph|
60
+ graph.title 'Austin Powers: International Man of Mystery'
61
+ graph.type 'movie'
62
+ end
63
+ assert_equal @template.opengraph, '<meta content="movie" property="og:type" />\n<meta content="Austin Powers: International Man of Mystery" property="og:title" />\n'
64
+ end
65
+
66
+ end
67
+
68
+ context "combined usage" do
69
+ should "generate all tags" do
70
+ @template.opengraph(:app_id => 'MyApp', :admins => [123, 1234]) do |graph|
71
+ graph.title 'Rain Man'
72
+ graph.type 'movie'
73
+ end
74
+ assert_equal @template.opengraph, '<meta content="MyApp" property="fb:app_id" />\n<meta content="123,1234" property="fb:admins" />\n<meta content="movie" property="og:type" />\n<meta content="Rain Man" property="og:title" />\n'
75
+ end
76
+
77
+ should "generate all tags - alternative usage" do
78
+ @template.opengraph do |graph|
79
+ graph.title 'Rain Man'
80
+ graph.type 'movie'
81
+ end
82
+ assert_equal @template.opengraph(:app_id => 'MyApp', :admins => [123, 1234]), '<meta content="MyApp" property="fb:app_id" />\n<meta content="123,1234" property="fb:admins" />\n<meta content="movie" property="og:type" />\n<meta content="Rain Man" property="og:title" />\n'
83
+ end
84
+ end
85
+
86
+ end
87
+
88
+ end
@@ -0,0 +1,54 @@
1
+ require 'helper'
2
+
3
+ class TopHatRobotsTestCase < Test::Unit::TestCase
4
+
5
+ context "using the meta helper" do
6
+
7
+ setup do
8
+ @template = ActionView::Base.new
9
+ end
10
+
11
+ context "robots" do
12
+
13
+ context "nofollow" do
14
+
15
+ should "default to all robots" do
16
+ assert_equal @template.nofollow, "<meta content=\"nofollow\" name=\"robots\" />"
17
+ end
18
+
19
+ should "use a descriptor if one is provided" do
20
+ assert_equal @template.nofollow('googlebot'), "<meta content=\"nofollow\" name=\"googlebot\" />"
21
+ end
22
+
23
+ end
24
+
25
+ context "noindex" do
26
+
27
+ should "default to all robots" do
28
+ assert_equal @template.noindex, "<meta content=\"noindex\" name=\"robots\" />"
29
+ end
30
+
31
+ should "use a descriptor if one is provided" do
32
+ assert_equal @template.noindex('googlebot'), "<meta content=\"noindex\" name=\"googlebot\" />"
33
+ end
34
+
35
+ end
36
+
37
+ context "canonical" do
38
+
39
+ should "not render when not passed a path" do
40
+ assert_nil @template.canonical
41
+ end
42
+
43
+ should "render a tag when passed a path" do
44
+ assert_equal @template.canonical('http://mysite.com/somepath/'), "<link href=\"http://mysite.com/somepath/\" rel=\"canonical\" />"
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+
52
+ end
53
+
54
+ end
@@ -21,7 +21,7 @@ class TopHatMetaTestCase < Test::Unit::TestCase
21
21
  end
22
22
 
23
23
  should "use default keywords if keywords is empty" do
24
- assert_equal @template.keywords(:default => @keywords), "<meta name=\"keywords\" content=\"#{@keywords.join(', ')}\" />"
24
+ assert_equal @template.keywords(:default => @keywords), "<meta content=\"#{@keywords.join(', ')}\" name=\"keywords\" />"
25
25
  end
26
26
 
27
27
  end
@@ -37,7 +37,7 @@ class TopHatMetaTestCase < Test::Unit::TestCase
37
37
  end
38
38
 
39
39
  should "use default keywords passed as a string if keywords is empty" do
40
- assert_equal @template.keywords(:default => @keywords), "<meta name=\"keywords\" content=\"#{@keywords}\" />"
40
+ assert_equal @template.keywords(:default => @keywords), "<meta content=\"#{@keywords}\" name=\"keywords\" />"
41
41
  end
42
42
 
43
43
  end
@@ -48,7 +48,7 @@ class TopHatMetaTestCase < Test::Unit::TestCase
48
48
 
49
49
  should "merge default tags with page tags, when merge_default is set to true" do
50
50
  @template.keywords("Stu, Pete")
51
- assert_equal @template.keywords(:default => "John, Paul, George, Ringo", :merge_default => true), "<meta name=\"keywords\" content=\"Stu, Pete, John, Paul, George, Ringo\" />"
51
+ assert_equal @template.keywords(:default => "John, Paul, George, Ringo", :merge_default => true), "<meta content=\"Stu, Pete, John, Paul, George, Ringo\" name=\"keywords\" />"
52
52
  end
53
53
 
54
54
  end
@@ -62,7 +62,7 @@ class TopHatMetaTestCase < Test::Unit::TestCase
62
62
 
63
63
  should "use the default description if no description is defined" do
64
64
  desc = "A flute without holes, is not a flute. A donut without a hole, is a Danish."
65
- assert_equal @template.description(:default => desc), "<meta name=\"description\" content=\"#{desc}\" />"
65
+ assert_equal @template.description(:default => desc), "<meta content=\"#{desc}\" name=\"description\" />"
66
66
  end
67
67
 
68
68
  should "not return a tag if no default is configured and no description is defined" do
@@ -71,20 +71,6 @@ class TopHatMetaTestCase < Test::Unit::TestCase
71
71
 
72
72
  end
73
73
 
74
- context "meta" do
75
-
76
- should "render properly" do
77
- assert_equal @template.meta_tag(:name => 'viewport', :content => 'width=device-width'), "<meta name=\"viewport\" content=\"width=device-width\" />"
78
- assert_equal @template.meta_tag(:name => 'robots', :content => 'all'), "<meta name=\"robots\" content=\"all\" />"
79
- assert_equal @template.meta_tag(:http_equiv => 'Content-Language', :content => 'en-us'), "<meta http-equiv=\"Content-Language\" content=\"en-us\" />"
80
- end
81
-
82
- should "not render when no arguments are passed" do
83
- assert_nil @template.meta_tag
84
- end
85
-
86
- end
87
-
88
74
  end
89
75
 
90
76
  end
@@ -55,6 +55,16 @@ class TopHatStylesheetTestCase < Test::Unit::TestCase
55
55
  }, "<!--[if gte IE 5.5]>\n<link href=\"/stylesheets/ie.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />\n<![endif]-->"
56
56
  end
57
57
 
58
+ should "render defined IE conditional with ! operator" do
59
+ assert_equal @template.ie_5_conditional(:not) {
60
+ @template.stylesheet_link_tag(@stylesheet)
61
+ }, "<!--[if !IE 5]>\n<link href=\"/stylesheets/ie.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />\n<![endif]-->"
62
+
63
+ assert_equal @template.ie_5_5_conditional(:not) {
64
+ @template.stylesheet_link_tag(@stylesheet)
65
+ }, "<!--[if !IE 5.5]>\n<link href=\"/stylesheets/ie.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />\n<![endif]-->"
66
+ end
67
+
58
68
  should "render defined IE conditional with less than operator" do
59
69
  assert_equal @template.ie_5_conditional(:lt) {
60
70
  @template.stylesheet_link_tag(@stylesheet)
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{tophat}
8
- s.version = "1.1.0"
8
+ s.version = "1.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Steve Agalloco"]
12
- s.date = %q{2010-05-04}
12
+ s.date = %q{2010-08-29}
13
13
  s.description = %q{simple view helpers for your layouts}
14
14
  s.email = %q{steve.agalloco@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -23,13 +23,16 @@ Gem::Specification.new do |s|
23
23
  "README.textile",
24
24
  "Rakefile",
25
25
  "lib/tophat.rb",
26
- "lib/tophat/core_extensions.rb",
27
26
  "lib/tophat/meta.rb",
27
+ "lib/tophat/opengraph.rb",
28
+ "lib/tophat/robots.rb",
28
29
  "lib/tophat/stylesheet.rb",
29
30
  "lib/tophat/title.rb",
30
31
  "lib/tophat/version.rb",
31
32
  "rails/init.rb",
32
33
  "test/helper.rb",
34
+ "test/test_opengraph.rb",
35
+ "test/test_robots.rb",
33
36
  "test/test_tophat.rb",
34
37
  "test/test_tophat_meta.rb",
35
38
  "test/test_tophat_stylesheets.rb",
@@ -39,10 +42,12 @@ Gem::Specification.new do |s|
39
42
  s.homepage = %q{http://github.com/spagalloco/tophat}
40
43
  s.rdoc_options = ["--charset=UTF-8"]
41
44
  s.require_paths = ["lib"]
42
- s.rubygems_version = %q{1.3.6}
45
+ s.rubygems_version = %q{1.3.7}
43
46
  s.summary = %q{simple view helpers for your layouts}
44
47
  s.test_files = [
45
48
  "test/helper.rb",
49
+ "test/test_opengraph.rb",
50
+ "test/test_robots.rb",
46
51
  "test/test_tophat.rb",
47
52
  "test/test_tophat_meta.rb",
48
53
  "test/test_tophat_stylesheets.rb",
@@ -53,7 +58,7 @@ Gem::Specification.new do |s|
53
58
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
54
59
  s.specification_version = 3
55
60
 
56
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
61
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
57
62
  s.add_runtime_dependency(%q<actionpack>, [">= 2.3.5"])
58
63
  s.add_development_dependency(%q<shoulda>, [">= 0"])
59
64
  else
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tophat
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 31
4
5
  prerelease: false
5
6
  segments:
6
7
  - 1
7
- - 1
8
+ - 2
8
9
  - 0
9
- version: 1.1.0
10
+ version: 1.2.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Steve Agalloco
@@ -14,16 +15,18 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-05-04 00:00:00 -04:00
18
+ date: 2010-08-29 00:00:00 -04:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: actionpack
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 9
27
30
  segments:
28
31
  - 2
29
32
  - 3
@@ -35,9 +38,11 @@ dependencies:
35
38
  name: shoulda
36
39
  prerelease: false
37
40
  requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
38
42
  requirements:
39
43
  - - ">="
40
44
  - !ruby/object:Gem::Version
45
+ hash: 3
41
46
  segments:
42
47
  - 0
43
48
  version: "0"
@@ -59,13 +64,16 @@ files:
59
64
  - README.textile
60
65
  - Rakefile
61
66
  - lib/tophat.rb
62
- - lib/tophat/core_extensions.rb
63
67
  - lib/tophat/meta.rb
68
+ - lib/tophat/opengraph.rb
69
+ - lib/tophat/robots.rb
64
70
  - lib/tophat/stylesheet.rb
65
71
  - lib/tophat/title.rb
66
72
  - lib/tophat/version.rb
67
73
  - rails/init.rb
68
74
  - test/helper.rb
75
+ - test/test_opengraph.rb
76
+ - test/test_robots.rb
69
77
  - test/test_tophat.rb
70
78
  - test/test_tophat_meta.rb
71
79
  - test/test_tophat_stylesheets.rb
@@ -81,28 +89,34 @@ rdoc_options:
81
89
  require_paths:
82
90
  - lib
83
91
  required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
84
93
  requirements:
85
94
  - - ">="
86
95
  - !ruby/object:Gem::Version
96
+ hash: 3
87
97
  segments:
88
98
  - 0
89
99
  version: "0"
90
100
  required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
91
102
  requirements:
92
103
  - - ">="
93
104
  - !ruby/object:Gem::Version
105
+ hash: 3
94
106
  segments:
95
107
  - 0
96
108
  version: "0"
97
109
  requirements: []
98
110
 
99
111
  rubyforge_project:
100
- rubygems_version: 1.3.6
112
+ rubygems_version: 1.3.7
101
113
  signing_key:
102
114
  specification_version: 3
103
115
  summary: simple view helpers for your layouts
104
116
  test_files:
105
117
  - test/helper.rb
118
+ - test/test_opengraph.rb
119
+ - test/test_robots.rb
106
120
  - test/test_tophat.rb
107
121
  - test/test_tophat_meta.rb
108
122
  - test/test_tophat_stylesheets.rb
@@ -1,13 +0,0 @@
1
- module TopHat
2
- module HashOnly
3
- # Returns a new hash with only the given keys.
4
- def only(*keys)
5
- reject {|key, value| !keys.include?(key) }
6
- end
7
-
8
- # Replaces the hash without only the given keys.
9
- def only!(*keys)
10
- replace(only(*keys))
11
- end
12
- end
13
- end