neorails-view_fu 0.1.20080705

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,40 @@
1
+ ViewFu - Copyright (c) 2008 NeoRails.com
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.
21
+
22
+ Original Plugins (Styler, Javascripter, Headliner) - Copyright (c) 2007 Patrick Crowley, the.railsi.st
23
+
24
+ Permission is hereby granted, free of charge, to any person obtaining a copy
25
+ of this software and associated documentation files (the "Software"), to deal
26
+ in the Software without restriction, including without limitation the rights
27
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28
+ copies of the Software, and to permit persons to whom the Software is
29
+ furnished to do so, subject to the following conditions:
30
+
31
+ The above copyright notice and this permission notice shall be included in
32
+ all copies or substantial portions of the Software.
33
+
34
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
40
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,133 @@
1
+ ViewFu
2
+ ======
3
+
4
+ ViewFu is a Rails plugin that provides all the miscellaneous View tasks. It's a combination of the functionality of Styler, Javascripter, and Headline (from Patrick Crowley, the.railsi.st) - along with additional tweaks such as providing commonly used View Helpers Methods.
5
+
6
+
7
+ ViewFu HTML Helpers
8
+ =======
9
+ ViewFu provides helpers for commonly used html elements
10
+
11
+ br #=> <br />
12
+ hr #=> <hr />
13
+ anchor("posts") #=> <a name='posts'></a>
14
+ clear #=> <div class="clear"></div>
15
+ clear(:left) #=> <div class="clearleft"></div>
16
+ clear_tag(:br) #=> <br class="clear" />
17
+ lorem #=> Lorem ipsum dolor...
18
+
19
+
20
+ ViewFu Helper Queries
21
+ =======
22
+ production? #=> returns true if Rails.env == "production"
23
+ is_new? #=> returns true is we're on a "new" or "create" action
24
+ is_edit? #=> returns true is we're on an "edit" or "update" action
25
+ use_cache? #=> returns true if perform_caching is turned on
26
+
27
+
28
+ ViewFu Misc Helpers
29
+ =======
30
+ paging(@array) #=> display a will_paginate paging links (only if the array is a valid paging collection)
31
+ paging(@array, :sabros) #=> wrap the paging links with a class "sabros"
32
+
33
+
34
+ Haml Specific Helpers
35
+ =======
36
+ Haml allows you to pash a hash of attributes. ViewFu assists this by providing a simple "hidden" helper that allows you to conditionally hide page elements.
37
+
38
+ %div{hidden} #=> <div style="display:none">
39
+ %div.posts{hidden_if(@posts.empty?)} #=> hide the .posts div if the array is empty
40
+ %p.empty{hidden_unless(@posts.empty?)} #=> hide the empty posts message if the array has elements
41
+
42
+
43
+ Stylesheets
44
+ =======
45
+ ViewFu allows you to add a dynamic stylesheets collection to the top of your page. If you have any partials that depend on a stylesheet, they just need to call add_stylesheet(path) and the styles will be available on the page.
46
+
47
+ You'll just need to add a call *stylesheets* somewhere your page header.
48
+
49
+ stylesheets
50
+ writes the stylesheet collection to html
51
+
52
+ stylesheet
53
+ alias around stylesheet_link_tag
54
+
55
+ stylesheet_folder(path)
56
+ lets you include a recursive folder of stylesheets
57
+
58
+ page_stylesheets
59
+ output stylesheets specific to a page
60
+
61
+ add_stylesheet(path)
62
+ adds a stylesheet to the collection to be included on the page
63
+
64
+ ignore_stylesheet(path)
65
+ removes a stylesheet from the collection to be included on the page
66
+
67
+ See lib/stylesheet/README for more details
68
+
69
+
70
+ Javascripts
71
+ =======
72
+ ViewFu allows you to add a dynamic javascript collection to the top(or bottom) of your page. Now, if you have a partial that depends on a javascript library, you can just include it via add_javascript(path) and it will appear on the top of your page.
73
+
74
+ You'll just need add a call to *javascripts* to your layout and the javascripts you included an all your partials will appear.
75
+
76
+ javascripts
77
+ writes the javascript collection to html
78
+
79
+ javascript
80
+ alias around javascript_include_tag
81
+
82
+ javascript_folder(path)
83
+ lets you include a recursive folder of javascripts
84
+
85
+ page_javascripts
86
+ output javascripts specific to a page
87
+
88
+ add_javascript(path)
89
+ adds a javascript to the collection to be included on the page
90
+
91
+ ignore_javascript(path)
92
+ removes a javascript from the collection to be included on the page
93
+
94
+ See lib/javascripter/README for more details
95
+
96
+ Page Titles
97
+ =======
98
+ ViewFu allows you to easily set the current page title from anywhere on your views
99
+
100
+ title
101
+ Displays the current page title
102
+
103
+ title(new_title)
104
+ Sets the page title
105
+
106
+ See lib/headliner/README for more details
107
+
108
+
109
+ Meta Tags
110
+ =======
111
+ ViewFu allows you to set meta tags on your page header from anywhere. Just add a call to meta_tags somewhere in your page header.
112
+
113
+ meta_tags
114
+ output all the html meta tags currently on the page
115
+
116
+ meta_keywords
117
+ output the meta keywords tag
118
+
119
+ meta_keywords(val)
120
+ set the page meta keywords
121
+
122
+ meta_description
123
+ output the meta description tag
124
+
125
+ meta_description(val)
126
+ set the page meta description
127
+
128
+
129
+
130
+
131
+
132
+
133
+ Copyright (c) 2008 NeoRails.com, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the view_fu plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the view_fu plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'ViewFu'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
@@ -0,0 +1,45 @@
1
+ module BrowserDetect::Helper
2
+
3
+ # check the current browser (via user agent) for the following:
4
+ # :mozilla
5
+ # :ie6
6
+ # :ie7
7
+ # :ie
8
+ # :safari
9
+ def browser_is? name
10
+
11
+ name = name.to_s.strip
12
+
13
+ return true if browser_name == name
14
+ return true if name == 'mozilla' && browser_name == 'gecko'
15
+ return true if name == 'ie6' && browser_name.index('ie6')
16
+ return true if name == 'ie7' && browser_name.index('ie7')
17
+ return true if name == 'ie' && browser_name.index('ie')
18
+ return true if name == 'webkit' && browser_name == 'safari'
19
+
20
+ end
21
+
22
+ # find the current browser name
23
+ def browser_name
24
+ @browser_name ||= begin
25
+
26
+ ua = request.env['HTTP_USER_AGENT'].to_s.downcase
27
+
28
+ if ua.index('msie') && !ua.index('opera') && !ua.index('webtv')
29
+ 'ie'+ua[ua.index('msie')+5].chr
30
+ elsif ua.index('gecko/')
31
+ 'gecko'
32
+ elsif ua.index('opera')
33
+ 'opera'
34
+ elsif ua.index('konqueror')
35
+ 'konqueror'
36
+ elsif ua.index('applewebkit/')
37
+ 'safari'
38
+ elsif ua.index('mozilla/')
39
+ 'gecko'
40
+ else
41
+ ""
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,118 @@
1
+ =========
2
+ Headliner
3
+ =========
4
+
5
+ Headliner DRYs up your page titles.
6
+
7
+
8
+ Background
9
+ ==========
10
+
11
+ Normally, if your Rails application has lots of actions and a shared
12
+ layout, you might find yourself setting custom page title names in your
13
+ controllers.
14
+
15
+ Here's an example:
16
+
17
+ class PagesController < ApplicationController
18
+ def about
19
+ @title = "About us"
20
+ end
21
+ end
22
+
23
+ Then, in your main layout, you might have something like this:
24
+
25
+ <head>
26
+ <title>My website<% if @title %>: <%= @title %><% end %></title>
27
+ </head
28
+
29
+ This works okay... but page titles don't really belong in controllers, do
30
+ they?
31
+
32
+ So, by moving these titles into your views, we can DRY things up a bit and
33
+ reinforce the MVC design pattern that's so fundamental to Ruby on Rails.
34
+
35
+
36
+ Usage
37
+ =====
38
+
39
+ First, add this code to your main layout:
40
+
41
+ <head>
42
+ <%= title :site => "My website" %>
43
+ </head>
44
+
45
+ Then, to set the page title, add this to each of your views:
46
+
47
+ <h1><%= title "My page title" %></h1>
48
+
49
+ When views are rendered, the page title will be included in the right
50
+ spots:
51
+
52
+ <head>
53
+ <title>My website | My page title</title>
54
+ </head>
55
+ <body>
56
+ <h1>My page title</h1>
57
+ </body>
58
+
59
+
60
+ Options
61
+ =======
62
+
63
+ Use these options to customize the title format:
64
+
65
+ :prefix (text between site name and separator)
66
+ :separator (text used to separate website name from page title)
67
+ :suffix (text between separator and page title)
68
+ :lowercase (when true, the page name will be lowercase)
69
+ :reverse (when true, the page and site names will be reversed)
70
+
71
+ And here are a few examples to give you ideas.
72
+
73
+ <%= title :separator => "&mdash;" %>
74
+ <%= title :prefix => false, :separator => ":" %>
75
+ <%= title :lowercase => true %>
76
+ <%= title :reverse => true, :prefix => false %>
77
+
78
+
79
+ Dealing with special pages
80
+ ==========================
81
+
82
+ How do you set the page title without showing it in the view?
83
+
84
+ <% title "My page title" %>
85
+
86
+ What if your view headline is different from your page title?
87
+
88
+ <%= title "My page title", "My headline" %>
89
+
90
+
91
+ Mr. T says, ‘Use my method, fool!’
92
+ ==================================
93
+
94
+ Just like ERB's HTML safe method, you can invoke Headliner with a single
95
+ letter alias.
96
+
97
+ <h1><%=t "My page title" %></h1>
98
+
99
+
100
+ How does it work?
101
+ =================
102
+
103
+ Ruby on Rails renders actions *before* inserting them into layouts. So, if
104
+ you set a variable in your view, it will be accessible in your layout. But,
105
+ at first glance, it looks like you're using a variable (in the head)
106
+ before it's been assigned a value (in the body). Cool, huh?
107
+
108
+
109
+ Credits
110
+ =======
111
+
112
+ Special thanks to Nick Zadrozny and Jordan Fowler for their input.
113
+
114
+
115
+ Feedback
116
+ ========
117
+
118
+ Comments, bug reports, and svn diffs welcome at http://the.railsi.st.
@@ -0,0 +1,65 @@
1
+ module Headliner
2
+ module Helper
3
+ def title(options, headline='')
4
+ if options.is_a? String
5
+ save_title(options, headline)
6
+ else
7
+ display_title(options)
8
+ end
9
+ end
10
+
11
+ def save_title(title, headline)
12
+ @title = title.gsub(/<\/?[^>]*>/, '')
13
+ headline.blank? ? title : headline
14
+ end
15
+
16
+ def display_title(options)
17
+ # Prefix (leading space)
18
+ if options[:prefix]
19
+ prefix = options[:prefix]
20
+ elsif options[:prefix] == false
21
+ prefix = ''
22
+ else
23
+ prefix = ' '
24
+ end
25
+
26
+ # Separator
27
+ unless options[:separator].blank?
28
+ separator = options[:separator]
29
+ else
30
+ separator = '|'
31
+ end
32
+
33
+ # Suffix (trailing space)
34
+ if options[:suffix]
35
+ suffix = options[:suffix]
36
+ elsif options[:suffix] == false
37
+ suffix = ''
38
+ else
39
+ suffix = ' '
40
+ end
41
+
42
+ # Lowercase title?
43
+ if options[:lowercase] == true
44
+ @title = @title.downcase unless @title.blank?
45
+ end
46
+
47
+ # Set website/page order
48
+ unless @title.blank?
49
+ if options[:reverse] == true
50
+ # Reverse order => "Page : Website"
51
+ return content_tag(:title, @title + prefix + separator + suffix + options[:site])
52
+ else
53
+ # Standard order => "Website : Page"
54
+ return content_tag(:title, options[:site] + prefix + separator + suffix + @title)
55
+ end
56
+ end
57
+
58
+ # If title is blank, return only website name
59
+ content_tag :title, options[:site]
60
+ end
61
+
62
+ # Mr. T says, "Use my method, fool!"
63
+ alias t title
64
+ end
65
+ end
@@ -0,0 +1,87 @@
1
+ ======
2
+ Javascripter
3
+ ======
4
+
5
+ Javascripter is designed to DRY up the process of including and generating
6
+ javascripts, so you can think less about javascript configuration and more
7
+ about scripting.
8
+
9
+
10
+ Usage
11
+ =====
12
+
13
+ To use Javascripter, just update your layout(s) with this code:
14
+
15
+ <head>
16
+ <title>the.rails.ist</title>
17
+ <%= javascripts %>
18
+ </head>
19
+
20
+ Javascripter will then include your javascripts automatically:
21
+
22
+ <head>
23
+ <title>the.rails.ist</title>
24
+ <link href="/javascripts/application.js?1170968897" />
25
+ </head>
26
+
27
+ Javascripter will also dynamically include javascripts for each of your
28
+ controllers (if such javascripts are present), so you can keep your scripts
29
+ organized into logical sections.
30
+
31
+
32
+ Organize your javascripts
33
+ =========================
34
+
35
+ Javascripter uses a simple set of conventions:
36
+
37
+ - Javascripts for your entire application should be stored in application.js
38
+ - Javascripts for specific controllers should be stored in controller.js
39
+ - Javascripts for specific actions should be stored in controller_action.js
40
+
41
+ When used in combination, these conventions can scale up to support pretty
42
+ big applications.
43
+
44
+
45
+ Include additional javascripts
46
+ ==============================
47
+
48
+ Conventions are great, but need to add your own javascripts?
49
+
50
+ <%= javascripts :include => "reset" %>
51
+ <%= javascripts :include => ["reset", "fonts"] %>
52
+
53
+
54
+ Use nested javascripts (optional)
55
+ =================================
56
+
57
+ For bigger projects, you might wish to break your javascripts into separate
58
+ directories.
59
+
60
+ To use nested javascripts, just create subdirectories in public/javascripts
61
+ for each of your controllers, and then add javascripts for individual
62
+ actions you wish to script.
63
+
64
+ - Javascripts for an entire controller should be stored in controller.js
65
+ - Javascripts for specific actions should be stored in controller/action.js
66
+
67
+
68
+ Generator
69
+ =========
70
+
71
+ Javascripter also includes a generator that will create a default set of
72
+ javascripts (application.js) and a separate
73
+ javascript for each controller in your application.
74
+
75
+ To use the generator, run this command in your terminal:
76
+
77
+ script/generate javascripts
78
+
79
+ If you add a new controller, just run the generator again and a new
80
+ javascript for the controller will be created. (Javascripter will safely ignore
81
+ any existing javascripts.)
82
+
83
+
84
+ Feedback
85
+ ========
86
+
87
+ Comments, bug reports, and svn diffs welcome at http://the.railsi.st.
@@ -0,0 +1,106 @@
1
+ module Javascripter
2
+ module Helper
3
+
4
+ # alias for javascript_include_tag
5
+ def javascript(*input)
6
+ input = [input].flatten
7
+
8
+ javascript_include_tag(*input)
9
+ end
10
+
11
+ # Override this if needed
12
+ def javascripts(options={})
13
+ [
14
+ page_javascripts(options),
15
+ javascript("application")
16
+ ].join("\n")
17
+ end
18
+
19
+ # returns an entire directory of javascripts recursively
20
+ def javascript_folder(path)
21
+ if use_cache?
22
+ return stylesheet "min/#{path}.js"
23
+ else
24
+ result = []
25
+ Dir["#{Rails.public_path}/javascripts/#{path}/**/*.js"].each do |item|
26
+ result << item.gsub("#{Rails.public_path}/javascripts/", "")
27
+ end
28
+ return stylesheet result
29
+ end
30
+ end
31
+
32
+ # Include Javascripts on the page
33
+ # options[:include] to an array of strings for javascripts to include
34
+ # options[:exclude] sets an array of strings for javascripts to exclude
35
+ # options[:cache] sets the cache option to javascript_include_tag (only for nonconditional items)
36
+ #
37
+ # Automatically includes javascripts (if they exist on filesystem)
38
+ # - javascripts for [controller]/[controller].js and [controller]/[action].js
39
+ #
40
+ def page_javascripts(options={})
41
+
42
+ # initialize collection
43
+ javascripts = []
44
+ conditional_scripts = []
45
+ @javascripts_add ||= []
46
+ @javascripts_ignore ||= []
47
+
48
+ # Additional scripts (if requested)
49
+ if options[:include]
50
+ @javascripts_add = [options[:include]].flatten + @javascripts_add
51
+ end
52
+
53
+ # Exclude scripts (if requested)
54
+ if options[:exclude]
55
+ @javascripts_ignore = [options[:exclude]].flatten + @javascripts_ignore
56
+ end
57
+
58
+ # Add more scripts
59
+ @javascripts_add.each do |javascript|
60
+ javascripts << javascript
61
+ end
62
+
63
+ # Ignore scripts
64
+ @javascripts_ignore.each do |javascript|
65
+ javascripts.delete(javascript)
66
+ end
67
+
68
+ # dedup javascript array
69
+ javascripts.uniq!
70
+
71
+ # Build Output
72
+ html_output = javascript_include_tag(*javascripts)
73
+
74
+ # Controller/action scripts
75
+ conditional_scripts << "#{controller.controller_name}"
76
+ conditional_scripts << "#{controller.controller_name}/#{controller.action_name}"
77
+
78
+ # only add conditional scripts if they exist on disk
79
+ conditional_scripts.each do |javascript|
80
+ if File.exist?("#{RAILS_ROOT}/public/javascripts/#{javascript}.js")
81
+ html_output << "\n"
82
+ html_output << javascript_include_tag(javascript)
83
+ end
84
+ end
85
+
86
+ # return output
87
+ return html_output
88
+ end
89
+
90
+ # add a javascript to the include list
91
+ def add_javascript(*javascripts)
92
+ @javascripts_add ||= []
93
+ javascripts.each do |javascript|
94
+ @javascripts_add << javascript
95
+ end
96
+ end
97
+
98
+ # ignore a javascript
99
+ def ignore_javascript(*javascripts)
100
+ @javascripts_ignore ||= []
101
+ javascripts.each do |javascript|
102
+ @javascripts_ignore << javascript
103
+ end
104
+ end
105
+ end
106
+ end
data/lib/styler/README ADDED
@@ -0,0 +1,95 @@
1
+ ======
2
+ Styler
3
+ ======
4
+
5
+ Styler is designed to DRY up the process of including and generating
6
+ stylesheets, so you can think less about stylesheet configuration and more
7
+ about styling.
8
+
9
+
10
+ Usage
11
+ =====
12
+
13
+ To use Styler, just update your layout(s) with this code:
14
+
15
+ <head>
16
+ <title>the.rails.ist</title>
17
+ <%= stylesheets %>
18
+ </head>
19
+
20
+ Styler will then include your stylesheets automatically:
21
+
22
+ <head>
23
+ <title>the.rails.ist</title>
24
+ <link href="/stylesheets/application.css?1170968897" />
25
+ <!--[if IE 7]>
26
+ <link href="/stylesheets/ie7.css?1170968897" />
27
+ <![endif]-->
28
+ <!--[if IE 6]>
29
+ <link href="/stylesheets/ie6.css?1170968897" />
30
+ <![endif]-->
31
+ </head>
32
+
33
+ Styler will also dynamically include stylesheets for each of your
34
+ controllers (if such stylesheets are present), so you can keep your styles
35
+ organized into logical sections.
36
+
37
+
38
+ Organize your stylesheets
39
+ =========================
40
+
41
+ Styler uses a simple set of conventions:
42
+
43
+ - Styles for your entire application should be stored in application.css
44
+ - Styles for specific controllers should be stored in controller.css
45
+ - Styles for specific actions should be stored in controller_action.css
46
+ - Styles for Internet Explorer 7 should be stored in ie7.css
47
+ - Styles for Internet Explorer 6 should be stored in ie6.css
48
+
49
+ When used in combination, these conventions can scale up to support pretty
50
+ big applications.
51
+
52
+
53
+ Include additional stylesheets
54
+ ==============================
55
+
56
+ Conventions are great, but need to add your own stylesheets?
57
+
58
+ <%= stylesheets :include => "reset" %>
59
+ <%= stylesheets :include => ["reset", "fonts"] %>
60
+
61
+
62
+ Use nested stylesheets (optional)
63
+ =================================
64
+
65
+ For bigger projects, you might wish to break your stylesheets into separate
66
+ directories.
67
+
68
+ To use nested stylesheets, just create subdirectories in public/stylesheets
69
+ for each of your controllers, and then add stylesheets for individual
70
+ actions you wish to style.
71
+
72
+ - Styles for an entire controller should be stored in controller.css
73
+ - Styles for specific actions should be stored in controller/action.css
74
+
75
+
76
+ Generator
77
+ =========
78
+
79
+ Styler also includes a generator that will create a default set of
80
+ stylesheets (application.css, ie7.css, and ie6.css) and a separate
81
+ stylesheet for each controller in your application.
82
+
83
+ To use the generator, run this command in your terminal:
84
+
85
+ script/generate stylesheets
86
+
87
+ If you add a new controller, just run the generator again and a new
88
+ stylesheet for the controller will be created. (Styler will safely ignore
89
+ any existing stylesheets.)
90
+
91
+
92
+ Feedback
93
+ ========
94
+
95
+ Comments, bug reports, and svn diffs welcome at http://the.railsi.st.
@@ -0,0 +1,117 @@
1
+ module Styler
2
+ module Helper
3
+ # alias for stylesheet_link_tag
4
+ def stylesheet(*input)
5
+ input = [input].flatten
6
+ stylesheet_link_tag(*input)
7
+ end
8
+
9
+ # override this in your application_helper to clean it up
10
+ def stylesheets(options = {})
11
+ [stylesheet(:defaults), page_stylesheets(options)].join("\n")
12
+ end
13
+
14
+ # returns an entire directory of stylesheets recursively
15
+ def stylesheet_folder(path)
16
+ if use_cache? # or browser_is? :ie
17
+ return stylesheet "min/#{path}.css"
18
+ else
19
+ result = []
20
+ Dir["#{Rails.public_path}/stylesheets/#{path}/**/*.css"].each do |css|
21
+ result << css.gsub("#{Rails.public_path}/stylesheets/", "")
22
+ end
23
+ return stylesheet result
24
+ end
25
+ end
26
+
27
+ # Include Stylesheets on the page
28
+ # options[:include] to an array of strings for stylesheets to include
29
+ # options[:exclude] sets an array of strings for stylesheets to exclude
30
+ #
31
+ # Automatically includes stylesheets (if they exist on filesystem)
32
+ # - stylesheets for [controller]/[controller].css and [controller]/[action].css
33
+ # - stylesheets for browser overrides (ie6.css, ie7.css, safari.css)
34
+ #
35
+ def page_stylesheets(options={})
36
+
37
+ # set default values
38
+ stylesheets = []
39
+ @stylesheets_add ||= []
40
+ @stylesheets_ignore ||= []
41
+
42
+ # Include sheets from options
43
+ if options[:include]
44
+ @stylesheets_add = [options[:include]].flatten + @stylesheets_add
45
+ end
46
+
47
+ # Exclude sheets from options
48
+ if options[:exclude]
49
+ @stylesheets_ignore = [options[:exclude]].flatten + @stylesheets_ignore
50
+ end
51
+
52
+ # Add all sheets from instance variable
53
+ @stylesheets_add.each do |stylesheet|
54
+ stylesheets << stylesheet
55
+ end
56
+
57
+ # Ignore all sheets from instance variable
58
+ @stylesheets_ignore.each do |stylesheet|
59
+ stylesheets.delete(stylesheet)
60
+ end
61
+
62
+ # dedupe stylesheets array
63
+ stylesheets.uniq!
64
+
65
+ # Build Initial Output
66
+ html_output = stylesheet_link_tag(*stylesheets)
67
+
68
+ # Build conditional stylesheets (we need to check these on the filesystem before including)
69
+ conditional_stylesheets = []
70
+
71
+ # Controller/action sheets
72
+ conditional_stylesheets << "pages/#{controller.controller_name}"
73
+ conditional_stylesheets << "pages/#{controller.controller_name}/#{controller.action_name}"
74
+
75
+ # IE6
76
+ if browser_is?(:ie6)
77
+ conditional_stylesheets << "ie6"
78
+ end
79
+
80
+ # IE7
81
+ if browser_is?(:ie7)
82
+ conditional_stylesheets << "ie7"
83
+ end
84
+
85
+ # Safari
86
+ if browser_is?(:safari) and
87
+ conditional_stylesheets << "safari"
88
+ end
89
+
90
+ # only add conditional stylesheets if they exist on disk
91
+ conditional_stylesheets.each do |stylesheet|
92
+ if File.exist?("#{RAILS_ROOT}/public/stylesheets/#{stylesheet}.css")
93
+ html_output << "\n"
94
+ html_output << stylesheet_link_tag(stylesheet)
95
+ end
96
+ end
97
+
98
+ return html_output
99
+ end
100
+
101
+ # Allows individual views to customize the stylesheet list
102
+ def add_stylesheet(*stylesheets)
103
+ @stylesheets_add ||= []
104
+ stylesheets.each do |stylesheet|
105
+ @stylesheets_add << stylesheet
106
+ end
107
+ end
108
+
109
+ # Allows individual views to customize the stylesheet list
110
+ def ignore_stylesheet(*stylesheets)
111
+ @stylesheets_ignore ||= []
112
+ stylesheets.each do |stylesheet|
113
+ @stylesheets_ignore << stylesheet
114
+ end
115
+ end
116
+ end
117
+ end
data/lib/view_fu.rb ADDED
@@ -0,0 +1,16 @@
1
+ require "view_fu/tag_helper"
2
+ require "view_fu/meta_helper"
3
+ ActionView::Base.send :include, ViewFu::TagHelper
4
+ ActionView::Base.send :include, ViewFu::MetaHelper
5
+
6
+ require "browser_detect/helper"
7
+ ActionView::Base.send :include, BrowserDetect::Helper
8
+
9
+ require "headliner/helper"
10
+ ActionView::Base.send :include, Headliner::Helper
11
+
12
+ require "javascripter/helper"
13
+ ActionView::Base.send :include, Javascripter::Helper
14
+
15
+ require "styler/helper"
16
+ ActionView::Base.send :include, Styler::Helper
@@ -0,0 +1,51 @@
1
+ module ViewFu
2
+ module MetaHelper
3
+ # Output the current page's meta tags
4
+ def meta_tags
5
+ %(
6
+ <meta name="keywords" content="#{meta_keywords}" />
7
+ <meta name="description" content="#{meta_description}" />
8
+ )
9
+ end
10
+
11
+ # Get/Set Meta Keywords
12
+ def meta_keywords(meta_keywords = nil)
13
+ if meta_keywords
14
+ @__meta_keywords = meta_keywords
15
+ else
16
+ @__meta_keywords ||= ""
17
+
18
+ # Check if we have AppConfig to use for default
19
+ if defined? AppConfig
20
+ default_meta_keywords = AppConfig.default_meta_keywords
21
+ else
22
+ default_meta_keywords = ""
23
+ end
24
+
25
+ # set the default if meta_keywords is blank
26
+ @__meta_keywords = default_meta_keywords if @__meta_keywords.blank?
27
+ end
28
+ return @__meta_keywords
29
+ end
30
+
31
+ # Get/Set Meta Description
32
+ def meta_description(meta_description = nil)
33
+ if meta_description
34
+ @__meta_description = meta_description
35
+ else
36
+ @__meta_description ||= ""
37
+
38
+ # Check if we have AppConfig to use for default
39
+ if defined? AppConfig
40
+ default_meta_description = AppConfig.default_meta_description
41
+ else
42
+ default_meta_description = ""
43
+ end
44
+
45
+ # set the default if meta_description is blank
46
+ @__meta_description = default_meta_description if @__meta_description.blank?
47
+ end
48
+ return @__meta_description
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,134 @@
1
+ module ViewFu
2
+ module TagHelper
3
+
4
+ # Writes a br tag
5
+ def br
6
+ "<br />"
7
+ end
8
+
9
+ # Writes an hr tag
10
+ def hr
11
+ "<hr />"
12
+ end
13
+
14
+ # Writes an hr space tag
15
+ def space
16
+ "<hr class='space' />"
17
+ end
18
+
19
+ # Writes an anchor tag
20
+ def anchor(anchor_name)
21
+ "<a name='#{anchor_name}'></a>"
22
+ end
23
+
24
+ # Writes a clear tag
25
+ def clear_tag(tag, direction = nil)
26
+ if tag == :br
27
+ "<br class=\"clear#{direction}\" />"
28
+ else
29
+ "<#{tag} class=\"clear#{direction}\"></#{tag}>"
30
+ end
31
+ end
32
+
33
+ # Writes a clear div tag
34
+ def clear(direction = nil)
35
+ clear_tag(:div, direction)
36
+ end
37
+
38
+ # Return some lorem text
39
+ def lorem
40
+ "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
41
+ end
42
+
43
+ # Return a hidden attribute hash (useful in Haml tags - %div{hidden})
44
+ def hidden
45
+ {:style => "display:none"}
46
+ end
47
+
48
+ # Easily link to an image
49
+ def link_to_image(image_path, label, url, options={})
50
+ # setup vertical alignment
51
+ vert_align = options.delete(:vert)
52
+ if vert_align.nil?
53
+ vert_style = "vertical-align: middle"
54
+ elsif vert_align.blank?
55
+ vert_style = ""
56
+ else
57
+ vert_style = "vertical-align: #{vert_align.to_i}px"
58
+ end
59
+
60
+ link_to(image_tag(image_path, :class => "vert-middle", :style => "#{vert_style}"), url, options)+"&nbsp;"+
61
+ link_to(label, url, options)
62
+ end
63
+
64
+
65
+ # Return a hidden attribute hash if a condition evaluates to true
66
+ def hide_if(condition)
67
+ if condition
68
+ hidden
69
+ else
70
+ {}
71
+ end
72
+ end
73
+
74
+ # Return a hidden attribute hash if a condition evaluates to false
75
+ def hide_unless(condition)
76
+ if !condition
77
+ hidden
78
+ else
79
+ {}
80
+ end
81
+ end
82
+
83
+ # Wrap a delete link
84
+ def delete_link(*args)
85
+ options = {:method => :delete, :confirm => "Are you sure you want to delete this?"}.merge(args.extract_options!)
86
+ args << options
87
+ link_to(*args)
88
+ end
89
+
90
+ # Wrap a block with a link
91
+ def link_to_block(*args, &block)
92
+ content = capture_haml(&block)
93
+ return link_to(content, *args)
94
+ end
95
+
96
+ # Check if we're on production environment
97
+ def production?
98
+ Rails.env == "production"
99
+ end
100
+
101
+ # Check if we're on New or Create actions
102
+ def is_new?
103
+ action = params[:action]
104
+ action == "new" || action == "create"
105
+ end
106
+
107
+ # Check if we're on Edit or Update actions
108
+ def is_edit?
109
+ action = params[:action]
110
+ action == "edit" || action == "update"
111
+ end
112
+
113
+ # Whether or not to use caching
114
+ def use_cache?
115
+ ActionController::Base.perform_caching
116
+ end
117
+
118
+ # Display will_paginate paging links
119
+ def paging(page_data, style = :sabros)
120
+ return unless page_data.class == WillPaginate::Collection
121
+ will_paginate(page_data, :class => "pagination #{style}", :inner_window => 3)
122
+ end
123
+
124
+ # clearbit icons
125
+ def clearbit_icon(icon, color, options = {})
126
+ image_tag "clearbits/#{icon}.gif", {:class => "clearbits #{color}", :alt => icon}.merge(options)
127
+ end
128
+
129
+ # pixel spacing helper
130
+ def pixel(options = {})
131
+ image_tag "pixel.png", options
132
+ end
133
+ end
134
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'view_fu'
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'browser_detect/helper'
4
+
5
+ class BrowserDetectTest < Test::Unit::TestCase
6
+
7
+ include ActionView::Helpers::TagHelper
8
+ include BrowserDetect::Helper
9
+
10
+ def test_truth
11
+ assert true
12
+ end
13
+
14
+ end
@@ -0,0 +1,106 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'headliner/helper'
4
+
5
+ class HeadlinerTest < Test::Unit::TestCase
6
+
7
+ include ActionView::Helpers::TagHelper
8
+ include Headliner::Helper
9
+
10
+ # Make the blank? method available without loading Rails
11
+ Object.class_eval do
12
+ def blank?
13
+ if respond_to?(:empty?) && respond_to?(:strip)
14
+ empty? or strip.empty?
15
+ elsif respond_to?(:empty?)
16
+ empty?
17
+ else
18
+ !self
19
+ end
20
+ end
21
+ end
22
+
23
+ def test_title_is_saved
24
+ title "Headliner", ""
25
+ assert_equal "Headliner", @title
26
+ end
27
+
28
+ def test_title_is_saved_with_headline
29
+ headline = save_title "Headliner", "Headliner: a plugin for setting page titles"
30
+ assert_equal "Headliner", @title
31
+ assert_equal "Headliner: a plugin for setting page titles", headline
32
+ end
33
+
34
+ def test_title_is_site_when_empty
35
+ title :site => "the.railsi.st"
36
+ assert_equal nil, @title
37
+ end
38
+
39
+ def test_title_removes_all_tags
40
+ title 'Headliner is <strong class="underline">cool</strong>'
41
+ assert_equal "Headliner is cool", @title
42
+ end
43
+
44
+ def test_title_removes_all_tags_but_displays_tags_in_views
45
+ original_title = save_title 'Headliner is <strong class="underline">cool</strong>', ''
46
+ assert_equal "Headliner is cool", @title
47
+ assert_equal 'Headliner is <strong class="underline">cool</strong>', original_title
48
+ end
49
+
50
+ def test_title_is_lowercase
51
+ @title = "Headliner"
52
+ full_title = display_title :site => "the.railsi.st", :lowercase => true
53
+ assert_equal "<title>the.railsi.st | headliner</title>", full_title
54
+ end
55
+
56
+ def test_title_is_reversed
57
+ @title = "Headliner"
58
+ full_title = display_title :site => "the.railsi.st", :reverse => true
59
+ assert_equal "<title>Headliner | the.railsi.st</title>", full_title
60
+ end
61
+
62
+ def test_title_has_custom_separator
63
+ @title = "Headliner"
64
+ full_title = display_title :site => "the.railsi.st", :separator => "&mdash;"
65
+ assert_equal "<title>the.railsi.st &mdash; Headliner</title>", full_title
66
+ end
67
+
68
+ def test_title_has_custom_prefix_and_suffix
69
+ @title = "Headliner"
70
+ full_title = display_title :site => "the.railsi.st", :prefix => " ", :suffix => " "
71
+ assert_equal "<title>the.railsi.st | Headliner</title>", full_title
72
+ end
73
+
74
+ def test_title_has_no_prefix_and_has_custom_separator
75
+ @title = "Headliner"
76
+ full_title = display_title :site => "the.railsi.st", :prefix => false, :separator => ":"
77
+ assert_equal "<title>the.railsi.st: Headliner</title>", full_title
78
+ end
79
+
80
+ def test_title_has_no_suffix_and_has_custom_separator
81
+ @title = "Headliner"
82
+ full_title = display_title :site => "the.railsi.st", :suffix => false, :separator => "~"
83
+ assert_equal "<title>the.railsi.st ~Headliner</title>", full_title
84
+ end
85
+
86
+ def test_title_has_no_prefix_and_suffix_and_has_custom_separator
87
+ @title = "Headliner"
88
+ full_title = display_title :site => "the.railsi.st",
89
+ :prefix => false,
90
+ :suffix => false,
91
+ :separator => "&mdash;"
92
+ assert_equal "<title>the.railsi.st&mdash;Headliner</title>", full_title
93
+ end
94
+
95
+ def test_title_has_all_custom_options
96
+ @title = "Headliner"
97
+ full_title = display_title :site => "the.railsi.st",
98
+ :prefix => " ",
99
+ :suffix => " ",
100
+ :separator => ".:.",
101
+ :lowercase => true,
102
+ :reverse => true
103
+ assert_equal "<title>headliner .:. the.railsi.st</title>", full_title
104
+ end
105
+
106
+ end
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'styler/helper'
4
+
5
+ class StylerTest < Test::Unit::TestCase
6
+
7
+ include ActionView::Helpers::TagHelper
8
+ include Styler::Helper
9
+
10
+ def test_truth
11
+ assert true
12
+ end
13
+
14
+ end
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'view_fu/meta_helper'
4
+ require 'view_fu/tag_helper'
5
+
6
+ class ViewFuTest < Test::Unit::TestCase
7
+
8
+ include ActionView::Helpers::TagHelper
9
+ include ViewFu::TagHelper
10
+ include ViewFu::MetaHelper
11
+
12
+ def test_truth
13
+ assert true
14
+ end
15
+
16
+ end
data/view_fu.gemspec ADDED
@@ -0,0 +1,41 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'view_fu'
3
+ s.version = '0.1.20080705'
4
+ s.date = '2008-07-05'
5
+
6
+ s.summary = "Lots of handy Rails View helpers. Includes the functionality of Headliner, Styler, and Javascripter"
7
+ s.description = "ViewFu is a Rails plugin that provides all the miscellaneous View tasks. It's a combination of the functionality of Styler, Javascripter, and Headline (from Patrick Crowley, the.railsi.st) - along with additional tweaks such as providing commonly used View Helpers Methods."
8
+
9
+ s.authors = ['Tyler Crocker', 'Patrick Crowley']
10
+ s.email = 'neorails@gmail.com'
11
+ s.homepage = 'http://github.com/neorails/view_fu'
12
+
13
+ s.has_rdoc = true
14
+ s.rdoc_options = ["--main", "README"]
15
+ s.extra_rdoc_files = ["README"]
16
+
17
+ s.add_dependency 'rails', ['>= 2.1']
18
+
19
+ s.files = ["MIT-LICENSE",
20
+ "README",
21
+ "Rakefile",
22
+ "init.rb",
23
+ "lib/browser_detect/helper.rb",
24
+ "lib/headliner/helper.rb",
25
+ "lib/headliner/README",
26
+ "lib/javascripter/helper.rb",
27
+ "lib/javascripter/README",
28
+ "lib/styler/helper.rb",
29
+ "lib/styler/README",
30
+ "lib/view_fu/meta_helper.rb",
31
+ "lib/view_fu/tag_helper.rb",
32
+ "lib/view_fu.rb",
33
+ "rails/init.rb",
34
+ "view_fu.gemspec"]
35
+
36
+ s.test_files = ["test/browser_detect_test.rb",
37
+ "test/headliner_test.rb",
38
+ "test/styler_test.rb",
39
+ "test/view_fu_test.rb"]
40
+
41
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: neorails-view_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.20080705
5
+ platform: ruby
6
+ authors:
7
+ - Tyler Crocker
8
+ - Patrick Crowley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-07-05 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "2.1"
24
+ version:
25
+ description: ViewFu is a Rails plugin that provides all the miscellaneous View tasks. It's a combination of the functionality of Styler, Javascripter, and Headline (from Patrick Crowley, the.railsi.st) - along with additional tweaks such as providing commonly used View Helpers Methods.
26
+ email: neorails@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ files:
34
+ - MIT-LICENSE
35
+ - README
36
+ - Rakefile
37
+ - init.rb
38
+ - lib/browser_detect/helper.rb
39
+ - lib/headliner/helper.rb
40
+ - lib/headliner/README
41
+ - lib/javascripter/helper.rb
42
+ - lib/javascripter/README
43
+ - lib/styler/helper.rb
44
+ - lib/styler/README
45
+ - lib/view_fu/meta_helper.rb
46
+ - lib/view_fu/tag_helper.rb
47
+ - lib/view_fu.rb
48
+ - rails/init.rb
49
+ - view_fu.gemspec
50
+ has_rdoc: true
51
+ homepage: http://github.com/neorails/view_fu
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --main
55
+ - README
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.2.0
74
+ signing_key:
75
+ specification_version: 2
76
+ summary: Lots of handy Rails View helpers. Includes the functionality of Headliner, Styler, and Javascripter
77
+ test_files:
78
+ - test/browser_detect_test.rb
79
+ - test/headliner_test.rb
80
+ - test/styler_test.rb
81
+ - test/view_fu_test.rb