helperful 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ #
2
+ # = Helperful
3
+ #
4
+ # A collection of useful Rails helpers.
5
+ #
6
+ #
7
+ # Category:: Rails
8
+ # Package:: Helperful
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ # Copyright:: 2008-2009 The Authors
11
+ # License:: MIT License
12
+ #
13
+ #--
14
+ #
15
+ #++
16
+
17
+
18
+ module Helperful
19
+
20
+ #
21
+ # = Affiliations Helper
22
+ #
23
+ # Provides a set of helpers for working with online affiliations.
24
+ #
25
+ module AffiliationsHelper
26
+
27
+ #
28
+ # Returns the tradedoubler site verification tag for +publisher_id+.
29
+ #
30
+ # tradedoubler_verification_tag('123456')
31
+ # # => "<! -- TradeDoubler site verification 123456 -->"
32
+ #
33
+ # tradedoubler_verification_tag(123456)
34
+ # # => "<! -- TradeDoubler site verification 123456 -->"
35
+ #
36
+ # tradedoubler_verification_tag(nil)
37
+ # # => "<! -- TradeDoubler site verification -->"
38
+ #
39
+ def tradedoubler_verification_tag(publisher_id)
40
+ "<!-- TradeDoubler site verification #{publisher_id.to_s} -->"
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,105 @@
1
+ #
2
+ # = Helperful
3
+ #
4
+ # A collection of useful Rails helpers.
5
+ #
6
+ #
7
+ # Category:: Rails
8
+ # Package:: Helperful
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ # Copyright:: 2008-2009 The Authors
11
+ # License:: MIT License
12
+ #
13
+ #--
14
+ #
15
+ #++
16
+
17
+
18
+ module Helperful
19
+
20
+ #
21
+ # = Asset Tag Helper
22
+ #
23
+ # Provides a set of helpers for generating HTML that links views to assets such
24
+ # as images, javascripts, stylesheets, and feeds.
25
+ #
26
+ # ==== Requirements
27
+ #
28
+ # The following requirements are mandatory for this module.
29
+ # Including this helper will automatically include them unless already included.
30
+ #
31
+ # * ActionView::Helpers::AssetTagHelper
32
+ #
33
+ module AssetTagHelper
34
+
35
+ def self.included(base)
36
+ base.class_eval do
37
+ base.included_modules.include?(ActionView::Helpers::AssetTagHelper) || include(ActionView::Helpers::AssetTagHelper)
38
+ end
39
+ end
40
+
41
+ # Returns a link tag that search engine crawlers may use to determine
42
+ # the preferred version of a page with multiple URLs.
43
+ #
44
+ # More details about the canonical link tag are available
45
+ # at the following sources:
46
+ # * http://www.mattcutts.com/blog/canonical-link-tag/
47
+ # * http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html
48
+ #
49
+ # Control the link options in url_for format using the +url_options+.
50
+ #
51
+ # ==== Examples
52
+ #
53
+ # canonical_link_tag
54
+ # # => <link rel="canonical" href=http://www.currenthost.com/controller/action" />
55
+ # canonical_link_tag(:action => "show")
56
+ # # => <link rel="canonical" href=http://www.currenthost.com/controller/show" />
57
+ # canonical_link_tag("http://www.example.com/product.php?item=swedish-fish")
58
+ # # => <link rel="canonical" href="http://www.example.com/product.php?item=swedish-fish" />
59
+ #
60
+ def canonical_link_tag(url_options = {})
61
+ tag(
62
+ "link",
63
+ "rel" => "canonical",
64
+ "href" => url_options.is_a?(Hash) ? url_for(url_options.merge(:only_path => false)) : url_options
65
+ )
66
+ end
67
+
68
+ # Converts given +files+ in javascript include statements
69
+ # and appends them in the head section of the page.
70
+ #
71
+ # ==== Examples
72
+ #
73
+ # # in your index.html.erb view
74
+ # <% javascript :foo, :bar, :cache => "foobar" %>
75
+ #
76
+ # # this is equivalent to
77
+ # <% content_for :head, javascript_include_tag(:foo, :bar, :cache => "foobar") %>
78
+ #
79
+ # # in your application
80
+ # <% yield :head %>
81
+ #
82
+ def javascript(*files)
83
+ content_for(:head, javascript_include_tag(*files))
84
+ end
85
+
86
+ # Converts given +files+ in stylesheet link statements
87
+ # and appends them in the head section of the page.
88
+ #
89
+ # ==== Examples
90
+ #
91
+ # # in your index.html.erb view
92
+ # <% stylesheet :foo, :bar, :cache => "foobar" %>
93
+ #
94
+ # # this is equivalent to
95
+ # <% content_for :head, stylesheet_link_tag(:foo, :bar, :cache => "foobar") %>
96
+ #
97
+ # # in your application
98
+ # <% yield :head %>
99
+ #
100
+ def stylesheet(*files)
101
+ content_for(:head, stylesheet_link_tag(*files))
102
+ end
103
+
104
+ end
105
+ end
@@ -0,0 +1,105 @@
1
+ #
2
+ # = Helperful
3
+ #
4
+ # A collection of useful Rails helpers.
5
+ #
6
+ #
7
+ # Category:: Rails
8
+ # Package:: Helperful
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ # Copyright:: 2008-2009 The Authors
11
+ # License:: MIT License
12
+ #
13
+ #--
14
+ #
15
+ #++
16
+
17
+
18
+ module Helperful
19
+
20
+ #
21
+ # = Content Helper
22
+ #
23
+ # Provides a set of helpers for capturing and working with your page content
24
+ # in a more effective way.
25
+ #
26
+ # ==== Requires
27
+ #
28
+ # The following requirements are mandatory for this module.
29
+ # Including this helper will automatically include them unless already included.
30
+ #
31
+ # * ActionView::Helpers::CaptureHelper
32
+ #
33
+ module ContentHelper
34
+
35
+ def self.included(base)
36
+ base.class_eval do
37
+ base.included_modules.include?(ActionView::Helpers::CaptureHelper) || include(ActionView::Helpers::CaptureHelper)
38
+ alias_method_chain :content_for, :has
39
+ end
40
+ end
41
+
42
+ #
43
+ # Returns <tt>true</tt> if <tt>name</tt> has any content,
44
+ # in other workds if content_for(name) has ever been called.
45
+ #
46
+ # ==== Important
47
+ #
48
+ # Some important details to keep in mind.
49
+ #
50
+ # Due to the way <tt>content_for</tt> stores data, it doesn't matter
51
+ # whether name is a Symbol or a String.
52
+ # They are treated in the same way. The following calls are equivalent.
53
+ #
54
+ # has_content? :foo # => true
55
+ # has_content? "foo" # => true
56
+ #
57
+ # This method doesn't make any difference between an empty,
58
+ # blank or nil content. It always returns <tt>true</tt> if
59
+ # <tt>content_for</tt> has been called with <tt>name</tt>,
60
+ # it doesn't matter which content is stored.
61
+ # A call to <tt>has_content?</tt> will always return true
62
+ # in any of the following cases:
63
+ #
64
+ # content_for :name, nil
65
+ # content_for :name, ''
66
+ # content_for :name, []
67
+ # content_for :name, 'A real content'
68
+ # content_for :name do
69
+ # "nothing here"
70
+ # end
71
+ #
72
+ def has_content?(name)
73
+ @has_content ||= {}
74
+ @has_content.key?(name.to_s)
75
+ end
76
+
77
+ # Flags +name+ to ensure <tt>has_content?</tt> will work as expected.
78
+ # You would probably never call this method directly.
79
+ # In fact, you are encouraged to not do so.
80
+ def has_content!(name) # :nodoc:
81
+ @has_content ||= {}
82
+ @has_content[name.to_s] = true
83
+ end
84
+
85
+ #
86
+ # Despite its name, this method is a transparent replacement for
87
+ # the original <tt>content_for</tt> Rails helper.
88
+ #
89
+ # It forwards any request to the original Rails helper,
90
+ # but before it keep tracks of every request to ensure <tt>has_content?</tt>
91
+ # will work as expected.
92
+ #
93
+ # == Behind this original name
94
+ #
95
+ # For those of you interested in, this name arises from the need
96
+ # to chain it with <tt>alias_method_chain</tt>, following
97
+ # Rails best practice for extending original core methods.
98
+ #
99
+ def content_for_with_has(*args, &block) # :nodoc:
100
+ has_content!(args.first)
101
+ content_for_without_has(*args, &block)
102
+ end
103
+
104
+ end
105
+ end
@@ -0,0 +1,26 @@
1
+ #
2
+ # = Helperful
3
+ #
4
+ # A collection of useful Rails helpers.
5
+ #
6
+ #
7
+ # Category:: Rails
8
+ # Package:: Helperful
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ # Copyright:: 2008-2009 The Authors
11
+ # License:: MIT License
12
+ #
13
+ #--
14
+ #
15
+ #++
16
+
17
+
18
+ unless defined?(TitleHelper)
19
+ module TitleHelper
20
+ def self.included(base)
21
+ ActiveSupport::Deprecation.warn("TitleHelper is deprecated and will be removed in a future version. " +
22
+ "Please include Helperful::TitleHelper instead.", caller)
23
+ base.send :include, Helperful::TitleHelper
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,84 @@
1
+ #
2
+ # = Helperful
3
+ #
4
+ # A collection of useful Rails helpers.
5
+ #
6
+ #
7
+ # Category:: Rails
8
+ # Package:: Helperful
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ # Copyright:: 2008-2009 The Authors
11
+ # License:: MIT License
12
+ #
13
+ #--
14
+ #
15
+ #++
16
+
17
+
18
+ module Helperful
19
+
20
+ #
21
+ # = Javascript Helper
22
+ #
23
+ # Provides a set of helpers for working with JavaScript in your views.
24
+ #
25
+ # ==== Requires
26
+ #
27
+ # The following requirements are mandatory for this module.
28
+ # Including this helper will automatically include them unless already included.
29
+ #
30
+ # * ActionView::Helpers::CaptureHelper
31
+ # * ActionView::Helpers::JavaScriptHelper
32
+ #
33
+ module JavascriptHelper
34
+
35
+ def self.included(base)
36
+ base.class_eval do
37
+ # base.included_modules.include?(ActionView::Helpers::CaptureHelper) || include(ActionView::Helpers::CaptureHelper)
38
+ # base.included_modules.include?(ActionView::Helpers::JavaScriptHelper) || include(ActionView::Helpers::JavaScriptHelper)
39
+ end
40
+ end
41
+
42
+ #
43
+ # Mixes the features of <tt>content_for</tt> and <tt>javascript_tag</tt> into a single helper.
44
+ #
45
+ # Supports all original method options and features, including the ability to use write the content
46
+ # in a block and javascript tag options.
47
+ #
48
+ # ==== Examples
49
+ #
50
+ # <% javascript_content_for :head do %>
51
+ # $("#id").hide();
52
+ # <% end %>
53
+ #
54
+ # The block is passed as it is to <tt>javascript_tag</tt>,
55
+ # then the result is stored as <tt>content_for</tt> :head.
56
+ # Now you can call <tt>yield</tt> and output your javascript content.
57
+ #
58
+ # <%= yield :head %>
59
+ #
60
+ # <script>$("#id").hide();</script>
61
+ #
62
+ # This example is equal to the following statements.
63
+ #
64
+ # <% javascript_content_for :head do; javascript_tag do %>
65
+ # $("#id").hide();
66
+ # <% end; end %>
67
+ #
68
+ # <% javascript_content_for :head do %>
69
+ # <script>$("#id").hide();</script>
70
+ # <% end %>
71
+ #
72
+ # You can make subsequent calls to <tt>javascript_content_for</tt> and <tt>content_for</tt>,
73
+ # the result for each call will be appended to the value currently stored for <tt>name</tt>.
74
+ #
75
+ def javascript_content_for(name, content_or_options_with_block = nil, html_options = {}, &block)
76
+ if block_given?
77
+ content_for(name, javascript_tag(capture(&block), content_or_options_with_block || {}))
78
+ else
79
+ content_for(name, javascript_tag(content_or_options_with_block, html_options))
80
+ end
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,79 @@
1
+ #
2
+ # = Helperful
3
+ #
4
+ # A collection of useful Rails helpers.
5
+ #
6
+ #
7
+ # Category:: Rails
8
+ # Package:: Helperful
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ # Copyright:: 2008-2009 The Authors
11
+ # License:: MIT License
12
+ #
13
+ #--
14
+ #
15
+ #++
16
+
17
+
18
+ module Helperful
19
+
20
+ #
21
+ # = Title Helper
22
+ #
23
+ # This module provides an helper for managing page title in Rails views and layouts.
24
+ #
25
+ module TitleHelper
26
+
27
+ #
28
+ # = Title Helper
29
+ #
30
+ # This helper provides both input and output capabilities
31
+ # for managing page title in Rails views and layouts.
32
+ #
33
+ # When called with +args+, it stores all args for later retrieval.
34
+ # Additionally, it always return a formatted title so that
35
+ # it can be easily called without arguments to display the full page title.
36
+ #
37
+ # You can pass some additional options to customize the behavior of the returned string.
38
+ # The following options are supported:
39
+ #
40
+ # :separator::
41
+ # the separator used to join all title elements, by default a dash.
42
+ #
43
+ # :site::
44
+ # the name of the site to be appended to page title.
45
+ #
46
+ # :headline::
47
+ # website headline to be appended to page title, after any element.
48
+ #
49
+ # ==== Examples
50
+ #
51
+ # # in a template set the title of the page
52
+ # <h1><%= title "Latest news" %></h1>
53
+ # # => <h1>Latest news</h1>
54
+ #
55
+ # # in the layout print the title of the page
56
+ # # with the name of the site
57
+ # <title><%= title :site => 'My Site' %></title>
58
+ # # => <title>Latest news | My Site</title>
59
+ #
60
+ def title(*args)
61
+ @helperful_title ||= []
62
+ @helperful_title_options ||= {
63
+ :separator => ' - ',
64
+ :headline => nil,
65
+ :site => nil,
66
+ }
67
+ options = args.extract_options!
68
+
69
+ @helperful_title += args
70
+ @helperful_title_options.merge!(options)
71
+
72
+ t = @helperful_title.clone
73
+ t << @helperful_title_options[:site]
74
+ t << @helperful_title_options[:headline]
75
+ t.compact.join(@helperful_title_options[:separator])
76
+ end
77
+
78
+ end
79
+ end