whurl_engine 1.2.2 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. data/README.markdown +2 -0
  2. data/Rakefile +2 -12
  3. data/app/assets/javascripts/whurl_engine/application.js +0 -159
  4. data/app/assets/javascripts/whurl_engine/jquery-ujs.js +315 -0
  5. data/app/assets/javascripts/whurl_engine/jquery.livequery.js +226 -0
  6. data/app/assets/javascripts/whurl_engine/whurl.js +136 -0
  7. data/app/assets/stylesheets/whurl_engine/application.css.scss +88 -1
  8. data/app/assets/stylesheets/whurl_engine/custom-theme/images/ui-bg_flat_100_ffffff_40x100.png +0 -0
  9. data/app/assets/stylesheets/whurl_engine/custom-theme/images/ui-bg_flat_10_000000_40x100.png +0 -0
  10. data/app/assets/stylesheets/whurl_engine/custom-theme/images/ui-bg_flat_18_f6ecd5_40x100.png +0 -0
  11. data/app/assets/stylesheets/whurl_engine/custom-theme/images/ui-bg_flat_20_666666_40x100.png +0 -0
  12. data/app/assets/stylesheets/whurl_engine/custom-theme/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  13. data/app/assets/stylesheets/whurl_engine/custom-theme/images/ui-bg_highlight-hard_100_dddddd_1x100.png +0 -0
  14. data/app/assets/stylesheets/whurl_engine/custom-theme/images/ui-bg_highlight-hard_100_eeeeee_1x100.png +0 -0
  15. data/app/assets/stylesheets/whurl_engine/custom-theme/images/ui-bg_highlight-hard_40_f6a828_1x100.png +0 -0
  16. data/app/assets/stylesheets/whurl_engine/custom-theme/images/ui-bg_highlight-soft_75_f1fbe5_1x100.png +0 -0
  17. data/app/assets/stylesheets/whurl_engine/custom-theme/images/ui-icons_1c94c4_256x240.png +0 -0
  18. data/app/assets/stylesheets/whurl_engine/custom-theme/images/ui-icons_222222_256x240.png +0 -0
  19. data/app/assets/stylesheets/whurl_engine/custom-theme/images/ui-icons_8cce3b_256x240.png +0 -0
  20. data/app/assets/stylesheets/whurl_engine/custom-theme/images/ui-icons_cd0a0a_256x240.png +0 -0
  21. data/app/assets/stylesheets/whurl_engine/custom-theme/images/ui-icons_ef8c08_256x240.png +0 -0
  22. data/app/assets/stylesheets/whurl_engine/custom-theme/images/ui-icons_ffffff_256x240.png +0 -0
  23. data/app/assets/stylesheets/whurl_engine/custom-theme/jquery-ui-1.8.16.custom.css +562 -0
  24. data/app/assets/stylesheets/whurl_engine/whurl.css.scss +128 -0
  25. data/app/controllers/whurl_engine/application_controller.rb +2 -0
  26. data/app/controllers/whurl_engine/categories_controller.rb +36 -0
  27. data/app/controllers/whurl_engine/parameter_definitions_controller.rb +32 -0
  28. data/app/controllers/whurl_engine/resources_controller.rb +36 -0
  29. data/app/controllers/whurl_engine/whurls_controller.rb +42 -0
  30. data/app/models/whurl_engine/category.rb +7 -0
  31. data/app/models/whurl_engine/parameter_definition.rb +7 -0
  32. data/app/models/whurl_engine/resource.rb +12 -0
  33. data/app/models/whurl_engine/whurl.rb +66 -0
  34. data/app/views/layouts/whurl_engine/application.html.haml +41 -44
  35. data/app/views/whurl_engine/categories/_category.html.haml +15 -0
  36. data/app/views/whurl_engine/categories/_form.html.haml +24 -0
  37. data/app/views/whurl_engine/categories/create.js.haml +5 -0
  38. data/app/views/whurl_engine/categories/destroy.js.haml +1 -0
  39. data/app/views/whurl_engine/categories/edit.js.haml +3 -0
  40. data/app/views/whurl_engine/categories/index.html.haml +6 -0
  41. data/app/views/whurl_engine/categories/new.js.haml +3 -0
  42. data/app/views/whurl_engine/categories/update.js.haml +6 -0
  43. data/app/views/whurl_engine/parameter_definitions/_form.html.haml +31 -0
  44. data/app/views/whurl_engine/parameter_definitions/_parameter_definition.html.haml +14 -0
  45. data/app/views/whurl_engine/parameter_definitions/create.js.haml +5 -0
  46. data/app/views/whurl_engine/parameter_definitions/destroy.js.haml +1 -0
  47. data/app/views/whurl_engine/parameter_definitions/edit.js.haml +3 -0
  48. data/app/views/whurl_engine/parameter_definitions/new.js.haml +3 -0
  49. data/app/views/whurl_engine/parameter_definitions/update.js.haml +6 -0
  50. data/app/views/whurl_engine/resources/_form.html.haml +40 -0
  51. data/app/views/whurl_engine/resources/_resource.html.haml +6 -0
  52. data/app/views/whurl_engine/resources/create.js.haml +5 -0
  53. data/app/views/whurl_engine/resources/destroy.js.haml +1 -0
  54. data/app/views/whurl_engine/resources/edit.js.haml +3 -0
  55. data/app/views/whurl_engine/resources/new.js.haml +3 -0
  56. data/app/views/whurl_engine/resources/show.html.haml +29 -0
  57. data/app/views/whurl_engine/resources/update.js.haml +6 -0
  58. data/app/views/whurl_engine/shared/_error.js.haml +2 -0
  59. data/app/views/whurl_engine/whurls/_edit_form.html.haml +29 -0
  60. data/app/views/whurl_engine/whurls/_form.html.haml +42 -0
  61. data/app/views/whurl_engine/{whurl_requests → whurls}/_input_set.html.haml +1 -1
  62. data/app/views/whurl_engine/whurls/_response.html.haml +5 -0
  63. data/app/views/whurl_engine/whurls/_whurl.html.haml +18 -0
  64. data/app/views/whurl_engine/whurls/destroy.js.haml +2 -0
  65. data/app/views/whurl_engine/{whurl_requests → whurls}/edit.html.haml +3 -4
  66. data/app/views/whurl_engine/whurls/edit.js.haml +3 -0
  67. data/app/views/whurl_engine/whurls/show.html.haml +4 -0
  68. data/config/initializers/httparty/request.rb +9 -0
  69. data/config/initializers/httparty/response.rb +7 -25
  70. data/config/initializers/net/http_generic_request.rb +9 -0
  71. data/config/initializers/nilclass.rb +5 -0
  72. data/config/initializers/string.rb +32 -0
  73. data/config/routes.rb +6 -3
  74. data/db/migrate/20110209054322_create_whurls_table.rb +14 -0
  75. data/db/migrate/20111210192724_add_description_to_whurl.rb +9 -0
  76. data/db/migrate/20111210201607_add_custom_url_to_whurl.rb +9 -0
  77. data/db/migrate/20111211030904_rename_whurls_to_requests.rb +8 -0
  78. data/db/migrate/20111220233800_create_fields_for_whurl_requests.rb +10 -10
  79. data/db/migrate/20120106033416_create_responses.rb +11 -0
  80. data/db/migrate/20120106190508_create_categories_table.rb +9 -0
  81. data/db/migrate/20120107011500_create_resources_table.rb +15 -0
  82. data/db/migrate/20120107065652_create_parameters_table.rb +12 -0
  83. data/db/migrate/20120110205837_rename_parameters_to_parameter_definitions.rb +5 -0
  84. data/db/migrate/20120110205853_rename_requests_to_whurls.rb +19 -0
  85. data/db/migrate/20120111014928_add_raw_request_to_whurls.rb +5 -0
  86. data/lib/whurl_engine/version.rb +1 -1
  87. metadata +140 -177
  88. data/app/assets/stylesheets/whurl_engine/code.css.scss +0 -13
  89. data/app/assets/stylesheets/whurl_engine/jquery.ui.autocomplete.css.scss +0 -55
  90. data/app/assets/stylesheets/whurl_engine/whurls.css.scss +0 -358
  91. data/app/controllers/whurl_engine/whurl_requests_controller.rb +0 -52
  92. data/app/models/whurl_engine/any_client.rb +0 -7
  93. data/app/models/whurl_engine/whurl_request.rb +0 -37
  94. data/app/views/whurl_engine/whurl_requests/_form.html.haml +0 -51
  95. data/app/views/whurl_engine/whurl_requests/show.html.haml +0 -1
  96. data/db/migrate/20110209054322_create_whurl_requests_table.rb +0 -14
  97. data/db/migrate/20111210192724_add_description_to_whurl_requests.rb +0 -9
  98. data/db/migrate/20111210201607_add_custom_url_to_whurl_requests.rb +0 -9
  99. data/db/migrate/20111221010348_copy_data_to_new_fields.rb +0 -39
  100. data/db/migrate/20111221011145_drop_data_from_whurl_requestss.rb +0 -9
  101. data/test/dummy/Rakefile +0 -7
  102. data/test/dummy/app/assets/javascripts/application.js +0 -9
  103. data/test/dummy/app/assets/stylesheets/application.css +0 -7
  104. data/test/dummy/app/controllers/application_controller.rb +0 -3
  105. data/test/dummy/app/helpers/application_helper.rb +0 -2
  106. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  107. data/test/dummy/app/views/layouts/whurl_engine/application.html.haml +0 -56
  108. data/test/dummy/config/application.rb +0 -45
  109. data/test/dummy/config/boot.rb +0 -10
  110. data/test/dummy/config/database.sample.yml +0 -17
  111. data/test/dummy/config/database.yml +0 -17
  112. data/test/dummy/config/environment.rb +0 -5
  113. data/test/dummy/config/environments/development.rb +0 -30
  114. data/test/dummy/config/environments/production.rb +0 -60
  115. data/test/dummy/config/environments/test.rb +0 -39
  116. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  117. data/test/dummy/config/initializers/inflections.rb +0 -10
  118. data/test/dummy/config/initializers/mime_types.rb +0 -5
  119. data/test/dummy/config/initializers/secret_token.rb +0 -7
  120. data/test/dummy/config/initializers/session_store.rb +0 -8
  121. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  122. data/test/dummy/config/locales/en.yml +0 -5
  123. data/test/dummy/config/routes.rb +0 -4
  124. data/test/dummy/config.ru +0 -4
  125. data/test/dummy/db/schema.rb +0 -31
  126. data/test/dummy/log/development.log +0 -80
  127. data/test/dummy/public/404.html +0 -26
  128. data/test/dummy/public/422.html +0 -26
  129. data/test/dummy/public/500.html +0 -26
  130. data/test/dummy/public/favicon.ico +0 -0
  131. data/test/dummy/script/rails +0 -6
  132. data/test/dummy/tmp/cache/assets/C6D/500/sprockets%2Fa60a126c749b135b047d47c9116f7010 +0 -0
  133. data/test/dummy/tmp/cache/assets/C85/760/sprockets%2F67d86b8948de011684953a4b2d30c280 +0 -0
  134. data/test/dummy/tmp/cache/assets/CB8/FF0/sprockets%2F789d641670bfe15510034f63eb635ff8 +0 -0
  135. data/test/dummy/tmp/cache/assets/CB9/340/sprockets%2Fc383855f31a6f96cfad737d462029019 +0 -0
  136. data/test/dummy/tmp/cache/assets/CBD/A60/sprockets%2F8e7fec8543052565b343a93c7b174d78 +0 -0
  137. data/test/dummy/tmp/cache/assets/CE6/140/sprockets%2F97da5e07430aa9249a9633e88c5073ea +0 -0
  138. data/test/dummy/tmp/cache/assets/D01/6E0/sprockets%2F3974f4fd239fc76f2d9700899609da7e +0 -0
  139. data/test/dummy/tmp/cache/assets/D09/700/sprockets%2F68ca75710cc64126df7d1a6638ca16d1 +0 -0
  140. data/test/dummy/tmp/cache/assets/D18/6A0/sprockets%2Fdf8266a6da92a42ce29b771318176dc9 +0 -0
  141. data/test/dummy/tmp/cache/assets/D1C/440/sprockets%2Fd5f5cd99df7e614581ec5f2256403e33 +0 -0
  142. data/test/dummy/tmp/cache/assets/D2C/DB0/sprockets%2F802a914bb4d275c8c34a3aa5ec134d54 +0 -0
  143. data/test/dummy/tmp/cache/assets/D39/780/sprockets%2F0da3fdc5af08e2938cc21829334b1d48 +0 -0
  144. data/test/dummy/tmp/cache/assets/D3D/010/sprockets%2F389924c72fa75b369a0e61aacfa230e8 +0 -0
  145. data/test/dummy/tmp/cache/assets/D4C/8D0/sprockets%2F024e6456c9877ae45cdb28d7d16e06fe +0 -0
  146. data/test/dummy/tmp/cache/assets/D66/D70/sprockets%2Ffdf1533f584b1ba62ce0ca92848b14d3 +0 -0
  147. data/test/dummy/tmp/cache/assets/D6F/A10/sprockets%2F3fa8ede522a279bfc12817267d1ca57e +0 -0
  148. data/test/dummy/tmp/cache/assets/D9D/AC0/sprockets%2F082b6d491d42f3a4febe3cb4d0983e6d +0 -0
  149. data/test/dummy/tmp/cache/assets/DA4/180/sprockets%2F118a85fd98ac4bda49f6485fd41aca44 +0 -0
  150. data/test/dummy/tmp/cache/assets/DB4/C60/sprockets%2F6ee1d0983f6518ba5dc089797bfc6ffa +0 -0
  151. data/test/dummy/tmp/cache/assets/DBD/7A0/sprockets%2F872f84e976ebe7654383dfdc7cfec1c6 +0 -0
  152. data/test/dummy/tmp/cache/assets/DD7/420/sprockets%2F11c951f1c4e2bfc0d7c1b10e0bb1b2be +0 -0
  153. data/test/dummy/tmp/cache/assets/E29/F00/sprockets%2F5b6ee8619eacc15eaf00c329cedff60a +0 -0
  154. data/test/dummy/tmp/cache/assets/E5D/680/sprockets%2F5ef33ecefc8e09aafd4d2725c0ef0a9c +0 -0
  155. data/test/fixtures/whurl_engine/whurl_requests.yml +0 -11
  156. data/test/functional/whurl_engine/api_call_controller_test.rb +0 -21
  157. data/test/integration/navigation_test.rb +0 -10
  158. data/test/test_helper.rb +0 -10
  159. data/test/unit/helpers/whurl_engine/api_call_helper_test.rb +0 -6
  160. data/test/unit/whurl_engine/whurl_request_test.rb +0 -9
  161. data/test/whurl_engine_test.rb +0 -7
  162. /data/app/views/whurl_engine/{whurl_requests → whurls}/new.html.haml +0 -0
