scotch-view_fu 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,112 @@
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
+ # include the default sources (minus application.js)
15
+ javascript(ActionView::Helpers::AssetTagHelper::JAVASCRIPT_DEFAULT_SOURCES),
16
+
17
+ # include the dynamic page javascripts
18
+ page_javascripts(options),
19
+
20
+ # application.js always needs to be at the end
21
+ javascript("application")
22
+ ].join("\n")
23
+ end
24
+
25
+ # returns an entire directory of javascripts recursively
26
+ def javascript_folder(path)
27
+ if use_cache?
28
+ return javascript("min/#{path}.js")
29
+ else
30
+ result = []
31
+ Dir["#{Rails.public_path}/javascripts/#{path}/**/*.js"].each do |item|
32
+ result << item.gsub("#{Rails.public_path}/javascripts/", "")
33
+ end
34
+ return javascript(result)
35
+ end
36
+ end
37
+
38
+ # Include Javascripts on the page
39
+ # options[:include] to an array of strings for javascripts to include
40
+ # options[:exclude] sets an array of strings for javascripts to exclude
41
+ # options[:cache] sets the cache option to javascript_include_tag (only for nonconditional items)
42
+ #
43
+ # Automatically includes javascripts (if they exist on filesystem)
44
+ # - javascripts for [controller]/[controller].js and [controller]/[action].js
45
+ #
46
+ def page_javascripts(options={})
47
+
48
+ # initialize collection
49
+ javascripts = []
50
+ conditional_scripts = []
51
+ @javascripts_add ||= []
52
+ @javascripts_ignore ||= []
53
+
54
+ # Additional scripts (if requested)
55
+ if options[:include]
56
+ @javascripts_add = [options[:include]].flatten + @javascripts_add
57
+ end
58
+
59
+ # Exclude scripts (if requested)
60
+ if options[:exclude]
61
+ @javascripts_ignore = [options[:exclude]].flatten + @javascripts_ignore
62
+ end
63
+
64
+ # Add more scripts
65
+ @javascripts_add.each do |javascript|
66
+ javascripts << javascript
67
+ end
68
+
69
+ # Ignore scripts
70
+ @javascripts_ignore.each do |javascript|
71
+ javascripts.delete(javascript)
72
+ end
73
+
74
+ # dedup javascript array
75
+ javascripts.uniq!
76
+
77
+ # Build Output
78
+ html_output = javascript_include_tag(*javascripts)
79
+
80
+ # Controller/action scripts
81
+ conditional_scripts << "#{controller.controller_name}"
82
+ conditional_scripts << "#{controller.controller_name}/#{controller.action_name}"
83
+
84
+ # only add conditional scripts if they exist on disk
85
+ conditional_scripts.each do |javascript|
86
+ if File.exist?("#{RAILS_ROOT}/public/javascripts/#{javascript}.js")
87
+ html_output << "\n"
88
+ html_output << javascript_include_tag(javascript)
89
+ end
90
+ end
91
+
92
+ # return output
93
+ return html_output
94
+ end
95
+
96
+ # add a javascript to the include list
97
+ def add_javascript(*javascripts)
98
+ @javascripts_add ||= []
99
+ javascripts.each do |javascript|
100
+ @javascripts_add << javascript
101
+ end
102
+ end
103
+
104
+ # ignore a javascript
105
+ def ignore_javascript(*javascripts)
106
+ @javascripts_ignore ||= []
107
+ javascripts.each do |javascript|
108
+ @javascripts_ignore << javascript
109
+ end
110
+ end
111
+ end
112
+ 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("application"), 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,18 @@
1
+ require "view_fu/tag_helper"
2
+ require "view_fu/meta_helper"
3
+ require "view_fu/controller_extensions"
4
+ ActionView::Base.send :include, ViewFu::TagHelper
5
+ ActionView::Base.send :include, ViewFu::MetaHelper
6
+ ActionController::Base.send :include, ViewFu::ControllerExtensions
7
+
8
+ require "browser_detect/helper"
9
+ ActionView::Base.send :include, BrowserDetect::Helper
10
+
11
+ require "headliner/helper"
12
+ ActionView::Base.send :include, Headliner::Helper
13
+
14
+ require "javascripter/helper"
15
+ ActionView::Base.send :include, Javascripter::Helper
16
+
17
+ require "styler/helper"
18
+ ActionView::Base.send :include, Styler::Helper
@@ -0,0 +1,13 @@
1
+ module ViewFu
2
+ module ControllerExtensions
3
+ # allows us to say "helpers." anywhere in our controllers
4
+ def helpers
5
+ self.class.helpers
6
+ end
7
+
8
+ # checks to see if app is in production mode
9
+ def production?
10
+ Rails.env == "production"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,52 @@
1
+ module ViewFu
2
+ module MetaHelper
3
+ # Output the current page's meta tags
4
+ def meta_tags
5
+ %(<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
6
+ <meta content="no" name="imagetoolbar"/>
7
+ <meta content="#{meta_keywords}" name="keywords"/>
8
+ <meta content="#{meta_description}" name="description"/>
9
+ )
10
+ end
11
+
12
+ # Get/Set Meta Keywords
13
+ def meta_keywords(meta_keywords = nil)
14
+ if meta_keywords
15
+ @__meta_keywords = meta_keywords
16
+ else
17
+ @__meta_keywords ||= ""
18
+
19
+ # Check if we have AppConfig to use for default
20
+ if defined? AppConfig
21
+ default_meta_keywords = AppConfig.default_meta_keywords
22
+ else
23
+ default_meta_keywords = ""
24
+ end
25
+
26
+ # set the default if meta_keywords is blank
27
+ @__meta_keywords = default_meta_keywords if @__meta_keywords.blank?
28
+ end
29
+ return @__meta_keywords
30
+ end
31
+
32
+ # Get/Set Meta Description
33
+ def meta_description(meta_description = nil)
34
+ if meta_description
35
+ @__meta_description = meta_description
36
+ else
37
+ @__meta_description ||= ""
38
+
39
+ # Check if we have AppConfig to use for default
40
+ if defined? AppConfig
41
+ default_meta_description = AppConfig.default_meta_description
42
+ else
43
+ default_meta_description = ""
44
+ end
45
+
46
+ # set the default if meta_description is blank
47
+ @__meta_description = default_meta_description if @__meta_description.blank?
48
+ end
49
+ return @__meta_description
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,146 @@
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 a nonbreaking space
15
+ def nbsp
16
+ "&nbsp;"
17
+ end
18
+
19
+ # Writes an hr space tag
20
+ def space
21
+ "<hr class='space' />"
22
+ end
23
+
24
+ # Writes an anchor tag
25
+ def anchor(anchor_name)
26
+ "<a name='#{anchor_name}'></a>"
27
+ end
28
+
29
+ # Writes a clear tag
30
+ def clear_tag(tag, direction = nil)
31
+ if tag == :br
32
+ "<br class=\"clear#{direction}\" />"
33
+ else
34
+ "<#{tag} class=\"clear#{direction}\"></#{tag}>"
35
+ end
36
+ end
37
+
38
+ def current_year
39
+ Time.now.strftime("%Y")
40
+ end
41
+
42
+ # Writes a clear div tag
43
+ def clear(direction = nil)
44
+ clear_tag(:div, direction)
45
+ end
46
+
47
+ # Return some lorem text
48
+ def lorem
49
+ "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."
50
+ end
51
+
52
+ # Return a hidden attribute hash (useful in Haml tags - %div{hidden})
53
+ def hidden
54
+ {:style => "display:none"}
55
+ end
56
+
57
+ # Easily link to an image
58
+ def link_to_image(image_path, label, url, options={})
59
+ # setup vertical alignment
60
+ vert_align = options.delete(:vert)
61
+ if vert_align.nil?
62
+ vert_style = "vertical-align: middle"
63
+ elsif vert_align.blank?
64
+ vert_style = ""
65
+ else
66
+ vert_style = "vertical-align: #{vert_align.to_i}px"
67
+ end
68
+
69
+ link_to(image_tag(image_path, :class => "vert-middle", :style => "#{vert_style}"), url, options)+"&nbsp;"+
70
+ link_to(label, url, options)
71
+ end
72
+
73
+
74
+ # Return a hidden attribute hash if a condition evaluates to true
75
+ def hide_if(condition)
76
+ if condition
77
+ hidden
78
+ else
79
+ {}
80
+ end
81
+ end
82
+ alias :hidden_if :hide_if
83
+
84
+ # Return a hidden attribute hash if a condition evaluates to false
85
+ def hide_unless(condition)
86
+ if !condition
87
+ hidden
88
+ else
89
+ {}
90
+ end
91
+ end
92
+ alias :hidden_unless :hide_if
93
+
94
+
95
+ # Wrap a delete link
96
+ def delete_link(*args)
97
+ options = {:method => :delete, :confirm => "Are you sure you want to delete this?"}.merge(args.extract_options!)
98
+ args << options
99
+ link_to(*args)
100
+ end
101
+
102
+ # Wrap a block with a link
103
+ def link_to_block(*args, &block)
104
+ content = capture_haml(&block)
105
+ return link_to(content, *args)
106
+ end
107
+
108
+ # Check if we're on production environment
109
+ def production?
110
+ Rails.env == "production"
111
+ end
112
+
113
+ # Check if we're on New or Create actions
114
+ def is_new?
115
+ action = params[:action]
116
+ action == "new" || action == "create"
117
+ end
118
+
119
+ # Check if we're on Edit or Update actions
120
+ def is_edit?
121
+ action = params[:action]
122
+ action == "edit" || action == "update"
123
+ end
124
+
125
+ # Whether or not to use caching
126
+ def use_cache?
127
+ ActionController::Base.perform_caching
128
+ end
129
+
130
+ # Display will_paginate paging links
131
+ def paging(page_data, style = :sabros)
132
+ return unless page_data.class == WillPaginate::Collection
133
+ will_paginate(page_data, :class => "pagination #{style}", :inner_window => 3)
134
+ end
135
+
136
+ # clearbit icons
137
+ def clearbit_icon(icon, color, options = {})
138
+ image_tag "clearbits/#{icon}.gif", {:class => "clearbits #{color}", :alt => icon}.merge(options)
139
+ end
140
+
141
+ # pixel spacing helper
142
+ def pixel(options = {})
143
+ image_tag "pixel.png", options
144
+ end
145
+ end
146
+ end