metamagic 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/metamagic.rb CHANGED
@@ -1,4 +1,2 @@
1
1
  require 'metamagic/helper_methods'
2
-
3
- ActionController::Base.send :include, Metamagic::HelperMethods
4
2
  ActionView::Base.send :include, Metamagic::HelperMethods
@@ -1,38 +1,52 @@
1
1
  module Metamagic
2
2
  module HelperMethods
3
- def meta(options = {})
4
- @meta_tags = options
3
+ def meta_tags
4
+ @meta_tags ||= []
5
+ end
6
+
7
+ def meta(*options)
8
+ # add page specific meta tags
9
+ add_meta_tags options
10
+ end
11
+
12
+ def add_tag_if_not_existing(new_tag)
13
+ # add meta tag if it's not existing
14
+ unless meta_tags.find { |tag| tag[:name] && new_tag[:name] && tag[:name] == new_tag[:name] }
15
+ meta_tags << new_tag
16
+ end
17
+ end
18
+
19
+ def add_meta_tags(options)
20
+ options.each do |option|
21
+ if option.is_a?(Hash)
22
+ option.each_pair do |key, value|
23
+ add_tag_if_not_existing :name => key, :content => value
24
+ end
25
+ elsif option.is_a?(Array)
26
+ option.each do |tag|
27
+ add_tag_if_not_existing tag
28
+ end
29
+ else
30
+ raise TypeError, "Unknown tag type #{tag.class.name}. Use either Hash or Array."
31
+ end
32
+ end
5
33
  end
6
34
 
7
- def metamagic(options = {})
8
- options = @meta_tags || options
9
-
35
+ def metamagic(*options)
36
+ # apply default meta tags if they don't exist
37
+ add_meta_tags options
38
+
39
+ # loop through the added tags
10
40
  out = []
11
- options.each do |key, value|
12
- if key.to_s == "title"
13
- out << content_tag(:title, value)
41
+ meta_tags.each do |tag|
42
+ if tag[:name] == :title
43
+ out << content_tag(:title, tag[:content])
14
44
  else
15
- props = { :name => key.to_s }
16
-
17
- if value.is_a?(Hash)
18
- props.merge! value
19
- else
20
- props[:content] = value
21
- end
22
-
23
- # loop through each property to find
24
- # arrays that need to be joined
25
- props.each_pair do |propkey, propvalue|
26
- if propvalue.is_a?(Array)
27
- propvalue = propvalue.join(", ")
28
- end
29
- props[propkey] = propvalue
30
- end
31
-
32
- out << tag("meta", props) if value
45
+ out << tag(:meta, tag)
33
46
  end
34
47
  end
35
48
 
49
+ # return tags
36
50
  out.join.html_safe
37
51
  end
38
52
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metamagic
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
- - 1
7
+ - 2
8
8
  - 0
9
- - 1
10
- version: 1.0.1
9
+ - 0
10
+ version: 2.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lasse Bunk
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-05-27 00:00:00 Z
18
+ date: 2012-05-28 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: Metamagic is a simple Ruby on Rails plugin for creating meta tags.