data/README.markdown CHANGED
@@ -46,6 +46,8 @@ To do so, simply create a new application.html.haml in the corresponding directo
46
46
 
47
47
  ## Goals for this project
48
48
 
49
+ Whurl is inspired by [hurl](http://hurl.it/), and the [Apigee Console](https://apigee.com/console/)
50
+
49
51
  * Use HTTPS for all requests, assume all users are passing sensitive data
50
52
  * Provide a usable tool for working with APIs
51
53
  * Further the use of Whurl through viral "whurls" - the urls that represent an API request
data/Rakefile CHANGED
@@ -20,20 +20,10 @@ RDoc::Task.new(:rdoc) do |rdoc|
20
20
  rdoc.rdoc_files.include('lib/**/*.rb')
21
21
  end
22
22
 
23
- APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
23
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
24
24
  load 'rails/tasks/engine.rake'
25
25
 
26
26
 
27
27
  Bundler::GemHelper.install_tasks
28
28
 
29
- require 'rake/testtask'
30
-
31
- Rake::TestTask.new(:test) do |t|
32
- t.libs << 'lib'
33
- t.libs << 'test'
34
- t.pattern = 'test/**/*_test.rb'
35
- t.verbose = false
36
- end
37
-
38
-
39
- task :default => :test
29
+ task :default => 'app:spec'
@@ -1,159 +0,0 @@
1
- var headers = ["Accept",
2
- "Accept-Charset",
3
- "Accept-Encoding",
4
- "Accept-Language",
5
- "Authorization",
6
- "Cache-Control",
7
- "Connection",
8
- "Cookie",
9
- "Content-Length",
10
- "Content-MD5",
11
- "Content-Type",
12
- "Date",
13
- "Expect",
14
- "From",
15
- "Host",
16
- "If-Match",
17
- "If-Modified-Since",
18
- "If-None-Match",
19
- "If-Range",
20
- "If-Unmodified-Since",
21
- "Max-Forwards",
22
- "Pragma",
23
- "Proxy-Authorization",
24
- "Range",
25
- "Referer",
26
- "TE",
27
- "Upgrade",
28
- "User-Agent",
29
- "Via",
30
- "Warning"];
31
-
32
-
33
- var Whurl = {
34
- computePermalink: function() {
35
-
36
- },
37
- addHeader: function() {
38
- var $headerFields = $('.header_pair:last').clone();
39
- $headerFields.children('input').val("");
40
- $headerFields.children('input').attr('disabled', false);
41
- $($headerFields.children('input')[1]).focusin(function() {
42
- if($('.header_pair:last').children('input').val() != "") {
43
- Whurl.addHeader();
44
- }
45
- });
46
- $headerFields.hide().appendTo('#headers');
47
- var temp = Whurl.addHeader;
48
- $headerFields.slideDown('fast', function () { Whurl.addHeader = temp; });
49
-
50
- $(".header_pair:last").children(":eq(0)").autocomplete({source: headers});
51
-
52
- Whurl.addHeader = function () {};
53
- },
54
- deleteHeader: function(element) {
55
- var $paramFields = $(element).closest(".header_pair");
56
- $paramFields.slideUp(function() {
57
- $paramFields.remove();
58
- });
59
- },
60
- addParam: function() {
61
- var $paramFields = $('.param_pair:last').clone();
62
- $paramFields.children('input').val("");
63
- $paramFields.children('input').attr('disabled', false);
64
- $($paramFields.children('input')[1]).focusin(function() {
65
- if($('.param_pair:last').children('input').val() != "") {
66
- Whurl.addParam();
67
- }
68
- });
69
- $paramFields.hide().appendTo('#params');
70
- var temp = Whurl.addParam;
71
- $paramFields.slideDown('fast', function () { Whurl.addParam = temp; });
72
- Whurl.addParam = function () {};
73
- },
74
- deleteParam: function(element) {
75
- var $paramFields = $(element).closest(".param_pair");
76
- $paramFields.slideUp(function() {
77
- $paramFields.remove();
78
- });
79
- },
80
- updateBodyInput: function() {
81
- if($.inArray($('#whurl_request_http_method').val(), ["PUT", "POST"]) > -1) {
82
- $('textarea#whurl_request_body').attr('disabled', false);
83
- $('textarea#whurl_request_body').removeClass('textarea_disabled');
84
- } else {
85
- $('textarea#whurl_request_body').attr('disabled', true);
86
- $('textarea#whurl_request_body').addClass('textarea_disabled');
87
- }
88
- }
89
- };
90
-
91
- $(document).ready(function() {
92
- $(".header_pair:eq(1)").children(":eq(0)").autocomplete({source: headers});
93
-
94
- $('#add_header').click(function() {
95
- Whurl.addHeader();
96
- });
97
-
98
- $("#clearFields").click(function() {
99
- var restoreTextBox = $("#permalink_text_box").val();
100
- $("input[type=text]").val("");
101
- $("textarea").val("");
102
- $("#permalink_text_box").val(restoreTextBox);
103
- });
104
-
105
- $('.delete_header').live('click', function() {
106
- Whurl.deleteHeader(this);
107
- });
108
-
109
- $('#add_param').click(function() {
110
- Whurl.addParam();
111
- });
112
-
113
- $('.delete_param').live('click', function() {
114
- Whurl.deleteParam(this);
115
- });
116
-
117
- $('#permalink').click(function() {
118
- Whurl.generatePermalink();
119
- });
120
-
121
- $("#trashheaders").click(function() {
122
- $(".header_pair").each(function(i,e) {
123
- if (i != 0) {
124
- $(e).remove();
125
- }
126
- });
127
- Whurl.addHeader();
128
- });
129
-
130
- $("#trashqueries").click(function() {
131
- $(".param_pair").each(function(i,e) {
132
- if (i != 0) {
133
- $(e).remove();
134
- }
135
- });
136
- Whurl.addParam();
137
- });
138
-
139
- $('.header_pair').each(function(i) {
140
- $($(this).children('input')[1]).focusin(function() {
141
- if($('.header_pair:last').children('input').val() != "") {
142
- Whurl.addHeader();
143
- }
144
- });
145
- });
146
-
147
- $('.param_pair').each(function(i) {
148
- $($(this).children('input')[1]).focusin(function() {
149
- if($('.param_pair:last').children('input').val() != "") {
150
- Whurl.addParam();
151
- }
152
- });
153
- });
154
-
155
- $('#whurl_request_http_method').change(function() {
156
- Whurl.updateBodyInput();
157
- });
158
- Whurl.updateBodyInput();
159
- });
@@ -0,0 +1,315 @@
1
+ /**
2
+ * Unobtrusive scripting adapter for jQuery
3
+ *
4
+ * Requires jQuery 1.4.4 or later.
5
+ * https://github.com/rails/jquery-ujs
6
+
7
+ * Uploading file using rails.js
8
+ * =============================
9
+ *
10
+ * By default, browsers do not allow files to be uploaded via AJAX. As a result, if there are any non-blank file fields
11
+ * in the remote form, this adapter aborts the AJAX submission and allows the form to submit through standard means.
12
+ *
13
+ * The `ajax:aborted:file` event allows you to bind your own handler to process the form submission however you wish.
14
+ *
15
+ * Ex:
16
+ * $('form').live('ajax:aborted:file', function(event, elements){
17
+ * // Implement own remote file-transfer handler here for non-blank file inputs passed in `elements`.
18
+ * // Returning false in this handler tells rails.js to disallow standard form submission
19
+ * return false;
20
+ * });
21
+ *
22
+ * The `ajax:aborted:file` event is fired when a file-type input is detected with a non-blank value.
23
+ *
24
+ * Third-party tools can use this hook to detect when an AJAX file upload is attempted, and then use
25
+ * techniques like the iframe method to upload the file instead.
26
+ *
27
+ * Required fields in rails.js
28
+ * ===========================
29
+ *
30
+ * If any blank required inputs (required="required") are detected in the remote form, the whole form submission
31
+ * is canceled. Note that this is unlike file inputs, which still allow standard (non-AJAX) form submission.
32
+ *
33
+ * The `ajax:aborted:required` event allows you to bind your own handler to inform the user of blank required inputs.
34
+ *
35
+ * !! Note that Opera does not fire the form's submit event if there are blank required inputs, so this event may never
36
+ * get fired in Opera. This event is what causes other browsers to exhibit the same submit-aborting behavior.
37
+ *
38
+ * Ex:
39
+ * $('form').live('ajax:aborted:required', function(event, elements){
40
+ * // Returning false in this handler tells rails.js to submit the form anyway.
41
+ * // The blank required inputs are passed to this function in `elements`.
42
+ * return ! confirm("Would you like to submit the form with missing info?");
43
+ * });
44
+ */
45
+
46
+ (function($) {
47
+ // Shorthand to make it a little easier to call public rails functions from within rails.js
48
+ var rails;
49
+
50
+ $.rails = rails = {
51
+ // Link elements bound by jquery-ujs
52
+ linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote]',
53
+
54
+ // Form elements bound by jquery-ujs
55
+ formSubmitSelector: 'form',
56
+
57
+ // Form input elements bound by jquery-ujs
58
+ formInputClickSelector: 'form input[type=submit], form input[type=image], form button[type=submit], form button:not([type])',
59
+
60
+ // Form input elements disabled during form submission
61
+ disableSelector: 'input[data-disable-with], button[data-disable-with], textarea[data-disable-with]',
62
+
63
+ // Form input elements re-enabled after form submission
64
+ enableSelector: 'input[data-disable-with]:disabled, button[data-disable-with]:disabled, textarea[data-disable-with]:disabled',
65
+
66
+ // Form required input elements
67
+ requiredInputSelector: 'input[name][required],textarea[name][required]',
68
+
69
+ // Form file input elements
70
+ fileInputSelector: 'input:file',
71
+
72
+ // Make sure that every Ajax request sends the CSRF token
73
+ CSRFProtection: function(xhr) {
74
+ var token = $('meta[name="csrf-token"]').attr('content');
75
+ if (token) xhr.setRequestHeader('X-CSRF-Token', token);
76
+ },
77
+
78
+ // Triggers an event on an element and returns false if the event result is false
79
+ fire: function(obj, name, data) {
80
+ var event = $.Event(name);
81
+ obj.trigger(event, data);
82
+ return event.result !== false;
83
+ },
84
+
85
+ // Default confirm dialog, may be overridden with custom confirm dialog in $.rails.confirm
86
+ confirm: function(message) {
87
+ return confirm(message);
88
+ },
89
+
90
+ // Default ajax function, may be overridden with custom function in $.rails.ajax
91
+ ajax: function(options) {
92
+ return $.ajax(options);
93
+ },
94
+
95
+ // Submits "remote" forms and links with ajax
96
+ handleRemote: function(element) {
97
+ var method, url, data,
98
+ dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType);
99
+
100
+ if (rails.fire(element, 'ajax:before')) {
101
+
102
+ if (element.is('form')) {
103
+ method = element.attr('method');
104
+ url = element.attr('action');
105
+ data = element.serializeArray();
106
+ // memoized value from clicked submit button
107
+ var button = element.data('ujs:submit-button');
108
+ if (button) {
109
+ data.push(button);
110
+ element.data('ujs:submit-button', null);
111
+ }
112
+ } else {
113
+ method = element.data('method');
114
+ url = element.attr('href');
115
+ data = element.data('params') || null;
116
+ }
117
+
118
+ rails.ajax({
119
+ url: url, type: method || 'GET', data: data, dataType: dataType,
120
+ // stopping the "ajax:beforeSend" event will cancel the ajax request
121
+ beforeSend: function(xhr, settings) {
122
+ if (settings.dataType === undefined) {
123
+ xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
124
+ }
125
+ return rails.fire(element, 'ajax:beforeSend', [xhr, settings]);
126
+ },
127
+ success: function(data, status, xhr) {
128
+ element.trigger('ajax:success', [data, status, xhr]);
129
+ },
130
+ complete: function(xhr, status) {
131
+ element.trigger('ajax:complete', [xhr, status]);
132
+ },
133
+ error: function(xhr, status, error) {
134
+ element.trigger('ajax:error', [xhr, status, error]);
135
+ }
136
+ });
137
+ }
138
+ },
139
+
140
+ // Handles "data-method" on links such as:
141
+ // <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
142
+ handleMethod: function(link) {
143
+ var href = link.attr('href'),
144
+ method = link.data('method'),
145
+ csrf_token = $('meta[name=csrf-token]').attr('content'),
146
+ csrf_param = $('meta[name=csrf-param]').attr('content'),
147
+ form = $('<form method="post" action="' + href + '"></form>'),
148
+ metadata_input = '<input name="_method" value="' + method + '" type="hidden" />';
149
+
150
+ if (csrf_param !== undefined && csrf_token !== undefined) {
151
+ metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
152
+ }
153
+
154
+ form.hide().append(metadata_input).appendTo('body');
155
+ form.submit();
156
+ },
157
+
158
+ /* Disables form elements:
159
+ - Caches element value in 'ujs:enable-with' data store
160
+ - Replaces element text with value of 'data-disable-with' attribute
161
+ - Adds disabled=disabled attribute
162
+ */
163
+ disableFormElements: function(form) {
164
+ form.find(rails.disableSelector).each(function() {
165
+ var element = $(this), method = element.is('button') ? 'html' : 'val';
166
+ element.data('ujs:enable-with', element[method]());
167
+ element[method](element.data('disable-with'));
168
+ element.attr('disabled', 'disabled');
169
+ });
170
+ },
171
+
172
+ /* Re-enables disabled form elements:
173
+ - Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
174
+ - Removes disabled attribute
175
+ */
176
+ enableFormElements: function(form) {
177
+ form.find(rails.enableSelector).each(function() {
178
+ var element = $(this), method = element.is('button') ? 'html' : 'val';
179
+ if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
180
+ element.removeAttr('disabled');
181
+ });
182
+ },
183
+
184
+ /* For 'data-confirm' attribute:
185
+ - Fires `confirm` event
186
+ - Shows the confirmation dialog
187
+ - Fires the `confirm:complete` event
188
+
189
+ Returns `true` if no function stops the chain and user chose yes; `false` otherwise.
190
+ Attaching a handler to the element's `confirm` event that returns a `falsy` value cancels the confirmation dialog.
191
+ Attaching a handler to the element's `confirm:complete` event that returns a `falsy` value makes this function
192
+ return false. The `confirm:complete` event is fired whether or not the user answered true or false to the dialog.
193
+ */
194
+ allowAction: function(element) {
195
+ var message = element.data('confirm'),
196
+ answer = false, callback;
197
+ if (!message) { return true; }
198
+
199
+ if (rails.fire(element, 'confirm')) {
200
+ answer = rails.confirm(message);
201
+ callback = rails.fire(element, 'confirm:complete', [answer]);
202
+ }
203
+ return answer && callback;
204
+ },
205
+
206
+ // Helper function which checks for blank inputs in a form that match the specified CSS selector
207
+ blankInputs: function(form, specifiedSelector, nonBlank) {
208
+ var inputs = $(), input,
209
+ selector = specifiedSelector || 'input,textarea';
210
+ form.find(selector).each(function() {
211
+ input = $(this);
212
+ // Collect non-blank inputs if nonBlank option is true, otherwise, collect blank inputs
213
+ if (nonBlank ? input.val() : !input.val()) {
214
+ inputs = inputs.add(input);
215
+ }
216
+ });
217
+ return inputs.length ? inputs : false;
218
+ },
219
+
220
+ // Helper function which checks for non-blank inputs in a form that match the specified CSS selector
221
+ nonBlankInputs: function(form, specifiedSelector) {
222
+ return rails.blankInputs(form, specifiedSelector, true); // true specifies nonBlank
223
+ },
224
+
225
+ // Helper function, needed to provide consistent behavior in IE
226
+ stopEverything: function(e) {
227
+ $(e.target).trigger('ujs:everythingStopped');
228
+ e.stopImmediatePropagation();
229
+ return false;
230
+ },
231
+
232
+ // find all the submit events directly bound to the form and
233
+ // manually invoke them. If anyone returns false then stop the loop
234
+ callFormSubmitBindings: function(form) {
235
+ var events = form.data('events'), continuePropagation = true;
236
+ if (events !== undefined && events['submit'] !== undefined) {
237
+ $.each(events['submit'], function(i, obj){
238
+ if (typeof obj.handler === 'function') return continuePropagation = obj.handler(obj.data);
239
+ });
240
+ }
241
+ return continuePropagation;
242
+ }
243
+ };
244
+
245
+ // ajaxPrefilter is a jQuery 1.5 feature
246
+ if ('ajaxPrefilter' in $) {
247
+ $.ajaxPrefilter(function(options, originalOptions, xhr){ rails.CSRFProtection(xhr); });
248
+ } else {
249
+ $(document).ajaxSend(function(e, xhr){ rails.CSRFProtection(xhr); });
250
+ }
251
+
252
+ $(rails.linkClickSelector).live('click.rails', function(e) {
253
+ var link = $(this);
254
+ if (!rails.allowAction(link)) return rails.stopEverything(e);
255
+
256
+ if (link.data('remote') !== undefined) {
257
+ rails.handleRemote(link);
258
+ return false;
259
+ } else if (link.data('method')) {
260
+ rails.handleMethod(link);
261
+ return false;
262
+ }
263
+ });
264
+
265
+ $(rails.formSubmitSelector).live('submit.rails', function(e) {
266
+ var form = $(this),
267
+ remote = form.data('remote') !== undefined,
268
+ blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector),
269
+ nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
270
+
271
+ if (!rails.allowAction(form)) return rails.stopEverything(e);
272
+
273
+ // skip other logic when required values are missing or file upload is present
274
+ if (blankRequiredInputs && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
275
+ return rails.stopEverything(e);
276
+ }
277
+
278
+ if (remote) {
279
+ if (nonBlankFileInputs) {
280
+ return rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]);
281
+ }
282
+
283
+ // If browser does not support submit bubbling, then this live-binding will be called before direct
284
+ // bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
285
+ if (!$.support.submitBubbles && rails.callFormSubmitBindings(form) === false) return rails.stopEverything(e);
286
+
287
+ rails.handleRemote(form);
288
+ return false;
289
+ } else {
290
+ // slight timeout so that the submit button gets properly serialized
291
+ setTimeout(function(){ rails.disableFormElements(form); }, 13);
292
+ }
293
+ });
294
+
295
+ $(rails.formInputClickSelector).live('click.rails', function(event) {
296
+ var button = $(this);
297
+
298
+ if (!rails.allowAction(button)) return rails.stopEverything(event);
299
+
300
+ // register the pressed submit button
301
+ var name = button.attr('name'),
302
+ data = name ? {name:name, value:button.val()} : null;
303
+
304
+ button.closest('form').data('ujs:submit-button', data);
305
+ });
306
+
307
+ $(rails.formSubmitSelector).live('ajax:beforeSend.rails', function(event) {
308
+ if (this == event.target) rails.disableFormElements($(this));
309
+ });
310
+
311
+ $(rails.formSubmitSelector).live('ajax:complete.rails', function(event) {
312
+ if (this == event.target) rails.enableFormElements($(this));
313
+ });
314
+
315
+ })( jQuery );