prototype-rails 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/Gemfile +7 -0
  2. data/README +20 -0
  3. data/Rakefile +10 -0
  4. data/lib/action_view/helpers/prototype_helper.rb +852 -0
  5. data/lib/action_view/helpers/scriptaculous_helper.rb +263 -0
  6. data/lib/action_view/template/handlers/rjs.rb +14 -0
  7. data/lib/prototype-rails.rb +17 -0
  8. data/lib/prototype-rails/javascript_helper.rb +65 -0
  9. data/lib/prototype-rails/on_load_action_controller.rb +2 -0
  10. data/lib/prototype-rails/on_load_action_view.rb +22 -0
  11. data/lib/prototype-rails/renderers.rb +12 -0
  12. data/lib/prototype-rails/rendering.rb +13 -0
  13. data/lib/prototype-rails/selector_assertions.rb +208 -0
  14. data/test/abstract_unit.rb +235 -0
  15. data/test/assert_select_test.rb +452 -0
  16. data/test/controller/caching_test.rb +46 -0
  17. data/test/controller/content_type_test.rb +16 -0
  18. data/test/controller/mime_responds_test.rb +213 -0
  19. data/test/controller/new_base/content_type_test.rb +19 -0
  20. data/test/controller/new_base/render_rjs_test.rb +71 -0
  21. data/test/controller/render_js_test.rb +22 -0
  22. data/test/fixtures/functional_caching/_partial.erb +3 -0
  23. data/test/fixtures/functional_caching/formatted_fragment_cached.js.rjs +6 -0
  24. data/test/fixtures/functional_caching/js_fragment_cached_with_partial.js.rjs +1 -0
  25. data/test/fixtures/old_content_type/render_default_for_rjs.rjs +1 -0
  26. data/test/fixtures/respond_to/all_types_with_layout.js.rjs +1 -0
  27. data/test/fixtures/respond_to/layouts/standard.html.erb +1 -0
  28. data/test/fixtures/respond_to/using_defaults.js.rjs +1 -0
  29. data/test/fixtures/respond_to/using_defaults_with_type_list.js.rjs +1 -0
  30. data/test/fixtures/respond_with/using_resource.js.rjs +1 -0
  31. data/test/fixtures/test/_one.html.erb +1 -0
  32. data/test/fixtures/test/_partial.html.erb +1 -0
  33. data/test/fixtures/test/_partial.js.erb +1 -0
  34. data/test/fixtures/test/_two.html.erb +1 -0
  35. data/test/fixtures/test/delete_with_js.rjs +2 -0
  36. data/test/fixtures/test/enum_rjs_test.rjs +6 -0
  37. data/test/fixtures/test/greeting.js.rjs +1 -0
  38. data/test/fixtures/test/render_explicit_html_template.js.rjs +1 -0
  39. data/test/fixtures/test/render_implicit_html_template.js.rjs +1 -0
  40. data/test/javascript_helper_test.rb +61 -0
  41. data/test/lib/controller/fake_models.rb +29 -0
  42. data/test/render_other_test.rb +257 -0
  43. data/test/template/prototype_helper_test.rb +476 -0
  44. data/test/template/render_test.rb +24 -0
  45. data/test/template/scriptaculous_helper_test.rb +86 -0
  46. data/vendor/assets/javascripts/controls.js +965 -0
  47. data/vendor/assets/javascripts/dragdrop.js +974 -0
  48. data/vendor/assets/javascripts/effects.js +1123 -0
  49. data/vendor/assets/javascripts/prototype.js +6082 -0
  50. data/vendor/assets/javascripts/prototype_ujs.js +208 -0
  51. metadata +127 -0
