viewaide 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ module ActionView
2
+ module Partials
3
+ private
4
+
5
+ def render_partial_with_viewaide(*args)
6
+ path = args.first[:partial]
7
+ locals = args.last || {}
8
+
9
+ viewaide_cached_column_counts = session[:viewaide_cached_column_counts] ||= {}
10
+
11
+ if viewaide_cached_column_counts.keys.include?(path)
12
+ @_viewaide_column_count = locals[:viewaide_width] || viewaide_cached_column_counts[path]
13
+ viewaide_cached_column_counts[path] = @_viewaide_column_count
14
+ else
15
+ if @_viewaide_column_count.is_a?(Fixnum) && path !~ /^layout/
16
+ viewaide_cached_column_counts[path] = @_viewaide_column_count
17
+ end
18
+ end
19
+
20
+ render_partial_without_viewaide(*args)
21
+ end
22
+
23
+ alias_method_chain :render_partial, :viewaide
24
+ end
25
+ end
26
+
27
+ module Viewaide
28
+ module PartialCaching
29
+ def self.included(base)
30
+ base.send :include, Viewaide::PartialCaching::InstanceMethods
31
+ base.before_filter :clear_viewaide_cache
32
+ end
33
+
34
+ module InstanceMethods
35
+ def clear_viewaide_cache
36
+ session[:viewaide_cached_column_counts] = nil unless request.xhr?
37
+ end
38
+ end
39
+ end
40
+ end
data/lib/viewaide.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "hpricot"
2
+ require "viewaide/helpers"
3
+ require "viewaide/rails_partial_caching"
data/rails/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "viewaide"
2
+
3
+ ActionView::Base.send :include, Viewaide::Helpers
4
+ ActionController::Base.send :include, Viewaide::PartialCaching
File without changes
@@ -0,0 +1,59 @@
1
+ require "test_helper"
2
+
3
+ class DateHelperTest < ActiveSupport::TestCase
4
+ include Viewaide::Helpers::DateHelper
5
+
6
+ context "datetime helper" do
7
+ should "default to an empty string if date is not supplied" do
8
+ assert_equal "", datetime(nil)
9
+ end
10
+
11
+ should "default to passed default text if date is not supplied" do
12
+ assert_equal "default text", datetime(nil, "default text")
13
+ end
14
+
15
+ should "default to :long format for date" do
16
+ timestamp = Time.now
17
+ assert_equal timestamp.to_s(:long), datetime(timestamp)
18
+ end
19
+
20
+ should "use passed format if applicable" do
21
+ timestamp = Time.now
22
+ assert_equal timestamp.to_s(:short), datetime(timestamp, "", :short)
23
+ end
24
+
25
+ should "convert passed data to a time" do
26
+ datestamp = Date.today
27
+ timestamp = datestamp.to_time
28
+ assert_equal timestamp.to_s(:long), datetime(datestamp)
29
+ end
30
+ end
31
+
32
+ context "date helper" do
33
+ should "default to an empty string if date is not supplied" do
34
+ assert_equal "", date(nil)
35
+ end
36
+
37
+ should "default to passed default text if date is not supplied" do
38
+ assert_equal "default text", date(nil, "default text")
39
+ end
40
+
41
+ should "default to :long format for date" do
42
+ datestamp = Date.today
43
+ assert_equal datestamp.to_s(:long), date(datestamp)
44
+ end
45
+
46
+ should "use passed format if applicable" do
47
+ datestamp = Date.today
48
+ assert_equal datestamp.to_s(:short), date(datestamp, "", :short)
49
+ end
50
+
51
+ should "convert passed data to a time" do
52
+ timestamp = Time.now
53
+ datestamp = timestamp.to_date
54
+
55
+ assert_equal datestamp.to_s(:long), date(timestamp)
56
+ end
57
+ end
58
+
59
+ end
@@ -0,0 +1,135 @@
1
+ require "test_helper"
2
+
3
+ class FormHelperTest < Viewaide::ViewTestCase
4
+
5
+ context "submit_button" do
6
+
7
+ should "default with the correct structure" do
8
+ show_view "<%= submit_button 'Create' %>" do
9
+ assert_select "button.btn[type=submit][value=Create]" do
10
+ assert_select "span", "Create"
11
+ end
12
+ end
13
+ end
14
+
15
+ should "allow adding additional classes" do
16
+ show_view "<%= submit_button 'Create', 'adtl-class', :dumb %>" do
17
+ assert_select "button.btn.adtl-class.dumb[type=submit][value=Create]" do
18
+ assert_select "span", "Create"
19
+ end
20
+ end
21
+ end
22
+
23
+ should "handle additional attributes set" do
24
+ show_view "<%= submit_button 'Create', :kls, :id => 'my-id', :type => 'image' %>" do
25
+ assert_select "button.btn.kls#my-id[type=image][value=Create]" do
26
+ assert_select "span", "Create"
27
+ end
28
+ end
29
+ end
30
+
31
+ should "allow overriding of value" do
32
+ show_view "<%= submit_button 'Create', :value => 'override' %>" do
33
+ assert_select "button.btn[type=submit][value=override]" do
34
+ assert_select "span", "Create"
35
+ end
36
+ end
37
+ end
38
+
39
+ end
40
+
41
+ context "set" do
42
+
43
+ should "default with the correct structure" do
44
+ show_view "<% set do %>words<% end %>" do
45
+ assert_select "div.text", "words"
46
+ end
47
+ end
48
+
49
+ should "allow adding/overriding classes" do
50
+ show_view "<% set :checkbox do %>words<% end %>" do
51
+ assert_select "div.checkbox", "words"
52
+ end
53
+ end
54
+
55
+ should "handle additional attributes set" do
56
+ show_view "<% set :id => 'custom-id' do %>words<% end %>" do
57
+ assert_select "div.text#custom-id", "words"
58
+ end
59
+ end
60
+
61
+ should "assign default class if width class is passed as the only class" do
62
+ show_view "<% set :half do %>words<% end %>" do
63
+ assert_select "div.text.col-12", "words"
64
+ end
65
+ end
66
+
67
+ should "assign default class if last class is passed as the only class" do
68
+ show_view "<% set :half, :last do %>words<% end %>" do
69
+ assert_select "div.col-12.text.col-last", "words"
70
+ end
71
+ end
72
+
73
+ should "assign default class if error class is passed" do
74
+ show_view "<% set :half, :last, :error do %>words<% end %>" do
75
+ assert_select "div.col-12.text.col-last.error", "words"
76
+ end
77
+ end
78
+
79
+ should "assign default class if the class textarea is present" do
80
+ show_view "<% set :textarea do %>words<% end %>" do
81
+ assert_select "div.textarea.text", "words"
82
+ end
83
+ end
84
+
85
+ end
86
+
87
+ context "fieldset" do
88
+
89
+ should "default with the correct structure" do
90
+ show_view "<% fieldset do %>words<% end %>" do
91
+ assert_select "fieldset", "words"
92
+ end
93
+ end
94
+
95
+ should "assign the first argument as the legend if it is a string" do
96
+ show_view "<% fieldset 'User Information' do %>words<% end %>" do
97
+ assert_select "fieldset", /words/ do
98
+ assert_select "h3.legend", "User Information"
99
+ end
100
+ end
101
+ end
102
+
103
+ should "allow adding fieldset classes" do
104
+ show_view "<% fieldset :hform, 'col-last' do %>words<% end %>" do
105
+ assert_select "fieldset.hform.col-last", "words"
106
+ end
107
+ end
108
+
109
+ should "allow adding fieldset classes and a legend" do
110
+ show_view "<% fieldset 'User Information', :hform, 'col-last' do %>words<% end %>" do
111
+ assert_select "fieldset.hform.col-last", /words/ do
112
+ assert_select "h3.legend", "User Information"
113
+ end
114
+ end
115
+ end
116
+
117
+ should "allow assignment of legend attributes" do
118
+ show_view "<% fieldset 'User Information', :hform, :legend => {:class => 'lgnd', :id => 'legend-id'} do %>words<% end %>" do
119
+ assert_select "fieldset.hform", /words/ do
120
+ assert_select "h3.legend.lgnd#legend-id", "User Information"
121
+ end
122
+ end
123
+ end
124
+
125
+ should "allow assignment of fieldset attributes" do
126
+ show_view "<% fieldset 'User Information', :hform, :id => 'my-fieldset' do %>words<% end %>" do
127
+ assert_select "fieldset.hform#my-fieldset", /words/ do
128
+ assert_select "h3.legend", "User Information"
129
+ end
130
+ end
131
+ end
132
+
133
+ end
134
+
135
+ end
@@ -0,0 +1,226 @@
1
+ require "test_helper"
2
+
3
+ class GridHelperTest < Viewaide::ViewTestCase
4
+
5
+ context "advanced grid structures" do
6
+
7
+ should "properly assign classes for a simple column layout" do
8
+ template = %(
9
+ <% container do %>
10
+ <% column do %>
11
+ <% column :half, :id => "primary" do %>
12
+ <% column :one_third do %>
13
+ one third of one half of 24 is 4
14
+ <% end %>
15
+ <% column :one_third, :last, prepend_one_third do %>
16
+ one third of one half of 24 is 4 (but prepended 4 as well)
17
+ <% end %>
18
+ <hr/>
19
+ more text
20
+ <% end %>
21
+ <% column :half, :last, :id => "secondary" do %>
22
+ second column
23
+ <% end %>
24
+ <hr/>
25
+ text
26
+ <% end %>
27
+ <% end %>
28
+ )
29
+
30
+ show_view template do
31
+ assert_select ".container", 1
32
+ assert_select ".container.col-24", 0
33
+ assert_select ".col-24", 1
34
+ assert_select ".col-12", 2
35
+ assert_select ".col-12#primary", 1
36
+ assert_select ".col-12#secondary", 1
37
+ assert_select ".col-4", 2
38
+ assert_select ".prepend-4", 1
39
+ assert_select ".col-24.col-last", 0
40
+ assert_select ".col-12.col-last", 1
41
+ assert_select ".col-4.col-last", 1
42
+ assert_select "hr", 2
43
+ end
44
+ end
45
+
46
+ should "properly assign classes for generic helpers" do
47
+ template = %(
48
+ <% column do %>
49
+ <% fieldset :hform, :half do %>
50
+ <% set :one_third do %>text<% end %>
51
+ <% set :two_thirds, :last do %>more text<% end %>
52
+ <% end %>
53
+ <% recordset :half, :last do %>table<% end %>
54
+ <% end %>
55
+ )
56
+
57
+ show_view template do
58
+ assert_select "div.col-24" do
59
+ assert_select "fieldset.hform.col-12" do
60
+ assert_select "div.col-4", "text"
61
+ assert_select "div.col-8.col-last", "more text"
62
+ end
63
+ assert_select "table.col-12.col-last", "table"
64
+ end
65
+ end
66
+ end
67
+
68
+ should "properly assign classes for generic helpers without column wrappers" do
69
+ template = %(
70
+ <% fieldset :hform, :half do %>
71
+ <% set :one_third do %>text<% end %>
72
+ <% set :two_thirds, :last do %>more text<% end %>
73
+ <% column do %>
74
+ <% column :one_third do %>one third<% end %>
75
+ <% column :two_thirds, :last do %>
76
+ <% column :half do %>half<% end %>
77
+ <% column :half, :last do %>last half<% end %>
78
+ <% end %>
79
+ <% end %>
80
+ <% end %>
81
+ <% column :one_third do %>
82
+ <% column :one_fourth do %>two wide<% end %>
83
+ <% column :half do %>four wide<% end %>
84
+ <% column :one_fourth, :last do %>two more wide<% end %>
85
+ <% end %>
86
+ <% recordset :one_sixth, :last do %>table<% end %>
87
+ )
88
+
89
+ show_view template do
90
+ assert_select "fieldset.hform.col-12" do
91
+ assert_select "div.col-4", "text"
92
+ assert_select "div.col-8.col-last", "more text"
93
+
94
+ assert_select "div.col-12.col-last" do
95
+ assert_select "div.col-4", "one third"
96
+ assert_select "div.col-8.col-last" do
97
+ assert_select "div.col-4", "half"
98
+ assert_select "div.col-4.col-last", "last half"
99
+ end
100
+ end
101
+ end
102
+ assert_select "div.col-8" do
103
+ assert_select "div.col-2", "two wide"
104
+ assert_select "div.col-4", "four wide"
105
+ assert_select "div.col-2.col-last", "two more wide"
106
+ end
107
+ assert_select "table.col-4.col-last", "table"
108
+ end
109
+ end
110
+
111
+ should "properly assign classes for a deeply-nested view" do
112
+ template = %(
113
+ <% container :full do %>
114
+ <% column :half do %>
115
+ <% fieldset :hform, :half do %>
116
+ <% set :one_third do %>text<% end %>
117
+ <% set :two_thirds, :last do %>more text<% end %>
118
+ <% end %>
119
+ <% recordset :half, :last do %>table<% end %>
120
+ <% end %>
121
+ <% column :one_third do %>one third!<% end %>
122
+ <% column :one_sixth, :last do %>
123
+ <% fieldset :vform, :full do %>
124
+ <% set do %>text<% end %>
125
+ <% end %>
126
+ <% end %>
127
+ <% end %>
128
+ )
129
+
130
+ show_view template do
131
+ assert_select "div.container.col-24", 1
132
+ assert_select "div.container" do
133
+ assert_select "div.col-12" do
134
+ assert_select "fieldset.hform.col-6" do
135
+ assert_select "div.col-2", "text"
136
+ assert_select "div.col-4.col-last", "more text"
137
+ end
138
+ assert_select "table.col-6.col-last", "table"
139
+ end
140
+
141
+ assert_select "div.col-8", "one third!"
142
+
143
+ assert_select "div.col-4.col-last" do
144
+ assert_select "fieldset.vform.col-4.col-last" do
145
+ assert_select "div", "text"
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ should "properly assign classes when using Blueprint grid" do
153
+ template = %(
154
+ <% container do %>
155
+ <% column do %>
156
+ <% column :half, :id => "primary" do %>
157
+ <% column :one_third do %>
158
+ one third of one half of 24 is 4
159
+ <% end %>
160
+ <% column :one_third, :last, prepend_one_third do %>
161
+ one third of one half of 24 is 4 (but prepended 4 as well)
162
+ <% end %>
163
+ <hr/>
164
+ more text
165
+ <% end %>
166
+ <% column :half, :last, :id => "secondary" do %>
167
+ second column
168
+ <% end %>
169
+ <hr/>
170
+ text
171
+ <% end %>
172
+ <% end %>
173
+ )
174
+ Viewaide::Helpers::GridHelper.blueprint_grid!
175
+ show_view template do
176
+ assert_select ".container", 1
177
+ assert_select ".span-24", 1
178
+ assert_select ".span-12", 2
179
+ assert_select ".span-12#primary", 1
180
+ assert_select ".span-12#secondary", 1
181
+ assert_select ".span-4", 2
182
+ assert_select ".prepend-4", 1
183
+ assert_select ".span-24.last", 0
184
+ assert_select ".span-12.last", 1
185
+ assert_select ".span-4.last", 1
186
+ assert_select "hr", 2
187
+ end
188
+ Viewaide::Helpers::GridHelper.easel_grid!
189
+ end
190
+ end
191
+
192
+ context "column" do
193
+
194
+ should "allow assigning options hash without having to define a width" do
195
+ show_view %(
196
+ <% column :id => "my-custom-id", :class => "content" do %>words<% end %>
197
+ ) do
198
+ assert_select "div.col-24.content#my-custom-id", "words"
199
+ end
200
+ end
201
+
202
+ should "allow explicit column assignment" do
203
+ show_view %(
204
+ <% column 6, :sidebar do %>
205
+ <% column :id => "main" do %>main sidebar<% end %>
206
+ <% column :half do %>three<% end %>
207
+ <% column :one_third do %>two<% end %>
208
+ <% column 1, :last do %>one<% end %>
209
+ <% end %>
210
+ ) do
211
+ assert_select "div.col-6.sidebar" do
212
+ assert_select "div.col-6.col-last#main", "main sidebar"
213
+ assert_select "div.col-3", "three"
214
+ assert_select "div.col-2", "two"
215
+ assert_select "div.col-1.col-last", "one"
216
+ end
217
+ end
218
+ end
219
+
220
+ should "allow tag overriding" do
221
+ show_view %(<% column :tag => :section do %>content<% end %>) do
222
+ assert_select "section.col-24:not([tag=section])", "content"
223
+ end
224
+ end
225
+ end
226
+ end
@@ -0,0 +1,30 @@
1
+ require "test_helper"
2
+
3
+ class JqueryHelperTest < Viewaide::ViewTestCase
4
+
5
+ context "document_ready" do
6
+
7
+ setup do
8
+ @whitespace = '\s+?'
9
+ @anything = '(.|\s)+?'
10
+
11
+ @anon_function_start_regex = Regexp.escape "(function($) {"
12
+ @document_ready_start_regex = Regexp.escape "$(document).ready(function() {"
13
+
14
+ @document_ready_end_regex = Regexp.escape "});"
15
+ @anon_function_end_regex = Regexp.escape "})(jQuery);"
16
+ end
17
+
18
+ should "properly build the document ready script tag" do
19
+ show_view %(
20
+ <% document_ready do %>
21
+ alert("hello!");
22
+ <% end %>
23
+ ) do
24
+ assert_select "script[type=?]", "text/javascript", /#{@anon_function_start_regex}#{@whitespace}#{@document_ready_start_regex}#{@whitespace}alert#{@anything}#{@document_ready_end_regex}#{@whitespace}#{@anon_function_end_regex}/
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,44 @@
1
+ require 'test_helper'
2
+
3
+ class LinkHelperTest < Viewaide::ViewTestCase
4
+ context "link_button" do
5
+
6
+ should "default with the correct structure" do
7
+ show_view "<%= link_button 'Link Text', '#' %>" do
8
+ assert_select "a.btn[href=#]" do
9
+ assert_select "span", "Link Text"
10
+ end
11
+ end
12
+ end
13
+
14
+ should "allow the same assignment as link_to" do
15
+ show_view "<%= link_button 'Link Text', '#', :class => 'my-button', :id => 'link-text' %>" do
16
+ assert_select "a.btn.my-button#link-text[href=#]" do
17
+ assert_select "span", "Link Text"
18
+ end
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ context "link_to_email" do
25
+ should "default with the correct structure" do
26
+ show_view "<%= link_to_email 'test@example.com' %>" do
27
+ assert_select "a[href=mailto:test@example.com]", "test@example.com"
28
+ end
29
+ end
30
+
31
+ should "allow override of link text as the first argument after email" do
32
+ show_view "<%= link_to_email 'test@example.com', 'Send an email to Test User' %>" do
33
+ assert_select "a[href=mailto:test@example.com]", "Send an email to Test User"
34
+ end
35
+ end
36
+
37
+ should "allow the same assignment as link_to" do
38
+ show_view "<%= link_to_email 'test@example.com', :class => 'test-user-email', :id => 'user_1_email' %>" do
39
+ assert_select "a.test-user-email#user_1_email[href=mailto:test@example.com]", "test@example.com"
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,50 @@
1
+ require "test_helper"
2
+
3
+ class MessageHelperTest < Viewaide::ViewTestCase
4
+
5
+ context "messages" do
6
+
7
+ should "default with the correct structure" do
8
+ show_view %(<%= messages(:structure => "Flash message") %>) do
9
+ assert_select "p.structure.box.single-line", "Flash message"
10
+ end
11
+ end
12
+
13
+ should "display all flash messages present" do
14
+ show_view %(<%= messages(:structure => "Flash message", :error => "Warning message") %>) do
15
+ assert_select "p.structure", "Flash message"
16
+ assert_select "p.error", "Warning message"
17
+ end
18
+ end
19
+
20
+ should "not display a flash if it is blank" do
21
+ show_view %(<%= messages(:structure => "", :error => nil) %>) do
22
+ assert_select "p.structure", false
23
+ assert_select "p.error", false
24
+ end
25
+ end
26
+
27
+ should "filter by the :only option" do
28
+ show_view %(<%= messages({:structure => "Flash message", :error => "Warning message", :notice => "Notice!"}, {:only => [:structure, :error]}) %>) do
29
+ assert_select "p.structure", "Flash message"
30
+ assert_select "p.error", "Warning message"
31
+ assert_select "p.notice", false
32
+ end
33
+ end
34
+
35
+ should "filter by the :except option" do
36
+ show_view %(<%= messages({:structure => "Flash message", :error => "Warning message", :notice => "Notice!"}, {:except => [:structure, :error]}) %>) do
37
+ assert_select "p.structure", false
38
+ assert_select "p.error", false
39
+ assert_select "p.notice", "Notice!"
40
+ end
41
+ end
42
+
43
+ should "raise an error of :only and :except are both passed" do
44
+ assert_raise ArgumentError, /conflict/ do
45
+ show_view %(<%= messages({:structure => "Flash message"}, {:except => [:structure, :error], :only => :structure}) %>)
46
+ end
47
+ end
48
+ end
49
+
50
+ end