hocus_pocus 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/engines/command_line/app/controllers/hocus_pocus/command_line/command_line_controller.rb +12 -0
  2. data/{app/views/hocus_pocus/generator → engines/command_line/app/views/hocus_pocus/command_line/command_line}/execute.js.erb +0 -0
  3. data/engines/command_line/config/routes.rb +3 -0
  4. data/engines/command_line/lib/hocus_pocus/command_line/engine.rb +15 -0
  5. data/engines/command_line/lib/hocus_pocus/command_line/middleware.rb +26 -0
  6. data/engines/command_line/lib/hocus_pocus/command_line/railtie.rb +17 -0
  7. data/engines/editor/app/controllers/hocus_pocus/editor/editor_controller.rb +21 -0
  8. data/{app/views/hocus_pocus → engines/editor/app/views/hocus_pocus/editor}/editor/_editor.html.erb +1 -1
  9. data/{app/views/hocus_pocus → engines/editor/app/views/hocus_pocus/editor}/editor/index.js.erb +0 -0
  10. data/{app/views/hocus_pocus → engines/editor/app/views/hocus_pocus/editor}/editor/save.js.erb +0 -0
  11. data/engines/editor/config/routes.rb +4 -0
  12. data/engines/editor/lib/hocus_pocus/editor/engine.rb +15 -0
  13. data/engines/editor/lib/hocus_pocus/editor/middleware.rb +33 -0
  14. data/engines/editor/lib/hocus_pocus/editor/railtie.rb +43 -0
  15. data/engines/generator/app/controllers/hocus_pocus/generator/generator_controller.rb +60 -0
  16. data/engines/generator/app/helpers/hocus_pocus/generator/generator_helper.rb +9 -0
  17. data/{app/views/hocus_pocus → engines/generator/app/views/hocus_pocus/generator}/generator/index.html.erb +3 -3
  18. data/{app/views/hocus_pocus → engines/generator/app/views/hocus_pocus/generator}/generator/scaffold.js.erb +0 -0
  19. data/engines/generator/config/routes.rb +5 -0
  20. data/engines/generator/lib/hocus_pocus/generator/engine.rb +7 -0
  21. data/engines/generator/lib/hocus_pocus/generator/railtie.rb +34 -0
  22. data/engines/recorder/app/controllers/hocus_pocus/recorder/recorder_controller.rb +15 -0
  23. data/{app/views/hocus_pocus/spec → engines/recorder/app/views/hocus_pocus/recorder/recorder}/destroy.js.erb +0 -0
  24. data/engines/recorder/app/views/hocus_pocus/recorder/recorder/index.js.erb +1 -0
  25. data/engines/recorder/config/routes.rb +4 -0
  26. data/engines/recorder/lib/assets/javascripts/recorder.js.coffee.erb +10 -0
  27. data/engines/recorder/lib/hocus_pocus/recorder/engine.rb +15 -0
  28. data/engines/recorder/lib/hocus_pocus/recorder/filter.rb +11 -0
  29. data/engines/recorder/lib/hocus_pocus/recorder/middleware.rb +32 -0
  30. data/engines/recorder/lib/hocus_pocus/recorder/railtie.rb +27 -0
  31. data/hocus_pocus.gemspec +1 -1
  32. data/lib/generators/hocus_pocus/config_generator.rb +1 -1
  33. data/lib/hocus_pocus.rb +1 -1
  34. data/lib/hocus_pocus/engine.rb +6 -3
  35. data/lib/hocus_pocus/middleware.rb +19 -0
  36. data/lib/hocus_pocus/middleware_util.rb +17 -0
  37. data/lib/hocus_pocus/railtie.rb +7 -44
  38. data/lib/hocus_pocus/version.rb +1 -1
  39. metadata +43 -22
  40. data/app/controllers/hocus_pocus/application_controller.rb +0 -4
  41. data/app/controllers/hocus_pocus/editor_controller.rb +0 -19
  42. data/app/controllers/hocus_pocus/generator_controller.rb +0 -47
  43. data/app/controllers/hocus_pocus/spec_controller.rb +0 -8
  44. data/app/helpers/hocus_pocus/application_helper.rb +0 -4
  45. data/app/helpers/hocus_pocus/generator_helper.rb +0 -7
  46. data/config/routes.rb +0 -16
  47. data/lib/hocus_pocus/filter.rb +0 -102
  48. data/vendor/assets/javascripts/jquery_ujs.js +0 -289
