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
@@ -0,0 +1,12 @@
1
+ module HocusPocus
2
+ module CommandLine
3
+ class CommandLineController < ActionController::Base
4
+ # XHR
5
+ def execute
6
+ #FIXME validate params
7
+ `#{params[:command]}`
8
+ flash.now[:notice] = "successfully executed: #{params[:command]}"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ HocusPocus::CommandLine::Engine.routes.draw do
2
+ post '/execute', :to => 'command_line#execute'
3
+ end
@@ -0,0 +1,15 @@
1
+ require 'hocus_pocus/command_line/middleware'
2
+
3
+ module HocusPocus
4
+ module CommandLine
5
+ class Engine < ::Rails::Engine
6
+ isolate_namespace HocusPocus::CommandLine
7
+
8
+ initializer 'hocus_pocus.command_line.add middleware' do |app|
9
+ if HocusPocus.config.enable_command_line
10
+ app.middleware.insert HocusPocus::Middleware, HocusPocus::CommandLine::Middleware
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ require 'hocus_pocus/middleware_util'
2
+
3
+ module HocusPocus
4
+ module CommandLine
5
+ class Middleware
6
+ include MiddlewareUtil
7
+
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ @app.call(env).tap do |status, headers, body|
14
+ if body.is_a?(ActionDispatch::Response) && body.request.format.html?
15
+ body.body = insert_text body.body, :after, /<div id="#{HocusPocus::CONTAINER}" .*?>/i, command_line_link
16
+ end
17
+ end
18
+ end
19
+
20
+ private
21
+ def command_line_link
22
+ %Q[<a href="#" onclick="$(this).closest('div').find('div.command_line_form').toggle()">cmd</a><div class="command_line_form" style="display:none;"><form method="post" action="/hocus_pocus/command_line/execute" data-remote="true"><input type="text" name="command" placeholder="Command?" style="width: 512px;" /><input type="submit" name="run" /></form></div>]
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ require 'hocus_pocus/command_line/engine'
2
+
3
+ module HocusPocus
4
+ module CommandLine
5
+ class Railtie < ::Rails::Railtie #:nodoc:
6
+ initializer 'hocus_pocus.command_line' do |app|
7
+ ActiveSupport.on_load(:after_initialize) do
8
+ if HocusPocus.config.enable_command_line
9
+ Rails.application.routes.append do
10
+ mount HocusPocus::CommandLine::Engine, :at => '/hocus_pocus/command_line'
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ module HocusPocus
2
+ module Editor
3
+ class EditorController < ActionController::Base
4
+ # XHR
5
+ def index
6
+ #FIXME haml
7
+ @path = "#{Rails.application.root}/app/views/#{params[:template]}.html.erb"
8
+ @file = File.read @path
9
+ #FIXME extract partials
10
+ end
11
+
12
+ # XHR
13
+ def save
14
+ @file = File.open(params[:path], 'w') do |f|
15
+ f.puts params[:file]
16
+ end
17
+ # redirect_to "#{params[:uri]}", :notice => "successfully edited #{params[:path]} file!"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,7 +1,7 @@
1
1
  <div id="__editor__">
2
2
  <h3>editing <%= @path %>...</h3>
3
3
 
4
- <%= form_tag save_editor_path, :remote => true, :id => HocusPocus::EDITOR do %>
4
+ <%= form_tag save_editor_path, :remote => true, :id => HocusPocus::Editor::EDITOR do %>
5
5
  <%= hidden_field_tag :path, @path %>
6
6
  <%= text_area_tag :file, @file, :size => '100x30' %>
7
7
  <div align="right">
