viewaide 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.DS_Store
2
+ *.swp
3
+ pkg
4
+ doc
5
+ coverage/*
6
+ coverage.data
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Josh Clayton
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.
data/Manifest ADDED
@@ -0,0 +1,31 @@
1
+ MIT-LICENSE
2
+ Manifest
3
+ README.textile
4
+ Rakefile
5
+ lib/viewaide.rb
6
+ lib/viewaide/helpers.rb
7
+ lib/viewaide/helpers/date_helper.rb
8
+ lib/viewaide/helpers/form_helper.rb
9
+ lib/viewaide/helpers/grid_helper.rb
10
+ lib/viewaide/helpers/jquery_helper.rb
11
+ lib/viewaide/helpers/link_helper.rb
12
+ lib/viewaide/helpers/message_helper.rb
13
+ lib/viewaide/helpers/navigation_helper.rb
14
+ lib/viewaide/helpers/rjs_helper.rb
15
+ lib/viewaide/helpers/structure_helper.rb
16
+ lib/viewaide/helpers/table_helper.rb
17
+ lib/viewaide/rails_partial_caching.rb
18
+ rails/init.rb
19
+ tasks/viewaide_tasks.rake
20
+ test/date_helper_test.rb
21
+ test/form_helper_test.rb
22
+ test/grid_helper_test.rb
23
+ test/jquery_helper_test.rb
24
+ test/link_helper_test.rb
25
+ test/message_helper_test.rb
26
+ test/navigation_helper_test.rb
27
+ test/rjs_helper_test.rb
28
+ test/structure_helper_test.rb
29
+ test/table_helper_test.rb
30
+ test/test_helper.rb
31
+ viewaide.gemspec
data/README.textile ADDED
@@ -0,0 +1,3 @@
1
+ h1. Viewaide
2
+
3
+ Copyright (c) 2009 Josh Clayton, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require "rake"
2
+ require "rake/testtask"
3
+ require "rcov/rcovtask"
4
+
5
+ begin
6
+ require "jeweler"
7
+ Jeweler::Tasks.new do |gemspec|
8
+ gemspec.name = "viewaide"
9
+ gemspec.description = "Making your views easier"
10
+ gemspec.summary = "Making your views easier"
11
+ gemspec.email = "joshua.clayton@gmail.com"
12
+ gemspec.homepage = "http://github.com/joshuaclayton/viewaide"
13
+ gemspec.authors = ["Joshua Clayton"]
14
+ gemspec.add_dependency("actionview", ">= 2.1.0")
15
+ gemspec.add_dependency("activesupport", ">= 2.1.0")
16
+ gemspec.add_dependency("hpricot", ">= 0.8.1")
17
+ end
18
+ rescue LoadError
19
+ puts "Jeweler not available. Install it with: gem install jeweler"
20
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,15 @@
1
+ module Viewaide
2
+ module Helpers
3
+ module DateHelper
4
+
5
+ def datetime(dt = nil, default_text = "", format_string = :long)
6
+ dt ? dt.to_time.to_s(format_string) : default_text
7
+ end
8
+
9
+ def date(dt = nil, default_text = "", format_string = :long)
10
+ dt ? dt.to_date.to_s(format_string) : default_text
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,68 @@
1
+ module Viewaide
2
+ module Helpers
3
+ module FormHelper
4
+
5
+ def submit_button(value, *args)
6
+ options = args.extract_options!
7
+ css_classes = ["btn"] << options.delete(:class) << args
8
+ css_classes = clean_css_classes(css_classes, {"last" => last_column})
9
+
10
+ content_tag :button,
11
+ "<span>#{value}</span>",
12
+ { :value => value,
13
+ :type => "submit",
14
+ :class => css_classes
15
+ }.merge(options)
16
+ end
17
+
18
+ def set(*args, &block)
19
+ options = args.extract_options!
20
+ css_classes = [] << options.delete(:class) << args
21
+
22
+ if !other_than_grid?(args.map(&:to_s) - ["error", "last", last_column.to_s])
23
+ css_classes << "text"
24
+ end
25
+
26
+ if standardize_css_classes(css_classes).include?("textarea")
27
+ css_classes << "text"
28
+ end
29
+
30
+ css_classes = clean_css_classes(css_classes, {"last" => last_column})
31
+
32
+ html = clean_column(css_classes) do
33
+ content_tag :div,
34
+ capture(&block),
35
+ options.merge(:class => css_classes)
36
+ end
37
+
38
+ concat(html)
39
+ end
40
+
41
+ def fieldset(*args, &block)
42
+ options = args.extract_options!
43
+ css_classes = [] << options.delete(:class) << args
44
+ title = args.shift if args.first.is_a?(String)
45
+ legend = if title.blank?
46
+ ""
47
+ else
48
+ legend_opts = options.delete(:legend) || {}
49
+ legend_classes = clean_css_classes([legend_opts.delete(:class)] << "legend")
50
+ content_tag :h3,
51
+ title,
52
+ {:class => legend_classes}.merge(legend_opts)
53
+ end
54
+
55
+ css_classes = clean_css_classes(css_classes, {"last" => last_column})
56
+
57
+ html = clean_column(css_classes) do
58
+ content_tag :fieldset,
59
+ legend + capture(&block),
60
+ options.merge(:class => css_classes)
61
+ end
62
+
63
+ concat(html)
64
+ end
65
+
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,178 @@
1
+ module Viewaide
2
+ module Helpers
3
+ module GridHelper
4
+ MULTIPLES = {
5
+ :one_twentyfourth => (1/24.to_f),
6
+ :one_twelfth => (1/12.to_f),
7
+ :one_eigth => (1/8.to_f),
8
+ :one_sixth => (1/6.to_f),
9
+ :five_twentyfourths => (5/24.to_f),
10
+ :one_fourth => (1/4.to_f),
11
+ :seven_twentyfourths => (7/24.to_f),
12
+ :one_third => (1/3.to_f),
13
+ :three_eigths => (3/8.to_f),
14
+ :five_twelfths => (5/12.to_f),
15
+ :eleven_twentyfourths => (11/24.to_f),
16
+ :one_half => (1/2.to_f),
17
+ :half => (1/2.to_f),
18
+ :thirteen_twentyfourths => (13/24.to_f),
19
+ :seven_twelfths => (7/12.to_f),
20
+ :five_eigths => (5/8.to_f),
21
+ :two_thirds => (2/3.to_f),
22
+ :seventeen_twentyfourths => (17/24.to_f),
23
+ :three_fourths => (3/4.to_f),
24
+ :nineteen_twentyfourths => (19/24.to_f),
25
+ :five_sixths => (5/6.to_f),
26
+ :seven_eigths => (7/8.to_f),
27
+ :eleven_twelfths => (11/12.to_f),
28
+ :twentythree_twentyfourths => (23/24.to_f),
29
+ :full => (1.to_f)
30
+ }.freeze
31
+ MULTIPLE_FRACTIONS = MULTIPLES.keys.map {|key| key.to_s }.freeze
32
+
33
+ def self.easel_grid!
34
+ @@last_column = "col-last"
35
+ @@column_prefix = "col"
36
+ end
37
+
38
+ def self.blueprint_grid!
39
+ @@last_column = "last"
40
+ @@column_prefix = "span"
41
+ end
42
+
43
+ easel_grid!
44
+
45
+ def last_column
46
+ @@last_column
47
+ end
48
+
49
+ def column(*args, &block)
50
+ @_viewaide_column_count ||= application_width
51
+ col(*args, &block)
52
+ end
53
+
54
+ def container(size=nil, *args, &block)
55
+ opts = args.extract_options!
56
+ opts.merge!(:suppress_col => true) if size.nil?
57
+ args = args.insert(0, :container)
58
+ column(size, *([args, opts].flatten), &block)
59
+ end
60
+
61
+ def clean_column(classes, &block)
62
+ size = classes.scan(/#{column_prefix}-(\d+)/).flatten.last
63
+
64
+ if size.nil?
65
+ html = capture(&block)
66
+ if block_given? && block_is_within_action_view?(block)
67
+ concat(html)
68
+ else
69
+ html
70
+ end
71
+ else
72
+ size = size.to_i
73
+ increase_depth(size)
74
+ html = capture(&block)
75
+
76
+ if block_given? && block_is_within_action_view?(block)
77
+ concat(html)
78
+ decrease_depth(size)
79
+ else
80
+ decrease_depth(size)
81
+ html
82
+ end
83
+ end
84
+ end
85
+
86
+ def method_missing_with_viewaide_widths(call, *args)
87
+ # filter out any initial helper calls
88
+ found = false
89
+ MULTIPLE_FRACTIONS.each do |fraction|
90
+ found = true and break if call.to_s.include?(fraction)
91
+ end
92
+
93
+ method_missing_without_viewaide_widths(call, *args) and return unless found
94
+
95
+ # one of the widths is somewhere in the helper call; let's find it
96
+ call.to_s =~ /^((append|prepend|#{column_prefix})_)?(.+)$/
97
+ class_name = $2 || column_prefix
98
+ class_width = $3
99
+
100
+ if MULTIPLES.keys.include?(class_width.to_sym)
101
+ width = @_viewaide_column_count || application_width
102
+ "#{class_name}-#{(width*MULTIPLES[class_width.to_sym]).to_i}"
103
+ else
104
+ method_missing_without_viewaide_widths(call, *args)
105
+ end
106
+ end
107
+
108
+ alias_method_chain :method_missing, :viewaide_widths
109
+
110
+ private
111
+
112
+ def application_width; 24; end
113
+
114
+ def increase_depth(size)
115
+ @_viewaide_current_width ||= [application_width.to_s]
116
+
117
+ if @_viewaide_column_count.present?
118
+ @_viewaide_current_width.push(@_viewaide_column_count.to_s)
119
+ end
120
+
121
+ @_viewaide_column_count = if size.to_s =~ /^\d+$/
122
+ size.to_s.to_i
123
+ else
124
+ (@_viewaide_column_count*MULTIPLES[size]).to_i
125
+ end
126
+ end
127
+
128
+ def decrease_depth(size)
129
+ @_viewaide_column_count = if size.is_a?(Integer)
130
+ @_viewaide_current_width.last.to_i
131
+ else
132
+ (@_viewaide_column_count/MULTIPLES[size]).to_i
133
+ end
134
+
135
+ @_viewaide_current_width.pop
136
+ end
137
+
138
+ def col(*args, &block)
139
+ fractional_width = MULTIPLE_FRACTIONS.include?(args.first.to_s)
140
+ explicit_column_width = args.first.is_a?(Integer)
141
+ size = (fractional_width || explicit_column_width) ? args.shift : :full
142
+
143
+ increase_depth(size)
144
+ output_tag = generate_output_tag(size, *args, &block)
145
+
146
+ if block_given? && block_is_within_action_view?(block)
147
+ concat(output_tag)
148
+ decrease_depth(size)
149
+ else
150
+ decrease_depth(size)
151
+ output_tag
152
+ end
153
+ end
154
+
155
+ def generate_output_tag(size, *args, &block)
156
+ options = args.extract_options!
157
+
158
+ css_classes = [] << options.delete(:class) << args
159
+ unless options.delete(:suppress_col)
160
+ css_classes << "#{column_prefix}-#{@_viewaide_column_count}"
161
+ end
162
+
163
+ if (size.to_sym rescue nil) == :full && @_viewaide_column_count != application_width
164
+ css_classes << last_column
165
+ end
166
+
167
+ css_classes = clean_css_classes(css_classes, {"last" => last_column})
168
+ content_tag(options.delete(:tag) || :div,
169
+ capture(&block),
170
+ {:class => css_classes}.merge(options))
171
+ end
172
+
173
+ def column_prefix
174
+ @@column_prefix
175
+ end
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,21 @@
1
+ module Viewaide
2
+ module Helpers
3
+ module JqueryHelper
4
+
5
+ def document_ready(&block)
6
+ html = content_tag :script, :type => "text/javascript" do
7
+ %(
8
+ (function($) {
9
+ $(document).ready(function() {
10
+ #{capture(&block)}
11
+ });
12
+ })(jQuery);
13
+ )
14
+ end
15
+
16
+ concat(html)
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Viewaide
2
+ module Helpers
3
+ module LinkHelper
4
+
5
+ def link_button(*args, &block)
6
+ doc = Hpricot(link_to(*args, &block))
7
+ doc.at("a").inner_html = "<span>#{doc.at("a").inner_html}</span>"
8
+ (doc/:a).add_class("btn")
9
+ doc.to_html
10
+ end
11
+
12
+ def link_to_email(email_address, *args)
13
+ options = args.extract_options!
14
+ link = args.first.is_a?(String) ? h(args.first) : email_address
15
+ return link if email_address.blank?
16
+ link_to link, "mailto:#{email_address}", options
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,32 @@
1
+ module Viewaide
2
+ module Helpers
3
+ module MessageHelper
4
+
5
+ def messages(messages, options = {})
6
+ except_keys = [options[:except]].flatten.compact
7
+ only_keys = [options[:only]].flatten.compact
8
+
9
+ if except_keys.any? && only_keys.any?
10
+ raise ArgumentError, ":only and :except options conflict; use one"
11
+ end
12
+
13
+ keys = if except_keys.any?
14
+ messages.keys - except_keys
15
+ elsif only_keys.any?
16
+ messages.keys & only_keys
17
+ else
18
+ messages.keys
19
+ end
20
+
21
+ keys.map do |key|
22
+ if messages[key].present?
23
+ content_tag :p,
24
+ messages[key],
25
+ :class => [key, "box", "single-line"].join(" ")
26
+ end
27
+ end.join
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,29 @@
1
+ module Viewaide
2
+ module Helpers
3
+ module NavigationHelper
4
+ def tab(name, path, options = {}, li_options = {})
5
+ opts = parse_tab_options(name, li_options)
6
+
7
+ active = "active" if (opts[:active] == opts[:comparison]) || opts[:compare]
8
+ css_classes = [] << opts[:li_classes] << active
9
+ css_classes = clean_css_classes(css_classes)
10
+ li_options.merge!(:class => css_classes) if css_classes.present?
11
+
12
+ content_tag :li,
13
+ link_to(name, path, options),
14
+ li_options
15
+ end
16
+
17
+ private
18
+
19
+ def parse_tab_options(name, li_options = {})
20
+ returning({}) do |result|
21
+ result[:active] = li_options.delete(:active) || (name.gsub(/\s/, '').tableize || "")
22
+ result[:comparison] = li_options.delete(:active_compare) || controller.controller_name
23
+ result[:compare] = li_options.delete(:compare) || false
24
+ result[:li_classes] = li_options.delete(:class)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ module Viewaide
2
+ module Helpers
3
+ module RjsHelper
4
+
5
+ def inline_flash(page, flash, options = {})
6
+ container_id = options[:container] || "flash-container"
7
+ current_flash = flash
8
+
9
+ flash.discard unless options[:keep_flash]
10
+
11
+ if options[:replace]
12
+ page.replace_html container_id, messages(current_flash)
13
+ else
14
+ page.insert_html :top, container_id, messages(current_flash)
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,43 @@
1
+ module Viewaide
2
+ module Helpers
3
+ module StructureHelper
4
+
5
+ def blockquote(*args, &block)
6
+ options = args.extract_options!
7
+ author = options.delete(:author)
8
+ option_quote = textilize(options.delete(:quote))
9
+
10
+ bq = content_tag :blockquote,
11
+ option_quote.blank? ? capture(&block) : option_quote
12
+
13
+ html = if author
14
+ content_tag :div,
15
+ bq + content_tag(:cite, author),
16
+ :class => "quote-cited"
17
+ else
18
+ bq
19
+ end
20
+
21
+ concat(html)
22
+ end
23
+
24
+ def body(*args)
25
+ options = args.extract_options!
26
+ @_page_body_attributes ||= {}
27
+
28
+ css_classes = [] << @_page_body_attributes.delete(:class) << args
29
+
30
+ @_page_body_attributes = @_page_body_attributes.merge(options)
31
+ @_page_body_attributes[:class] = clean_css_classes(css_classes)
32
+
33
+ if block_given?
34
+ block = lambda { yield }
35
+
36
+ html = content_tag(:body, capture(&block), @_page_body_attributes)
37
+ concat(html)
38
+ end
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,61 @@
1
+ module Viewaide
2
+ module Helpers
3
+ module TableHelper
4
+
5
+ def zebra_row(options = {}, &block)
6
+ cycle_list = options.delete(:cycle_list) || [nil, "alt"]
7
+ css_classes = [cycle(*cycle_list)] << options.delete(:class)
8
+ css_classes = clean_css_classes(css_classes)
9
+
10
+ html = content_tag :tr,
11
+ capture(&block),
12
+ options.merge(:class => css_classes)
13
+ concat(html)
14
+ end
15
+
16
+ def recordset(*args, &block)
17
+ options = args.extract_options!
18
+ options[:table] ||= {}
19
+
20
+ headers = []
21
+ (options[:headers] || []).each_with_index do |header, index|
22
+ head = [header].flatten
23
+ opts = head.extract_options!
24
+
25
+ css_classes = [] << opts.delete(:class) << case index
26
+ when 0 then "first"
27
+ when (options[:headers].size - 1) then "last"
28
+ end
29
+
30
+ headers << if head.first =~ /^\<th/
31
+ th = Hpricot(head.first)
32
+ th_classes = th.at("th")["class"].join
33
+ th_classes = clean_css_classes([th_classes, css_classes])
34
+ th.at("th")["class"] = th_classes
35
+ th.to_html
36
+ else
37
+ content_tag :th,
38
+ head.first,
39
+ opts.merge(:class => clean_css_classes(css_classes))
40
+ end
41
+ end
42
+
43
+ table_classes = ["recordset", args] << options[:table].delete(:class)
44
+ css_classes = clean_css_classes(table_classes, {"last" => last_column})
45
+
46
+ html = clean_column(css_classes) do
47
+ table_options = options[:table]
48
+ table_options.merge!(:class => css_classes, :cellspacing => 0)
49
+ content_tag(:table,
50
+ content_tag(:thead, content_tag(:tr, headers.join)) + \
51
+ capture(&block),
52
+ table_options)
53
+ end
54
+
55
+ reset_cycle
56
+ concat(html)
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,81 @@
1
+ require "viewaide/helpers/date_helper"
2
+ require "viewaide/helpers/form_helper"
3
+ require "viewaide/helpers/link_helper"
4
+ require "viewaide/helpers/structure_helper"
5
+ require "viewaide/helpers/table_helper"
6
+ require "viewaide/helpers/grid_helper"
7
+ require "viewaide/helpers/message_helper"
8
+ require "viewaide/helpers/rjs_helper"
9
+ require "viewaide/helpers/jquery_helper"
10
+ require "viewaide/helpers/navigation_helper"
11
+
12
+ module Viewaide
13
+ module Helpers
14
+ include DateHelper
15
+ include FormHelper
16
+ include LinkHelper
17
+ include StructureHelper
18
+ include TableHelper
19
+ include GridHelper
20
+ include MessageHelper
21
+ include RjsHelper
22
+ include JqueryHelper
23
+ include NavigationHelper
24
+
25
+ protected
26
+
27
+ def other_than_grid?(classes)
28
+ (standardize_css_classes(classes).map {|s| s.to_s } -
29
+ Viewaide::Helpers::GridHelper::MULTIPLE_FRACTIONS).any?
30
+ end
31
+
32
+ def clean_css_classes(string_or_array, replace = {})
33
+ css_classes = [] + standardize_css_classes(string_or_array)
34
+
35
+ if replace.any?
36
+ replace.keys.each do |k|
37
+ if css_classes.include? k
38
+ css_classes.delete(k)
39
+ css_classes << replace[k]
40
+ end
41
+ end
42
+ end
43
+
44
+ fractions = css_classes & Viewaide::Helpers::GridHelper::MULTIPLE_FRACTIONS
45
+ if css_classes.any? && fractions.any?
46
+ fractions.each do |fraction|
47
+ css_classes.delete(fraction)
48
+ css_classes << self.send(fraction)
49
+ if fraction == "full" && @_viewaide_column_count != application_width
50
+ css_classes << last_column
51
+ end
52
+ end
53
+ end
54
+
55
+ css_classes.map {|s| s.strip }.reject {|s| s.blank? }.uniq.join(" ").strip
56
+ end
57
+
58
+ private
59
+
60
+ def standardize_css_classes(string_or_array)
61
+ [string_or_array].flatten.join(" ").split(/ /)
62
+ end
63
+
64
+ BLOCK_CALLED_FROM_ERB = 'defined? __in_erb_template'
65
+
66
+ if RUBY_VERSION < '1.9.0'
67
+ # Check whether we're called from an erb template.
68
+ # We'd return a string in any other case, but erb <%= ... %>
69
+ # can't take an <% end %> later on, so we have to use <% ... %>
70
+ # and implicitly concat.
71
+ def block_is_within_action_view?(block)
72
+ block && eval(BLOCK_CALLED_FROM_ERB, block)
73
+ end
74
+ else
75
+ def block_is_within_action_view?(block)
76
+ block && eval(BLOCK_CALLED_FROM_ERB, block.binding)
77
+ end
78
+ end
79
+
80
+ end
81
+ end