@@ -1,102 +0,0 @@
1
- module HocusPocus
2
- class Filter
3
- attr_reader :body
4
-
5
- def self.before(controller)
6
- controller.flash[HocusPocus::SPEC] = controller.params[HocusPocus::SPEC] if controller.params[HocusPocus::SPEC]
7
- controller.flash.keep(HocusPocus::SPEC)
8
- end
9
-
10
- def self.after(controller)
11
- if controller.request.format.html?
12
- filter = self.new(controller)
13
- filter.add_steak_recorder if HocusPocus.config.enable_scenario_recorder
14
- # filter.add_js
15
- unless controller.is_a?(HocusPocus::EditorController) || controller.is_a?(HocusPocus::GeneratorController)
16
- filter.add_buttons
17
- end
18
- filter.add_command_line if HocusPocus.config.enable_command_line
19
- controller.response.body = filter.body
20
- end
21
- end
22
-
23
- def initialize(controller)
24
- @controller = controller
25
- @template = controller.instance_variable_get(:@template)
26
- @body = controller.response.body
27
- end
28
-
29
- def add_steak_recorder
30
- #TODO exclude EDITOR and GENERATOR
31
- insert_text :before, /<\/body>/i, <<-TEPPAN
32
- <script type="text/javascript">
33
- $('form').live('submit', function() {
34
- var scenario = [" scenario '#{@controller.action_name.humanize} #{@controller.controller_name.humanize}' do"];
35
- scenario.push(" visit '" + window.location.href + "'");
36
- $(this).find('input[type=text],input[type=email],input[type=url],input[type=number],input[type=search],textarea').each(function() {
37
- scenario.push(" fill_in '" + $(this).attr('id') + "', :with => '" + $(this).val() + "'");
38
- });
39
- $(this).find('select').each(function() {
40
- scenario.push(" select '" + $(this).val() + "', :from => '" + $(this).attr('id') + "'");
41
- });
42
- scenario.push(" click_button '" + $(this).find('input[type=submit]').val() + "'");
43
- $(this).append('<textarea id="#{HocusPocus::SPEC}" name="#{HocusPocus::SPEC}" style="height: 0px; width: 0px;" />');
44
- $('##{HocusPocus::SPEC}').val(scenario.join('\\n'));
45
- })
46
- </script>
47
- TEPPAN
48
- end
49
-
50
-
51
- def add_js
52
- # insert_text :before, /<\/body>/i, %Q[<script type="text/javascript">alert('hoge!');</script>]
53
- end
54
-
55
- def add_buttons
56
- insert_text :before, /<\/body>/i, %Q[<div id="#{HocusPocus::CONTAINER}" style="position:absolute; top:0; right: 0; font-size: small;">#{[edit_link, spec_link].compact.join(' | ')}<br>#{partials}<br>#{spec}</div>]
57
- Thread.current[HocusPocus::VIEW_FILENAMES] = nil
58
- end
59
-
60
- def add_command_line
61
- insert_text :before, /<\/body>/i, %Q[<div style="position:absolute; bottom:0;"><form method="post" action="/generator/execute" data-remote="true"><input type="text" name="command" placeholder="Command?" style="width: 512px;" /><input type="submit" name="run" /></form></div>]
62
- end
63
-
64
- private
65
- def insert_text(position, pattern, new_text)
66
- index = case pattern
67
- when Regexp
68
- if match = @body.match(pattern)
69
- match.offset(0)[position == :before ? 0 : 1]
70
- else
71
- @body.size
72
- end
73
- else
74
- pattern
75
- end
76
- @body.insert index, new_text
77
- end
78
-
79
- def edit_link
80
- return nil unless HocusPocus.config.enable_editor
81
- #FIXME path
82
- #FIXME use @template somehow?
83
- %Q[<a href="/editor?template=#{@controller.controller_name}/#{@controller.action_name}" data-remote="true" onclick="$(this).closest('div').find('div.partials').toggle()">edit</a>]
84
- end
85
-
86
- def spec_link
87
- return nil unless HocusPocus.config.enable_scenario_recorder
88
- %Q[<a href="#" onclick="$(this).closest('div').find('div.spec').toggle();">spec</a>]
89
- end
90
-
91
- def partials
92
- return nil unless HocusPocus.config.enable_editor
93
- %Q[<div class="partials" style="display:none">#{(Thread.current[HocusPocus::VIEW_FILENAMES] || []).map(&:virtual_path).map {|v| '<a href="/editor?template=' + v + '" data-remote="true">' + v + '</a>'}.join('<br>')}</div>]
94
- end
95
-
96
- def spec
97
- return nil unless HocusPocus.config.enable_scenario_recorder
98
- #FIXME more assertions
99
- %Q[<div class="spec" style="display:none"><pre>#{CGI.unescape(@controller.flash[HocusPocus::SPEC]) + "\n end" if @controller.flash[HocusPocus::SPEC]}</pre><div align="right"><a href="/spec" data-remote="true" data-method="delete">Clear</a></div></div>]
100
- end
101
- end
102
- end
@@ -1,289 +0,0 @@
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
- // Submits "remote" forms and links with ajax
86
- handleRemote: function(element) {
87
- var method, url, data,
88
- dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType);
89
-
90
- if (rails.fire(element, 'ajax:before')) {
91
-
92
- if (element.is('form')) {
93
- method = element.attr('method');
94
- url = element.attr('action');
95
- data = element.serializeArray();
96
- // memoized value from clicked submit button
97
- var button = element.data('ujs:submit-button');
98
- if (button) {
99
- data.push(button);
100
- element.data('ujs:submit-button', null);
101
- }
102
- } else {
103
- method = element.data('method');
104
- url = element.attr('href');
105
- data = null;
106
- }
107
-
108
- $.ajax({
109
- url: url, type: method || 'GET', data: data, dataType: dataType,
110
- // stopping the "ajax:beforeSend" event will cancel the ajax request
111
- beforeSend: function(xhr, settings) {
112
- if (settings.dataType === undefined) {
113
- xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
114
- }
115
- return rails.fire(element, 'ajax:beforeSend', [xhr, settings]);
116
- },
117
- success: function(data, status, xhr) {
118
- element.trigger('ajax:success', [data, status, xhr]);
119
- },
120
- complete: function(xhr, status) {
121
- element.trigger('ajax:complete', [xhr, status]);
122
- },
123
- error: function(xhr, status, error) {
124
- element.trigger('ajax:error', [xhr, status, error]);
125
- }
126
- });
127
- }
128
- },
129
-
130
- // Handles "data-method" on links such as:
131
- // <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
132
- handleMethod: function(link) {
133
- var href = link.attr('href'),
134
- method = link.data('method'),
135
- csrf_token = $('meta[name=csrf-token]').attr('content'),
136
- csrf_param = $('meta[name=csrf-param]').attr('content'),
137
- form = $('<form method="post" action="' + href + '"></form>'),
138
- metadata_input = '<input name="_method" value="' + method + '" type="hidden" />';
139
-
140
- if (csrf_param !== undefined && csrf_token !== undefined) {
141
- metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
142
- }
143
-
144
- form.hide().append(metadata_input).appendTo('body');
145
- form.submit();
146
- },
147
-
148
- /* Disables form elements:
149
- - Caches element value in 'ujs:enable-with' data store
150
- - Replaces element text with value of 'data-disable-with' attribute
151
- - Adds disabled=disabled attribute
152
- */
153
- disableFormElements: function(form) {
154
- form.find(rails.disableSelector).each(function() {
155
- var element = $(this), method = element.is('button') ? 'html' : 'val';
156
- element.data('ujs:enable-with', element[method]());
157
- element[method](element.data('disable-with'));
158
- element.attr('disabled', 'disabled');
159
- });
160
- },
161
-
162
- /* Re-enables disabled form elements:
163
- - Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
164
- - Removes disabled attribute
165
- */
166
- enableFormElements: function(form) {
167
- form.find(rails.enableSelector).each(function() {
168
- var element = $(this), method = element.is('button') ? 'html' : 'val';
169
- if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
170
- element.removeAttr('disabled');
171
- });
172
- },
173
-
174
- // If message provided in 'data-confirm' attribute, fires `confirm` event and returns result of confirm dialog.
175
- // Attaching a handler to the element's `confirm` event that returns false cancels the confirm dialog.
176
- allowAction: function(element) {
177
- var message = element.data('confirm');
178
- return !message || (rails.fire(element, 'confirm') && confirm(message));
179
- },
180
-
181
- // Helper function which checks for blank inputs in a form that match the specified CSS selector
182
- blankInputs: function(form, specifiedSelector, nonBlank) {
183
- var inputs = $(), input,
184
- selector = specifiedSelector || 'input,textarea';
185
- form.find(selector).each(function() {
186
- input = $(this);
187
- // Collect non-blank inputs if nonBlank option is true, otherwise, collect blank inputs
188
- if (nonBlank ? input.val() : !input.val()) {
189
- inputs = inputs.add(input);
190
- }
191
- });
192
- return inputs.length ? inputs : false;
193
- },
194
-
195
- // Helper function which checks for non-blank inputs in a form that match the specified CSS selector
196
- nonBlankInputs: function(form, specifiedSelector) {
197
- return rails.blankInputs(form, specifiedSelector, true); // true specifies nonBlank
198
- },
199
-
200
- // Helper function, needed to provide consistent behavior in IE
201
- stopEverything: function(e) {
202
- e.stopImmediatePropagation();
203
- return false;
204
- },
205
-
206
- // find all the submit events directly bound to the form and
207
- // manually invoke them. If anyone returns false then stop the loop
208
- callFormSubmitBindings: function(form) {
209
- var events = form.data('events'), continuePropagation = true;
210
- if (events !== undefined && events['submit'] !== undefined) {
211
- $.each(events['submit'], function(i, obj){
212
- if (typeof obj.handler === 'function') return continuePropagation = obj.handler(obj.data);
213
- });
214
- }
215
- return continuePropagation;
216
- }
217
- };
218
-
219
- // ajaxPrefilter is a jQuery 1.5 feature
220
- if ('ajaxPrefilter' in $) {
221
- $.ajaxPrefilter(function(options, originalOptions, xhr){ rails.CSRFProtection(xhr); });
222
- } else {
223
- $(document).ajaxSend(function(e, xhr){ rails.CSRFProtection(xhr); });
224
- }
225
-
226
- $(rails.linkClickSelector).live('click.rails', function(e) {
227
- var link = $(this);
228
- if (!rails.allowAction(link)) return rails.stopEverything(e);
229
-
230
- if (link.data('remote') !== undefined) {
231
- rails.handleRemote(link);
232
- return false;
233
- } else if (link.data('method')) {
234
- rails.handleMethod(link);
235
- return false;
236
- }
237
- });
238
-
239
- $(rails.formSubmitSelector).live('submit.rails', function(e) {
240
- var form = $(this),
241
- remote = form.data('remote') !== undefined,
242
- blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector),
243
- nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
244
-
245
- if (!rails.allowAction(form)) return rails.stopEverything(e);
246
-
247
- // skip other logic when required values are missing or file upload is present
248
- if (blankRequiredInputs && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
249
- return !remote;
250
- }
251
-
252
- if (remote) {
253
- if (nonBlankFileInputs) {
254
- return rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]);
255
- }
256
-
257
- // If browser does not support submit bubbling, then this live-binding will be called before direct
258
- // bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
259
- if (!$.support.submitBubbles && rails.callFormSubmitBindings(form) === false) return rails.stopEverything(e);
260
-
261
- rails.handleRemote(form);
262
- return false;
263
- } else {
264
- // slight timeout so that the submit button gets properly serialized
265
- setTimeout(function(){ rails.disableFormElements(form); }, 13);
266
- }
267
- });
268
-
269
- $(rails.formInputClickSelector).live('click.rails', function(event) {
270
- var button = $(this);
271
-
272
- if (!rails.allowAction(button)) return rails.stopEverything(event);
273
-
274
- // register the pressed submit button
275
- var name = button.attr('name'),
276
- data = name ? {name:name, value:button.val()} : null;
277
-
278
- button.closest('form').data('ujs:submit-button', data);
279
- });
280
-
281
- $(rails.formSubmitSelector).live('ajax:beforeSend.rails', function(event) {
282
- if (this == event.target) rails.disableFormElements($(this));
283
- });
284
-
285
- $(rails.formSubmitSelector).live('ajax:complete.rails', function(event) {
286
- if (this == event.target) rails.enableFormElements($(this));
287
- });
288
-
289
- })( jQuery );