@@ -0,0 +1,46 @@
1
+ require 'fileutils'
2
+ require 'abstract_unit'
3
+
4
+ CACHE_DIR = 'test_cache'
5
+ # Don't change '/../temp/' cavalierly or you might hose something you don't want hosed
6
+ FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR)
7
+ ActionController::Base.page_cache_directory = FILE_STORE_PATH
8
+
9
+ class CachingController < ActionController::Base
10
+ abstract!
11
+
12
+ self.cache_store = :file_store, FILE_STORE_PATH
13
+ end
14
+
15
+ class FunctionalCachingController < CachingController
16
+ def js_fragment_cached_with_partial
17
+ respond_to do |format|
18
+ format.js
19
+ end
20
+ end
21
+
22
+ def formatted_fragment_cached
23
+ respond_to do |format|
24
+ format.js
25
+ end
26
+ end
27
+ end
28
+
29
+ class FunctionalFragmentCachingTest < ActionController::TestCase
30
+ def setup
31
+ super
32
+ @store = ActiveSupport::Cache::MemoryStore.new
33
+ @controller = FunctionalCachingController.new
34
+ @controller.perform_caching = true
35
+ @controller.cache_store = @store
36
+ @request = ActionController::TestRequest.new
37
+ @response = ActionController::TestResponse.new
38
+ end
39
+
40
+ def test_fragment_caching_in_rjs_partials
41
+ xhr :get, :js_fragment_cached_with_partial
42
+ assert_response :success
43
+ assert_match(/Old fragment caching in a partial/, @response.body)
44
+ assert_match("Old fragment caching in a partial", @store.read('views/test.host/functional_caching/js_fragment_cached_with_partial'))
45
+ end
46
+ end
@@ -0,0 +1,16 @@
1
+ require 'abstract_unit'
2
+
3
+ class OldContentTypeController < ActionController::Base
4
+ def render_default_for_rjs
5
+ end
6
+ end
7
+
8
+ class ContentTypeTest < ActionController::TestCase
9
+ tests OldContentTypeController
10
+
11
+ def test_default_for_rjs
12
+ xhr :post, :render_default_for_rjs
13
+ assert_equal Mime::JS, @response.content_type
14
+ assert_equal "utf-8", @response.charset
15
+ end
16
+ end
@@ -0,0 +1,213 @@
1
+ require 'abstract_unit'
2
+ require 'controller/fake_models'
3
+ require 'active_support/core_ext/hash/conversions'
4
+
5
+ class RespondToController < ActionController::Base
6
+ layout :set_layout
7
+
8
+ def html_xml_or_rss
9
+ respond_to do |type|
10
+ type.html { render :text => "HTML" }
11
+ type.xml { render :text => "XML" }
12
+ type.rss { render :text => "RSS" }
13
+ type.all { render :text => "Nothing" }
14
+ end
15
+ end
16
+
17
+ def js_or_html
18
+ respond_to do |type|
19
+ type.js { render :text => "JS" }
20
+ end
21
+ end
22
+
23
+ def json_or_yaml
24
+ respond_to do |type|
25
+ type.json { render :text => "JSON" }
26
+ type.yaml { render :text => "YAML" }
27
+ end
28
+ end
29
+
30
+ def html_or_xml
31
+ respond_to do |type|
32
+ type.html { render :text => "HTML" }
33
+ type.xml { render :text => "XML" }
34
+ type.all { render :text => "Nothing" }
35
+ end
36
+ end
37
+
38
+ def json_xml_or_html
39
+ respond_to do |type|
40
+ type.json { render :text => 'JSON' }
41
+ type.xml { render :xml => 'XML' }
42
+ type.html { render :text => 'HTML' }
43
+ end
44
+ end
45
+
46
+
47
+ def forced_xml
48
+ request.format = :xml
49
+
50
+ respond_to do |type|
51
+ type.html { render :text => "HTML" }
52
+ type.xml { render :text => "XML" }
53
+ end
54
+ end
55
+
56
+ def just_xml
57
+ respond_to do |type|
58
+ type.xml { render :text => "XML" }
59
+ end
60
+ end
61
+
62
+ def using_defaults
63
+ respond_to do |type|
64
+ type.js
65
+ end
66
+ end
67
+
68
+ def using_defaults_with_type_list
69
+ respond_to(:js)
70
+ end
71
+
72
+ def made_for_content_type
73
+ respond_to do |type|
74
+ type.rss { render :text => "RSS" }
75
+ type.atom { render :text => "ATOM" }
76
+ type.all { render :text => "Nothing" }
77
+ end
78
+ end
79
+
80
+ def custom_type_handling
81
+ respond_to do |type|
82
+ type.html { render :text => "HTML" }
83
+ type.custom("application/crazy-xml") { render :text => "Crazy XML" }
84
+ type.all { render :text => "Nothing" }
85
+ end
86
+ end
87
+
88
+
89
+ def custom_constant_handling
90
+ respond_to do |type|
91
+ type.html { render :text => "HTML" }
92
+ type.mobile { render :text => "Mobile" }
93
+ end
94
+ end
95
+
96
+ def custom_constant_handling_without_block
97
+ respond_to do |type|
98
+ type.html { render :text => "HTML" }
99
+ type.mobile
100
+ end
101
+ end
102
+
103
+ def handle_any
104
+ respond_to do |type|
105
+ type.html { render :text => "HTML" }
106
+ type.any(:js, :xml) { render :text => "Either JS or XML" }
107
+ end
108
+ end
109
+
110
+ def handle_any_any
111
+ respond_to do |type|
112
+ type.html { render :text => 'HTML' }
113
+ type.any { render :text => 'Whatever you ask for, I got it' }
114
+ end
115
+ end
116
+
117
+ def all_types_with_layout
118
+ respond_to do |type|
119
+ type.js
120
+ end
121
+ end
122
+
123
+
124
+ def iphone_with_html_response_type
125
+ request.format = :iphone if request.env["HTTP_ACCEPT"] == "text/iphone"
126
+
127
+ respond_to do |type|
128
+ type.html { @type = "Firefox" }
129
+ type.iphone { @type = "iPhone" }
130
+ end
131
+ end
132
+
133
+ def iphone_with_html_response_type_without_layout
134
+ request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
135
+
136
+ respond_to do |type|
137
+ type.html { @type = "Firefox"; render :action => "iphone_with_html_response_type" }
138
+ type.iphone { @type = "iPhone" ; render :action => "iphone_with_html_response_type" }
139
+ end
140
+ end
141
+
142
+ def rescue_action(e)
143
+ raise
144
+ end
145
+
146
+ protected
147
+ def set_layout
148
+ if ["all_types_with_layout", "iphone_with_html_response_type"].include?(action_name)
149
+ "respond_to/layouts/standard"
150
+ elsif action_name == "iphone_with_html_response_type_without_layout"
151
+ "respond_to/layouts/missing"
152
+ end
153
+ end
154
+ end
155
+
156
+
157
+ class RespondToControllerTest < ActionController::TestCase
158
+ tests RespondToController
159
+
160
+ def test_using_defaults
161
+ @request.accept = "text/javascript"
162
+ get :using_defaults
163
+ assert_equal "text/javascript", @response.content_type
164
+ assert_equal '$("body").visualEffect("highlight");', @response.body
165
+ end
166
+
167
+ def test_using_defaults_with_type_list
168
+ @request.accept = "text/javascript"
169
+ get :using_defaults_with_type_list
170
+ assert_equal "text/javascript", @response.content_type
171
+ assert_equal '$("body").visualEffect("highlight");', @response.body
172
+ end
173
+
174
+ def test_rjs_type_skips_layout
175
+ @request.accept = "text/javascript"
176
+ get :all_types_with_layout
177
+ assert_equal 'RJS for all_types_with_layout', @response.body
178
+ end
179
+
180
+ def test_xhr
181
+ xhr :get, :using_defaults
182
+ assert_equal '$("body").visualEffect("highlight");', @response.body
183
+ end
184
+ end
185
+
186
+ class RespondWithController < ActionController::Base
187
+ respond_to :html, :json, :js
188
+
189
+ def using_resource
190
+ respond_with(resource)
191
+ end
192
+ protected
193
+
194
+ def resource
195
+ Customer.new("david", request.delete? ? nil : 13)
196
+ end
197
+
198
+ def _render_js(js, options)
199
+ self.content_type ||= Mime::JS
200
+ self.response_body = js.respond_to?(:to_js) ? js.to_js : js
201
+ end
202
+ end
203
+
204
+ class RespondWithControllerTest < ActionController::TestCase
205
+ tests RespondWithController
206
+
207
+ def test_using_resource
208
+ @request.accept = "text/javascript"
209
+ get :using_resource
210
+ assert_equal "text/javascript", @response.content_type
211
+ assert_equal '$("body").visualEffect("highlight");', @response.body
212
+ end
213
+ end
@@ -0,0 +1,19 @@
1
+ require 'abstract_unit'
2
+
3
+ module ContentType
4
+ class ImpliedController < ActionController::Base
5
+ # Template's mime type is used if no content_type is specified
6
+
7
+ self.view_paths = [ActionView::FixtureResolver.new(
8
+ "content_type/implied/i_am_js_rjs.js.rjs" => "page.alert 'hello'"
9
+ )]
10
+ end
11
+
12
+ class ImpliedContentTypeTest < Rack::TestCase
13
+ test "sets Content-Type as text/javascript when rendering *.js" do
14
+ get "/content_type/implied/i_am_js_rjs", "format" => "js"
15
+
16
+ assert_header "Content-Type", "text/javascript; charset=utf-8"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,71 @@
1
+ require 'abstract_unit'
2
+
3
+ module RenderRjs
4
+ class BasicController < ActionController::Base
5
+ layout "application", :only => :index_respond_to
6
+
7
+ self.view_paths = [ActionView::FixtureResolver.new(
8
+ "layouts/application.html.erb" => "",
9
+ "render_rjs/basic/index.js.rjs" => "page[:customer].replace_html render(:partial => 'customer')",
10
+ "render_rjs/basic/index_html.js.rjs" => "page[:customer].replace_html :partial => 'customer'",
11
+ "render_rjs/basic/index_no_js.js.erb" => "<%= render(:partial => 'developer') %>",
12
+ "render_rjs/basic/_customer.js.erb" => "JS Partial",
13
+ "render_rjs/basic/_customer.html.erb" => "HTML Partial",
14
+ "render_rjs/basic/_developer.html.erb" => "HTML Partial",
15
+ "render_rjs/basic/index_locale.js.rjs" => "page[:customer].replace_html :partial => 'customer'",
16
+ "render_rjs/basic/_customer.da.html.erb" => "Danish HTML Partial",
17
+ "render_rjs/basic/_customer.da.js.erb" => "Danish JS Partial"
18
+ )]
19
+
20
+ def index
21
+ render
22
+ end
23
+
24
+ def index_respond_to
25
+ respond_to do |format|
26
+ format.js { render :action => "index_no_js" }
27
+ end
28
+ end
29
+
30
+ def index_locale
31
+ self.locale = :da
32
+ end
33
+ end
34
+
35
+ class TestBasic < Rack::TestCase
36
+ testing BasicController
37
+
38
+ def setup
39
+ @old_locale = I18n.locale
40
+ end
41
+
42
+ def teardown
43
+ I18n.locale = @old_locale
44
+ end
45
+
46
+ test "rendering a partial in an RJS template should pick the JS template over the HTML one" do
47
+ get :index, "format" => "js"
48
+ assert_response("$(\"customer\").update(\"JS Partial\");")
49
+ end
50
+
51
+ test "rendering a partial in an RJS template should pick the HTML one if no JS is available" do
52
+ get :index_no_js, "format" => "js"
53
+ assert_response("HTML Partial")
54
+ end
55
+
56
+ test "rendering a partial in an RJS template should pick the HTML one if no JS is available on respond_to" do
57
+ get :index_respond_to, "format" => "js"
58
+ assert_response("HTML Partial")
59
+ end
60
+
61
+ test "replacing an element with a partial in an RJS template should pick the HTML template over the JS one" do
62
+ get :index_html, "format" => "js"
63
+ assert_response("$(\"customer\").update(\"HTML Partial\");")
64
+ end
65
+
66
+ test "replacing an element with a partial in an RJS template with a locale should pick the localed HTML template" do
67
+ get :index_locale, "format" => "js"
68
+ assert_response("$(\"customer\").update(\"Danish HTML Partial\");")
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,22 @@
1
+ require 'abstract_unit'
2
+
3
+ class RenderJSTest < ActionController::TestCase
4
+ class TestController < ActionController::Base
5
+ protect_from_forgery
6
+
7
+ def self.controller_path
8
+ 'test'
9
+ end
10
+
11
+ def greeting
12
+ # let's just rely on the template
13
+ end
14
+ end
15
+
16
+ tests TestController
17
+
18
+ def test_render_with_default_from_accept_header
19
+ xhr :get, :greeting
20
+ assert_equal "$(\"body\").visualEffect(\"highlight\");", @response.body
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ <% cache do %>
2
+ Old fragment caching in a partial
3
+ <% end %>
@@ -0,0 +1,6 @@
1
+ page.assign 'title', 'Hey'
2
+ cache do
3
+ page['element_1'].visual_effect :highlight
4
+ page['element_2'].visual_effect :highlight
5
+ end
6
+ page.assign 'footer', 'Bye'
@@ -0,0 +1 @@
1
+ page.replace_html 'notices', :partial => 'partial'
@@ -0,0 +1 @@
1
+ page.alert 'hello world!'
@@ -0,0 +1 @@
1
+ page << "RJS for all_types_with_layout"
@@ -0,0 +1 @@
1
+ <html><div id="html"><%= yield %></div></html>
@@ -0,0 +1 @@
1
+ page[:body].visual_effect :highlight
@@ -0,0 +1 @@
1
+ page[:body].visual_effect :highlight
@@ -0,0 +1 @@
1
+ page[:body].visual_effect :highlight
@@ -0,0 +1 @@
1
+ <%= render :partial => "two" %> world
@@ -0,0 +1 @@
1
+ partial html
@@ -0,0 +1 @@
1
+ partial js
@@ -0,0 +1 @@
1
+ Hello
@@ -0,0 +1,2 @@
1
+ page.remove 'person'
2
+ page.visual_effect :highlight, "project-#{@project_id}"