tophat 1.6.1 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/tophat.rb CHANGED
@@ -1,24 +1,33 @@
1
1
  require 'action_view'
2
+ require 'tophat/html'
3
+ require 'tophat/title'
4
+ require 'tophat/meta'
5
+ require 'tophat/stylesheet'
6
+ require 'tophat/robots'
7
+ require 'tophat/opengraph'
8
+ require 'tophat/twitter_card'
2
9
 
3
10
  module TopHat
4
11
  extend self
5
12
 
6
- def current
7
- return Thread.current[:tophat] if Thread.current[:tophat]
13
+ require "tophat/railtie" if defined?(::Rails)
8
14
 
9
- reset
15
+ def current
16
+ Thread.current[:tophat] ||= {}
10
17
  end
11
18
 
12
19
  def reset
13
20
  Thread.current[:tophat] = {}
14
21
  end
15
22
 
16
- end
23
+ def setup
24
+ ActionView::Base.send :include, TopHat::HtmlHelper
25
+ ActionView::Base.send :include, TopHat::TitleHelper
26
+ ActionView::Base.send :include, TopHat::MetaHelper
27
+ ActionView::Base.send :include, TopHat::StylesheetHelper
28
+ ActionView::Base.send :include, TopHat::RobotsHelper
29
+ ActionView::Base.send :include, TopHat::OpenGraphHelper
30
+ ActionView::Base.send :include, TopHat::TwitterCardHelper
31
+ end
17
32
 
18
- require 'tophat/html'
19
- require 'tophat/title'
20
- require 'tophat/meta'
21
- require 'tophat/stylesheet'
22
- require 'tophat/robots'
23
- require 'tophat/opengraph'
24
- require 'tophat/twitter_card'
33
+ end
data/lib/tophat/html.rb CHANGED
@@ -12,10 +12,26 @@ module TopHat
12
12
  end
13
13
  end
14
14
 
15
+ if options[:prefix]
16
+ if options[:prefix].kind_of?(Hash)
17
+ prefix_options = options.delete(:prefix)
18
+ options['prefix'] = "#{prefix_options[:prefix]}: #{prefix_options[:url]}"
19
+ elsif options[:prefix].kind_of?(Array)
20
+ prefixes = []
21
+ options.delete(:prefix).each do |prefix|
22
+ if prefix.kind_of?(Hash)
23
+ prefixes << "#{prefix[:prefix]}: #{prefix[:url]}"
24
+ else
25
+ prefixes << prefix
26
+ end
27
+ end
28
+
29
+ options['prefix'] = prefixes.join(' ')
30
+ end
31
+ end
32
+
15
33
  tag(:html, options, true)
16
34
  end
17
35
 
18
36
  end
19
37
  end
20
-
21
- ActionView::Base.send :include, TopHat::HtmlHelper
data/lib/tophat/meta.rb CHANGED
@@ -1,6 +1,19 @@
1
1
  module TopHat
2
2
  module MetaHelper
3
3
 
4
+ # Meta Tag helper
5
+ def meta_tag(options, open=false, escape=true)
6
+ tag(:meta, options, open, escape)
7
+ end
8
+
9
+ def charset(charset, options={})
10
+ meta_tag(options.merge(:charset => charset), true)
11
+ end
12
+
13
+ def viewport(viewport, options={})
14
+ meta_tag(options.merge(:name => 'viewport', :content => viewport), true)
15
+ end
16
+
4
17
  # page descriptions
5
18
  # <meta name="description" content="Description goes here." />
6
19
  def description(options={})
@@ -9,10 +22,11 @@ module TopHat
9
22
  TopHat.current['description'] = options
10
23
 
11
24
  else
25
+ default_description = options.delete(:default)
12
26
  options[:name] = 'description'
13
- options[:content] = TopHat.current['description'] || options.delete(:default)
27
+ options[:content] = TopHat.current['description'] || default_description
14
28
 
15
- tag(:meta, options) if options[:content]
29
+ meta_tag(options) if options[:content]
16
30
  end
17
31
  end
18
32
 
@@ -39,10 +53,8 @@ module TopHat
39
53
  display_keywords += default_keywords if options.delete(:merge_default) == true
40
54
 
41
55
  options.merge!(:content => display_keywords.uniq.join(', ').squeeze(' '))