@@ -0,0 +1,4 @@
1
+ HocusPocus::Editor::Engine.routes.draw do
2
+ get '/', :to => 'editor#index'
3
+ post '/save', :to => 'editor#save', :as => 'save_editor'
4
+ end
@@ -0,0 +1,15 @@
1
+ require 'hocus_pocus/editor/middleware'
2
+
3
+ module HocusPocus
4
+ module Editor
5
+ class Engine < ::Rails::Engine
6
+ isolate_namespace HocusPocus::Editor
7
+
8
+ initializer 'hocus_pocus.editor.add middleware' do |app|
9
+ if HocusPocus.config.enable_editor
10
+ app.middleware.insert HocusPocus::Middleware, HocusPocus::Editor::Middleware
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ require 'hocus_pocus/middleware_util'
2
+
3
+ module HocusPocus
4
+ module Editor
5
+ class Middleware
6
+ include MiddlewareUtil
7
+
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ @app.call(env).tap do |status, headers, body|
14
+ if body.is_a?(ActionDispatch::Response) && body.request.format.html?
15
+ if Thread.current[HocusPocus::Editor::VIEW_FILENAME]
16
+ body.body = insert_text body.body, :after, /<div id="#{HocusPocus::CONTAINER}" .*?>/i, %Q[#{edit_link}<br>#{partials}]
17
+ Thread.current[HocusPocus::Editor::VIEW_FILENAME] = nil
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+ def edit_link
25
+ %Q[<a href="/hocus_pocus/editor?template=#{Thread.current[HocusPocus::Editor::VIEW_FILENAME]}" data-remote="true" onclick="$(this).closest('div').find('div.partials').toggle()">edit</a>]
26
+ end
27
+
28
+ def partials
29
+ %Q[<div class="partials" style="display:none">#{(Thread.current[HocusPocus::Editor::PARTIAL_FILENAMES] || []).map(&:virtual_path).map {|v| '<a href="/hocus_pocus/editor?template=' + v + '" data-remote="true">' + v + '</a>'}.join('<br>')}</div>]
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,43 @@
1
+ require 'hocus_pocus/editor/engine'
2
+
3
+ module HocusPocus
4
+ module Editor
5
+ EDITOR = :__hocus_pocus_editor__
6
+ VIEW_FILENAME = :__hocus_pocus_view_filename__
7
+ PARTIAL_FILENAMES = :__hocus_pocus_partial_filenames__
8
+
9
+ class Railtie < ::Rails::Railtie #:nodoc:
10
+ initializer 'hocus_pocus.editor' do |app|
11
+ ActiveSupport.on_load(:after_initialize) do
12
+ if HocusPocus.config.enable_editor
13
+ Rails.application.routes.append do
14
+ mount HocusPocus::Editor::Engine, :at => '/hocus_pocus/editor'
15
+ end
16
+ end
17
+ end
18
+
19
+ ActiveSupport.on_load(:action_view) do
20
+ if HocusPocus.config.enable_editor
21
+ module ::ActionView
22
+ class PartialRenderer
23
+ def render_partial_with_filename_caching
24
+ (Thread.current[HocusPocus::Editor::PARTIAL_FILENAMES] ||= []) << @template unless @view.controller.class.name.starts_with?('HocusPocus::')
25
+ render_partial_without_filename_caching
26
+ end
27
+ alias_method_chain :render_partial, :filename_caching
28
+ end
29
+
30
+ class TemplateRenderer
31
+ def render_template_with_filename_caching(template, layout_name = nil, locals = {})
32
+ Thread.current[HocusPocus::Editor::VIEW_FILENAME] = template.virtual_path if @view.controller.try(:request).try(:format).try(:html?) && !@view.controller.class.name.starts_with?('HocusPocus::')
33
+ render_template_without_filename_caching template, layout_name, locals
34
+ end
35
+ alias_method_chain :render_template, :filename_caching
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,60 @@
1
+ require 'rails/generators'
2
+ require 'active_record/schema_dumper'
3
+
4
+ module HocusPocus
5
+ module Generator
6
+ class GeneratorController < ActionController::Base
7
+ def index
8
+ @name = params[:anything]
9
+ end
10
+
11
+ # XHR
12
+ def execute
13
+ #FIXME validate params
14
+ `#{params[:command]}`
15
+ flash.now[:notice] = "successfully executed: #{params[:command]}"
16
+ end
17
+
18
+ # XHR
19
+ def scaffold
20
+ #FIXME validate params
21
+ p options = params[:attr].select {|_k, v| v.present?}.map {|k, v| "#{v}:#{params[:type][k]}"}
22
+ execute_scaffold params[:name].dup, options
23
+ migrate
24
+ #FIXME notice doesn't appear?
25
+ # @return_path = '/' + (params[:return_path].present? ? params[:return_path] : params[:name])
26
+ @return_path = '/' + params[:name]
27
+ flash.now[:notice] = "successfully generated #{params[:name]} scaffold!"
28
+ # redirect_to "/#{return_path}", :notice => "successfully generated #{params[:name]} scaffold!"
29
+ end
30
+
31
+ private
32
+ def execute_scaffold(name, options)
33
+ overwriting_argv([name, options]) do
34
+ cmd = name.include?('/') ? 'nested_scaffold' : 'scaffold'
35
+ Rails::Generators.configure! Rails.application.config.generators
36
+ Rails::Generators.invoke cmd, [name, options], :behavior => :invoke, :destination_root => Rails.root
37
+ end
38
+ end
39
+
40
+ # `rake db:migrate`
41
+ def migrate
42
+ ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_path, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
43
+ if ActiveRecord::Base.schema_format == :ruby
44
+ File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", "w") do |file|
45
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
46
+ end
47
+ end
48
+ end
49
+
50
+ # a dirty workaround to make rspec-rails run
51
+ def overwriting_argv(value, &block)
52
+ original_argv = ARGV
53
+ Object.const_set :ARGV, value
54
+ block.call
55
+ ensure
56
+ Object.const_set :ARGV, original_argv
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,9 @@
1
+ module HocusPocus
2
+ module Generator
3
+ module GeneratorHelper
4
+ def type_select(field_name, options = {})
5
+ select_tag field_name, options_for_select(%w[string text integer float decimal datetime time date boolean references]), options
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,8 +1,8 @@
1
1
  <html>
2
2
  <head>
3
3
  <title>generator</title>
4
- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
5
- <script src="/assets/jquery_ujs.js" type="text/javascript"></script>
4
+ <%= javascript_include_tag 'application' %>
5
+ <%= csrf_meta_tags %>
6
6
  <script type="text/javascript">
7
7
  $('#add_attr_type').live('click', function() {
8
8
  var i = $('#attr_type input').length;
@@ -13,7 +13,7 @@
13
13
  </head>
14
14
  <body>
15
15
  <h1>generate scaffold</h1>
16
- <%= form_tag scaffold_generator_path, :remote => true, :id => HocusPocus::GENERATOR do %>
16
+ <%= form_tag scaffold_generator_path, :remote => true do %>
17
17
  <% if @name.include? '_' %>
18
18
  g scaffold
19
19
  <%= select_tag :name, options_for_select([@name, @name.gsub('_', '/')]) %>
@@ -0,0 +1,5 @@
1
+ HocusPocus::Generator::Engine.routes.draw do
2
+ post '/hocus_pocus/generator/scaffold', :to => 'generator#scaffold', :as => 'scaffold_generator'
3
+
4
+ get '/:anything', :to => 'generator#index', :constraints => {:anything => /.*/}
5
+ end
@@ -0,0 +1,7 @@
1
+ module HocusPocus
2
+ module Generator
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace HocusPocus::Generator
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,34 @@
1
+ require 'hocus_pocus/generator/engine'
2
+
3
+ module HocusPocus
4
+ module Generator
5
+ class Railtie < ::Rails::Railtie #:nodoc:
6
+ initializer 'hocus_pocus.generator' do |app|
7
+ ActiveSupport.on_load(:after_initialize) do
8
+ if HocusPocus.config.enable_generator
9
+ Rails.application.routes.append do
10
+ mount HocusPocus::Generator::Engine, :at => '/'
11
+ end
12
+ end
13
+ end
14
+
15
+ ActiveSupport.on_load(:action_view) do
16
+ if HocusPocus.config.enable_generator
17
+ class ::ActionView::Base
18
+ def method_missing(method, args = {}, &blk)
19
+ if method.to_s =~ /(new_|edit_)?(.*)(_path|_url)\z/
20
+ # to avoid DoubleRenderError
21
+ controller.instance_variable_set :@_response_body, nil
22
+ #FIXME preserve args
23
+ controller.redirect_to "/#{$2.pluralize}?return_path=#{method}(#{args})"
24
+ else
25
+ super
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,15 @@
1
+ module HocusPocus
2
+ module Recorder
3
+ class RecorderController < ActionController::Base
4
+ # XHR
5
+ def index
6
+ flash.keep HocusPocus::Recorder::SPEC
7
+ end
8
+
9
+ # XHR
10
+ def destroy
11
+ flash.discard HocusPocus::Recorder::SPEC
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1 @@
1
+ $('#<%= HocusPocus::CONTAINER %> div.spec').toggle().html('<pre><%= escape_javascript(flash[HocusPocus::Recorder::SPEC]) + '\n end' if flash[HocusPocus::Recorder::SPEC] %></pre><div align="right"><a href="/hocus_pocus/recorder" data-remote="true" data-method="delete">Clear</a></div></div>')
@@ -0,0 +1,4 @@
1
+ HocusPocus::Recorder::Engine.routes.draw do
2
+ get '/', :to => 'recorder#index'
3
+ delete '/', :to => 'recorder#destroy'
4
+ end
@@ -0,0 +1,10 @@
1
+ $('form').live 'submit', ->
2
+ scenario = [" scenario 'GENERATED' do"]
3
+ scenario.push " visit '#{window.location.pathname}'"
4
+ $(this).find('input[type=text],input[type=email],input[type=url],input[type=number],input[type=search],textarea').each ->
5
+ scenario.push " fill_in '#{$(this).attr('id')}', :with => '#{$(this).val()}'"
6
+ $(this).find('select').each ->
7
+ scenario.push " select '#{$(this).children(':selected').text()}', :from => '#{$(this).attr('id')}'"
8
+ scenario.push " click_button '#{$(this).find('input[type=submit]').val()}'"
9
+ $(this).append '<textarea id="<%= HocusPocus::Recorder::SPEC %>" name="<%= HocusPocus::Recorder::SPEC %>" style="height: 0px; width: 0px;" />'
10
+ $('#<%= HocusPocus::Recorder::SPEC %>').val(scenario.join('\n'))
@@ -0,0 +1,15 @@
1
+ require 'hocus_pocus/recorder/middleware'
2
+
3
+ module HocusPocus
4
+ module Recorder
5
+ class Engine < ::Rails::Engine
6
+ isolate_namespace HocusPocus::Recorder
7
+
8
+ initializer 'hocus_pocus.recorder.add middleware' do |app|
9
+ if HocusPocus.config.enable_scenario_recorder
10
+ app.middleware.insert HocusPocus::Middleware, HocusPocus::Recorder::Middleware
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module HocusPocus
2
+ module Recorder
3
+ class Filter
4
+ def self.before(controller)
5
+ # (controller.flash[HocusPocus::Recorder::SPEC] ||= '') << controller.params.delete(HocusPocus::Recorder::SPEC) if controller.params[HocusPocus::Recorder::SPEC]
6
+ controller.flash[HocusPocus::Recorder::SPEC] = controller.params.delete HocusPocus::Recorder::SPEC if controller.params[HocusPocus::Recorder::SPEC]
7
+ controller.flash.keep HocusPocus::Recorder::SPEC
8
+ end
9
+ end
10
+ end
11
+ end