42
- tag(:meta, options) if display_keywords.any?
56
+ meta_tag(options) if display_keywords.any?
43
57
  end
44
58
  end
45
59
  end
46
60
  end
47
-
48
- ActionView::Base.send :include, TopHat::MetaHelper
@@ -106,5 +106,3 @@ module TopHat
106
106
 
107
107
  end
108
108
  end
109
-
110
- ActionView::Base.send :include, TopHat::OpenGraphHelper
@@ -0,0 +1,20 @@
1
+ require 'tophat'
2
+ require 'tophat/reset'
3
+ require 'rails'
4
+
5
+ module TopHat
6
+ class Railtie < Rails::Railtie
7
+
8
+ initializer "tophat.view_helpers" do |app|
9
+ TopHat.setup
10
+ end
11
+
12
+ # Clear the identity map after each request
13
+ initializer "tophat.reset" do |app|
14
+ ActiveSupport.on_load(:action_controller) do
15
+ include(TopHat::Reset)
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ module TopHat
2
+ module Reset
3
+
4
+ def self.included(base)
5
+ base.append_before_filter :reset_tophat
6
+ end
7
+
8
+ def reset_tophat
9
+ puts 'i am getting reset'
10
+ TopHat.reset
11
+ end
12
+
13
+ end
14
+ end
data/lib/tophat/robots.rb CHANGED
@@ -19,5 +19,3 @@ module TopHat
19
19
 
20
20
  end
21
21
  end
22
-
23
- ActionView::Base.send :include, TopHat::RobotsHelper
@@ -83,5 +83,3 @@ module TopHat
83
83
 
84
84
  end
85
85
  end
86
-
87
- ActionView::Base.send :include, TopHat::StylesheetHelper
data/lib/tophat/title.rb CHANGED
@@ -67,5 +67,3 @@ module TopHat
67
67
  end
68
68
  end
69
69
  end
70
-
71
- ActionView::Base.send :include, TopHat::TitleHelper
@@ -51,5 +51,3 @@ module TopHat
51
51
  end
52
52
  end
53
53
  end
54
-
55
- ActionView::Base.send :include, TopHat::TwitterCardHelper
@@ -1,3 +1,3 @@
1
1
  module TopHat
2
- VERSION = '1.6.1'
2
+ VERSION = '1.7.0'
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -11,6 +11,10 @@ require 'rails/all'
11
11
 
12
12
 
13
13
  RSpec.configure do |config|
14
+ config.before(:suite) do
15
+ TopHat.setup
16
+ end
17
+
14
18
  config.after(:each) do
15
19
  TopHat.reset
16
20
  end
@@ -15,15 +15,43 @@ describe TopHat::HtmlHelper do
15
15
  output.should eq('<html version="123">')
16
16
  end
17
17
 
18
- it 'accepts xmlns in an options hash' do
19
- output = @template.html_tag(:xmlns => 'http://someurl.com')
20
- output.should eq('<html xmlns="http://someurl.com">')
18
+ context 'xmlns' do
19
+ it 'accepts xmlns passed as strings' do
20
+ output = @template.html_tag(:xmlns => 'http://someurl.com')
21
+ output.should eq('<html xmlns="http://someurl.com">')
22
+ end
23
+
24
+ it 'accepts xmlns passed as an array of hashes' do
25
+ xmlns = { :prefix => 'fb', :url => 'http://developers.facebook.com/schema/' }
26
+ output = @template.html_tag(:xmlns => [xmlns])
27
+ output.should eq('<html xmlns:fb="http://developers.facebook.com/schema/">')
28
+ end
21
29
  end
22
30
 
23
- it 'accepts an array of xmlns including prefixes' do
24
- xmlns = { :prefix => 'fb', :url => 'http://developers.facebook.com/schema/' }
25
- output = @template.html_tag(:xmlns => [xmlns])
26
- output.should eq('<html xmlns:fb="http://developers.facebook.com/schema/">')
31
+ context 'prefixes' do
32
+ it 'accepts prefixes passed as strings' do
33
+ output = @template.html_tag(:prefix => 'ohai')
34
+ output.should eq('<html prefix="ohai">')
35
+ end
36
+
37
+ it 'accepts prefixes passed as a hash' do
38
+ output = @template.html_tag(:prefix => { :prefix => 'og', :url => 'http://ogp.me/ns#' })
39
+ output.should eq('<html prefix="og: http://ogp.me/ns#">')
40
+ end
41
+
42
+ it 'accepts prefixes passed as an array of hashes' do
43
+ prefixes = [
44
+ { :prefix => 'og', :url => 'http://ogp.me/ns#' },
45
+ { :prefix => 'fb', :url => 'http://ogp.me/ns/fb#' }
46
+ ]
47
+ output = @template.html_tag(:prefix => prefixes)
48
+ output.should eq('<html prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#">')
49
+ end
50
+
51
+ it 'accepts prefixes passes and an array of strings' do
52
+ output = @template.html_tag(:prefix => ['og: http://ogp.me/ns#', 'fb: http://ogp.me/ns/fb#'])
53
+ output.should eq('<html prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#">')
54
+ end
27
55
  end
28
56
 
29
57
  end
@@ -6,6 +6,24 @@ describe TopHat::MetaHelper do
6
6
  @template = ActionView::Base.new
7
7
  end
8
8
 
9
+ describe "charset" do
10
+ it 'renders a meta tag with a charset' do
11
+ @template.charset('utf-8').should eq('<meta charset="utf-8">')
12
+ end
13
+ end
14
+
15
+ describe "viewport" do
16
+ it 'renders a meta tag with a viewport' do
17
+ @template.viewport('width=device-width').should eq('<meta content="width=device-width" name="viewport">')
18
+ end
19
+ end
20
+
21
+ describe "meta_tag" do
22
+ it 'renders meta_tags' do
23
+ @template.meta_tag(:charset => 'utf-8').should eq('<meta charset="utf-8" />')
24
+ end
25
+ end
26
+
9
27
  describe "keywords" do
10
28
  context "defined as an array" do
11
29
  before do
@@ -67,6 +85,11 @@ describe TopHat::MetaHelper do
67
85
  it "returns nil when no default is configured and no description is defined" do
68
86
  @template.description.should be_nil
69
87
  end
88
+
89
+ it 'overrides the default' do
90
+ @template.description('This is a custom description')
91
+ @template.description(:default => 'This is a default description.').should eq('<meta content="This is a custom description" name="description" />')
92
+ end
70
93
  end
71
94
 
72
95
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tophat
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 1.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-18 00:00:00.000000000Z
12
+ date: 2012-07-24 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
16
- requirement: &70282580984540 !ruby/object:Gem::Requirement
16
+ requirement: &70142323594740 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70282580984540
24
+ version_requirements: *70142323594740
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70282580982080 !ruby/object:Gem::Requirement
27
+ requirement: &70142323593260 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70282580982080
35
+ version_requirements: *70142323593260
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70282580979740 !ruby/object:Gem::Requirement
38
+ requirement: &70142323591560 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70282580979740
46
+ version_requirements: *70142323591560
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: yard
49
- requirement: &70282580978720 !ruby/object:Gem::Requirement
49
+ requirement: &70142323590340 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70282580978720
57
+ version_requirements: *70142323590340
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rdiscount
60
- requirement: &70282580975360 !ruby/object:Gem::Requirement
60
+ requirement: &70142323588840 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70282580975360
68
+ version_requirements: *70142323588840
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: simplecov
71
- requirement: &70282580971680 !ruby/object:Gem::Requirement
71
+ requirement: &70142323587760 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70282580971680
79
+ version_requirements: *70142323587760
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rails
82
- requirement: &70282580969740 !ruby/object:Gem::Requirement
82
+ requirement: &70142323586680 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 3.0.0
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70282580969740
90
+ version_requirements: *70142323586680
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: guard-rspec
93
- requirement: &70282580968820 !ruby/object:Gem::Requirement
93
+ requirement: &70142323585960 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70282580968820
101
+ version_requirements: *70142323585960
102
102
  description: Simple view helpers for your layouts
103
103
  email:
104
104
  - steve.agalloco@gmail.com
@@ -119,6 +119,8 @@ files:
119
119
  - lib/tophat/html.rb
120
120
  - lib/tophat/meta.rb
121
121
  - lib/tophat/opengraph.rb
122
+ - lib/tophat/railtie.rb
123
+ - lib/tophat/reset.rb
122
124
  - lib/tophat/robots.rb
123
125
  - lib/tophat/stylesheet.rb
124
126
  - lib/tophat/title.rb