hocus_pocus 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.5.2)
19
+ rcov
20
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Akira Matsuda
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = hocus_pocus
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to hocus_pocus
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Akira Matsuda. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "hocus_pocus"
16
+ gem.homepage = "http://github.com/amatsuda/hocus_pocus"
17
+ gem.license = "MIT"
18
+ gem.summary = 'An Isolated Engine that casts a spell on your Rails 3.1 app'
19
+ gem.description = 'An Isolated Engine that casts a spell on your Rails 3.1 app'
20
+ gem.email = "ronnie@dio.jp"
21
+ gem.authors = ["Akira Matsuda"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ gem.add_runtime_dependency 'nested_scaffold', '>= 0.1.0'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "hocus_pocus #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,4 @@
1
+ module HocusPocus
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,19 @@
1
+ module HocusPocus
2
+ class EditorController < ActionController::Base
3
+ # XHR
4
+ def index
5
+ #FIXME haml
6
+ @path = "#{Rails.application.root}/app/views/#{params[:template]}.html.erb"
7
+ @file = File.read @path
8
+ #FIXME extract partials
9
+ end
10
+
11
+ # XHR
12
+ def save
13
+ @file = File.open(params[:path], 'w') do |f|
14
+ f.puts params[:file]
15
+ end
16
+ # redirect_to "#{params[:uri]}", :notice => "successfully edited #{params[:path]} file!"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,47 @@
1
+ require 'rails/generators'
2
+ require 'active_record/schema_dumper'
3
+
4
+ module HocusPocus
5
+ class GeneratorController < ActionController::Base
6
+ def index
7
+ @name = params[:anything]
8
+ end
9
+
10
+ # XHR
11
+ def execute
12
+ #FIXME validate params
13
+ `#{params[:command]}`
14
+ flash.now[:notice] = "successfully executed: #{params[:command]}"
15
+ end
16
+
17
+ # XHR
18
+ def scaffold
19
+ #FIXME validate params
20
+ p options = params[:attr].select {|_k, v| v.present?}.map {|k, v| "#{v}:#{params[:type][k]}"}
21
+ execute_scaffold params[:name].dup, options
22
+ migrate
23
+ #FIXME notice doesn't appear?
24
+ # @return_path = '/' + (params[:return_path].present? ? params[:return_path] : params[:name])
25
+ @return_path = '/' + params[:name]
26
+ flash.now[:notice] = "successfully generated #{params[:name]} scaffold!"
27
+ # redirect_to "/#{return_path}", :notice => "successfully generated #{params[:name]} scaffold!"
28
+ end
29
+
30
+ private
31
+ def execute_scaffold(name, options)
32
+ cmd = name.include?('/') ? 'nested_scaffold' : 'scaffold'
33
+ Rails::Generators.configure!
34
+ Rails::Generators.invoke cmd, [name, options], :behavior => :invoke, :destination_root => Rails.root
35
+ end
36
+
37
+ # `rake db:migrate`
38
+ def migrate
39
+ ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_path, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
40
+ if ActiveRecord::Base.schema_format == :ruby
41
+ File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", "w") do |file|
42
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,4 @@
1
+ module HocusPocus
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,7 @@
1
+ module HocusPocus
2
+ module GeneratorHelper
3
+ def type_select(field_name, options = {})
4
+ select_tag field_name, options_for_select(%w[string text integer float decimal datetime time date boolean references]), options
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ <div id="__editor__">
2
+ <h3>editing <%= @path %>...</h3>
3
+
4
+ <%= form_tag save_editor_path, :remote => true, :id => HocusPocus::EDITOR do %>
5
+ <%= hidden_field_tag :path, @path %>
6
+ <%= text_area_tag :file, @file, :size => '100x30' %>
7
+ <%= submit_tag 'save' %>
8
+ <% end %>
9
+
10
+ <hr>
11
+ </div>
@@ -0,0 +1,2 @@
1
+ $('#__editor__').remove();
2
+ $('body').prepend('<%= escape_javascript render('editor') %>');
@@ -0,0 +1,2 @@
1
+ $('#__editor__').remove();
2
+ location.href = location.href;
@@ -0,0 +1 @@
1
+ alert('<%= flash[:notice] %>');
@@ -0,0 +1,33 @@
1
+ <html>
2
+ <head>
3
+ <title>generator</title>
4
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
5
+ <script src="/hocus_pocus/javascripts/rails.js" type="text/javascript"></script>
6
+ <script type="text/javascript">
7
+ $('#add_attr_type').live('click', function() {
8
+ var i = $('#attr_type input').length;
9
+ $('#attr_type').append('<%= escape_javascript text_field_tag('attr[@]', '', :style => 'width: 50px') %>:<%= escape_javascript(type_select 'type[@]', :class => 'type') %>'.replace(/@/g, i));
10
+ $('#attr_' + i).focus();
11
+ })
12
+ </script>
13
+ </head>
14
+ <body>
15
+ <h1>generate scaffold</h1>
16
+ <%= form_tag scaffold_generator_path, :remote => true, :id => HocusPocus::GENERATOR do %>
17
+ <% if @name.include? '_' %>
18
+ g scaffold
19
+ <%= select_tag :name, options_for_select([@name, @name.gsub('_', '/')]) %>
20
+ <% else %>
21
+ g scaffold <%= @name %>
22
+ <%= hidden_field_tag :name, @name %>
23
+ <% end %>
24
+ <%= hidden_field_tag :return_path, params[:return_path] %>
25
+ <span id="attr_type">
26
+ <%= text_field_tag 'attr[0]', '', :style => 'width: 50px', :autofocus => true %>:<%= type_select 'type[0]', :class => 'type' %>
27
+ </span>
28
+ <input type="button" value="add attribute" id="add_attr_type">
29
+ <br>
30
+ <%= submit_tag 'generate' %>
31
+ <% end %>
32
+ </body>
33
+ </html>
@@ -0,0 +1 @@
1
+ window.location.replace('<%= @return_path %>');
data/config/routes.rb ADDED
@@ -0,0 +1,7 @@
1
+ HocusPocus::Engine.routes.draw do
2
+ post '/generator/scaffold', :to => 'generator#scaffold', :as => 'scaffold_generator'
3
+ post '/generator/execute', :to => 'generator#execute', :as => 'execute_generator'
4
+ get '/editor', :to => 'editor#index'
5
+ post '/editor/save', :to => 'editor#save', :as => 'save_editor'
6
+ get '/:anything', :to => 'generator#index', :constraints => {:anything => /.*/}
7
+ end
@@ -0,0 +1,83 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{hocus_pocus}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Akira Matsuda"]
12
+ s.date = %q{2010-12-22}
13
+ s.description = %q{An Isolated Engine that casts a spell on your Rails 3.1 app}
14
+ s.email = %q{ronnie@dio.jp}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "app/controllers/hocus_pocus/application_controller.rb",
28
+ "app/controllers/hocus_pocus/editor_controller.rb",
29
+ "app/controllers/hocus_pocus/generator_controller.rb",
30
+ "app/helpers/hocus_pocus/application_helper.rb",
31
+ "app/helpers/hocus_pocus/generator_helper.rb",
32
+ "app/views/hocus_pocus/editor/_editor.html.erb",
33
+ "app/views/hocus_pocus/editor/index.js.erb",
34
+ "app/views/hocus_pocus/editor/save.js.erb",
35
+ "app/views/hocus_pocus/generator/execute.js.erb",
36
+ "app/views/hocus_pocus/generator/index.html.erb",
37
+ "app/views/hocus_pocus/generator/scaffold.js.erb",
38
+ "config/routes.rb",
39
+ "hocus_pocus.gemspec",
40
+ "lib/hocus_pocus.rb",
41
+ "lib/hocus_pocus/engine.rb",
42
+ "lib/hocus_pocus/filter.rb",
43
+ "lib/tasks/hocus_pocus_tasks.rake",
44
+ "public/javascripts/rails.js",
45
+ "test/helper.rb",
46
+ "test/test_hocus_pocus.rb"
47
+ ]
48
+ s.homepage = %q{http://github.com/amatsuda/hocus_pocus}
49
+ s.licenses = ["MIT"]
50
+ s.require_paths = ["lib"]
51
+ s.rubygems_version = %q{1.3.7}
52
+ s.summary = %q{An Isolated Engine that casts a spell on your Rails 3.1 app}
53
+ s.test_files = [
54
+ "test/helper.rb",
55
+ "test/test_hocus_pocus.rb"
56
+ ]
57
+
58
+ if s.respond_to? :specification_version then
59
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
60
+ s.specification_version = 3
61
+
62
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
63
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
64
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
65
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
66
+ s.add_development_dependency(%q<rcov>, [">= 0"])
67
+ s.add_runtime_dependency(%q<nested_scaffold>, [">= 0.1.0"])
68
+ else
69
+ s.add_dependency(%q<shoulda>, [">= 0"])
70
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
71
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
72
+ s.add_dependency(%q<rcov>, [">= 0"])
73
+ s.add_dependency(%q<nested_scaffold>, [">= 0.1.0"])
74
+ end
75
+ else
76
+ s.add_dependency(%q<shoulda>, [">= 0"])
77
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
78
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
79
+ s.add_dependency(%q<rcov>, [">= 0"])
80
+ s.add_dependency(%q<nested_scaffold>, [">= 0.1.0"])
81
+ end
82
+ end
83
+
@@ -0,0 +1,43 @@
1
+ require 'hocus_pocus/engine'
2
+ require 'hocus_pocus/filter'
3
+
4
+ module HocusPocus
5
+ VIEW_FILENAMES = :__hocus_pocus_view_filenames__
6
+ EDITOR = :__hocus_pocus_editor__
7
+ GENERATOR = :__hocus_pocus_generator__
8
+ SPEC = :__hocus_pocus_spec__
9
+ end
10
+
11
+ if Rails.env.development?
12
+ ActiveSupport.on_load(:after_initialize) do
13
+ Rails.application.routes.append do
14
+ mount HocusPocus::Engine, :at => '/'
15
+ end
16
+ end
17
+
18
+ class ::ActionController::Base
19
+ before_filter HocusPocus::Filter
20
+ after_filter HocusPocus::Filter
21
+ end
22
+
23
+ class ::ActionView::Base
24
+ def method_missing(method, args = {}, &blk)
25
+ if method.to_s =~ /(new_|edit_)?(.*)(_path|_url)\z/
26
+ # to avoid DoubleRenderError
27
+ controller.instance_variable_set :@_response_body, nil
28
+ #FIXME preserve args
29
+ controller.redirect_to "/#{$2.pluralize}?return_path=#{method}(#{args})"
30
+ else
31
+ super
32
+ end
33
+ end
34
+ end
35
+
36
+ class ::ActionView::PartialRenderer
37
+ def render_partial_with_filename_caching
38
+ (Thread.current[HocusPocus::VIEW_FILENAMES] ||= []) << @template unless @view.controller.is_a?(HocusPocus::EditorController)
39
+ render_partial_without_filename_caching
40
+ end
41
+ alias_method_chain :render_partial, :filename_caching
42
+ end
43
+ end
@@ -0,0 +1,9 @@
1
+ module HocusPocus
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace HocusPocus
4
+ # initializer 'add middleware' do |app|
5
+ # app.middleware.use HocusPocus::RouteMissing
6
+ # app.middleware.use HocusPocus::ContentAppender
7
+ # end
8
+ end
9
+ end
@@ -0,0 +1,91 @@
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
+ unless controller.request.format.js?
12
+ filter = self.new(controller)
13
+ #FIXME avoid loading jquery twice
14
+ # filter.add_jquery
15
+ filter.add_steak_recorder
16
+ # filter.add_js
17
+ unless controller.is_a?(HocusPocus::EditorController) || controller.is_a?(HocusPocus::GeneratorController)
18
+ filter.add_buttons
19
+ end
20
+ # filter.add_command_line
21
+ controller.response.body = filter.body
22
+ end
23
+ end
24
+
25
+ def initialize(controller)
26
+ @controller = controller
27
+ @template = controller.instance_variable_get(:@template)
28
+ @body = controller.response.body
29
+ end
30
+
31
+ def add_jquery
32
+ # insert_text :before, /<\/head>/i, '<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script><script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script><link rel="stylesheet" href="/hocus_pocus/stylesheets/jquery.ui.dialog.css" type="text/css">'
33
+ insert_text :before, /<\/head>/i, '<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>'
34
+ end
35
+
36
+ def add_steak_recorder
37
+ #TODO exclude EDITOR and GENERATOR
38
+ insert_text :before, /<\/body>/i, <<-TEPPAN
39
+ <script type="text/javascript">
40
+ $('form').live('submit', function() {
41
+ var scenario = [" scenario '#{@controller.action_name.humanize} #{@controller.controller_name.humanize}' do"];
42
+ scenario.push(" visit '" + window.location.href + "'");
43
+ $(this).find('input[type=text]').each(function() {
44
+ scenario.push(" fill_in '" + $(this).attr('id') + "', :with => '" + $(this).val() + "'");
45
+ });
46
+ scenario.push(" click_button '" + $(this).find('input[type=submit]').val() + "'");
47
+ $(this).append('<textarea id="#{HocusPocus::SPEC}" name="#{HocusPocus::SPEC}" style="height: 0px; width: 0px;" />');
48
+ $('##{HocusPocus::SPEC}').val(scenario.join('\\n'));
49
+ })
50
+ </script>
51
+ TEPPAN
52
+ end
53
+
54
+
55
+ def add_js
56
+ # insert_text :before, /<\/body>/i, %Q[<script type="text/javascript">alert('hoge!');</script>]
57
+ end
58
+
59
+ def add_buttons
60
+ #FIXME path
61
+ #FIXME use @template somehow?
62
+ edit_link = %Q[<a href="/editor?template=#{@controller.controller_name}/#{@controller.action_name}" data-remote="true" onclick="$(this).closest('div').find('div.partials').toggle()">edit</a>]
63
+ spec_link = %Q[<a href="#" onclick="$(this).closest('div').find('div.spec').toggle();">spec</a>]
64
+ partials = %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>]
65
+ #FIXME more assertions
66
+ spec = %Q[<div class="spec" style="display:none"><pre>#{CGI.unescape(@controller.flash[HocusPocus::SPEC]) + "\n end" if @controller.flash[HocusPocus::SPEC]}</pre></div>]
67
+
68
+ insert_text :before, /<\/body>/i, %Q[<div style="position:absolute; top:0; right: 0;">#{edit_link} | #{spec_link}<br>#{partials}<br>#{spec}</div>]
69
+ Thread.current[HocusPocus::VIEW_FILENAMES] = nil
70
+ end
71
+
72
+ def add_command_line
73
+ 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="どうする?コマンド" style="width: 512px;" /><input type="submit" name="run" /></form></div>]
74
+ end
75
+
76
+ private
77
+ def insert_text(position, pattern, new_text)
78
+ index = case pattern
79
+ when Regexp
80
+ if match = @body.match(pattern)
81
+ match.offset(0)[position == :before ? 0 : 1]
82
+ else
83
+ @body.size
84
+ end
85
+ else
86
+ pattern
87
+ end
88
+ @body.insert index, new_text
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :hocus_pocus do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,160 @@
1
+ /*
2
+ * jquery-ujs
3
+ *
4
+ * http://github.com/rails/jquery-ujs/blob/master/src/rails.js
5
+ *
6
+ * This rails.js file supports jQuery 1.4.3 and 1.4.4 .
7
+ *
8
+ */
9
+
10
+ jQuery(function ($) {
11
+ var csrf_token = $('meta[name=csrf-token]').attr('content'),
12
+ csrf_param = $('meta[name=csrf-param]').attr('content');
13
+
14
+ $.fn.extend({
15
+ /**
16
+ * Triggers a custom event on an element and returns the event result
17
+ * this is used to get around not being able to ensure callbacks are placed
18
+ * at the end of the chain.
19
+ *
20
+ * TODO: deprecate with jQuery 1.4.2 release, in favor of subscribing to our
21
+ * own events and placing ourselves at the end of the chain.
22
+ */
23
+ triggerAndReturn: function (name, data) {
24
+ var event = new $.Event(name);
25
+ this.trigger(event, data);
26
+
27
+ return event.result !== false;
28
+ },
29
+
30
+ /**
31
+ * Handles execution of remote calls. Provides following callbacks:
32
+ *
33
+ * - ajax:beforeSend - is executed before firing ajax call
34
+ * - ajax:success - is executed when status is success
35
+ * - ajax:complete - is executed when the request finishes, whether in failure or success.
36
+ * - ajax:error - is execute in case of error
37
+ */
38
+ callRemote: function () {
39
+ var el = this,
40
+ method = el.attr('method') || el.attr('data-method') || 'GET',
41
+ url = el.attr('action') || el.attr('href'),
42
+ dataType = el.attr('data-type') || ($.ajaxSettings && $.ajaxSettings.dataType);
43
+
44
+ if (url === undefined) {
45
+ throw "No URL specified for remote call (action or href must be present).";
46
+ } else {
47
+ var $this = $(this), data = el.is('form') ? el.serializeArray() : [];
48
+
49
+ $.ajax({
50
+ url: url,
51
+ data: data,
52
+ dataType: dataType,
53
+ type: method.toUpperCase(),
54
+ beforeSend: function (xhr) {
55
+ xhr.setRequestHeader("Accept", "text/javascript");
56
+ if ($this.triggerHandler('ajax:beforeSend') === false) {
57
+ return false;
58
+ }
59
+ },
60
+ success: function (data, status, xhr) {
61
+ el.trigger('ajax:success', [data, status, xhr]);
62
+ },
63
+ complete: function (xhr) {
64
+ el.trigger('ajax:complete', xhr);
65
+ },
66
+ error: function (xhr, status, error) {
67
+ el.trigger('ajax:error', [xhr, status, error]);
68
+ }
69
+ });
70
+ }
71
+ }
72
+ });
73
+
74
+ /**
75
+ * confirmation handler
76
+ */
77
+
78
+ $('body').delegate('a[data-confirm], button[data-confirm], input[data-confirm]', 'click.rails', function () {
79
+ var el = $(this);
80
+ if (el.triggerAndReturn('confirm')) {
81
+ if (!confirm(el.attr('data-confirm'))) {
82
+ return false;
83
+ }
84
+ }
85
+ });
86
+
87
+
88
+
89
+ /**
90
+ * remote handlers
91
+ */
92
+ $('form[data-remote]').live('submit.rails', function (e) {
93
+ $(this).callRemote();
94
+ e.preventDefault();
95
+ });
96
+
97
+ $('a[data-remote],input[data-remote]').live('click.rails', function (e) {
98
+ $(this).callRemote();
99
+ e.preventDefault();
100
+ });
101
+
102
+ /**
103
+ * <%= link_to "Delete", user_path(@user), :method => :delete, :confirm => "Are you sure?" %>
104
+ *
105
+ * <a href="/users/5" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Delete</a>
106
+ */
107
+ $('a[data-method]:not([data-remote])').live('click.rails', function (e){
108
+ var link = $(this),
109
+ href = link.attr('href'),
110
+ method = link.attr('data-method'),
111
+ form = $('<form method="post" action="'+href+'"></form>'),
112
+ metadata_input = '<input name="_method" value="'+method+'" type="hidden" />';
113
+
114
+ if (csrf_param !== undefined && csrf_token !== undefined) {
115
+ metadata_input += '<input name="'+csrf_param+'" value="'+csrf_token+'" type="hidden" />';
116
+ }
117
+
118
+ form.hide()
119
+ .append(metadata_input)
120
+ .appendTo('body');
121
+
122
+ e.preventDefault();
123
+ form.submit();
124
+ });
125
+
126
+ /**
127
+ * disable-with handlers
128
+ */
129
+ var disable_with_input_selector = 'input[data-disable-with]',
130
+ disable_with_form_remote_selector = 'form[data-remote]:has(' + disable_with_input_selector + ')',
131
+ disable_with_form_not_remote_selector = 'form:not([data-remote]):has(' + disable_with_input_selector + ')';
132
+
133
+ var disable_with_input_function = function () {
134
+ $(this).find(disable_with_input_selector).each(function () {
135
+ var input = $(this);
136
+ input.data('enable-with', input.val())
137
+ .attr('value', input.attr('data-disable-with'))
138
+ .attr('disabled', 'disabled');
139
+ });
140
+ };
141
+
142
+ $(disable_with_form_remote_selector).live('ajax:before.rails', disable_with_input_function);
143
+ $(disable_with_form_not_remote_selector).live('submit.rails', disable_with_input_function);
144
+
145
+ $(disable_with_form_remote_selector).live('ajax:complete.rails', function () {
146
+ $(this).find(disable_with_input_selector).each(function () {
147
+ var input = $(this);
148
+ input.removeAttr('disabled')
149
+ .val(input.data('enable-with'));
150
+ });
151
+ });
152
+
153
+ var jqueryVersion = $().jquery;
154
+
155
+ if (!( (jqueryVersion === '1.4.3') || (jqueryVersion === '1.4.4'))){
156
+ alert('This rails.js does not support the jQuery version you are using. Please read documentation.');
157
+ }
158
+
159
+
160
+ });
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'hocus_pocus'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestHocusPocus < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hocus_pocus
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Akira Matsuda
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-22 00:00:00 +09:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ name: shoulda
33
+ requirement: *id001
34
+ type: :development
35
+ - !ruby/object:Gem::Dependency
36
+ prerelease: false
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ hash: 23
43
+ segments:
44
+ - 1
45
+ - 0
46
+ - 0
47
+ version: 1.0.0
48
+ name: bundler
49
+ requirement: *id002
50
+ type: :development
51
+ - !ruby/object:Gem::Dependency
52
+ prerelease: false
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 7
59
+ segments:
60
+ - 1
61
+ - 5
62
+ - 2
63
+ version: 1.5.2
64
+ name: jeweler
65
+ requirement: *id003
66
+ type: :development
67
+ - !ruby/object:Gem::Dependency
68
+ prerelease: false
69
+ version_requirements: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ name: rcov
79
+ requirement: *id004
80
+ type: :development
81
+ - !ruby/object:Gem::Dependency
82
+ prerelease: false
83
+ version_requirements: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 27
89
+ segments:
90
+ - 0
91
+ - 1
92
+ - 0
93
+ version: 0.1.0
94
+ name: nested_scaffold
95
+ requirement: *id005
96
+ type: :runtime
97
+ description: An Isolated Engine that casts a spell on your Rails 3.1 app
98
+ email: ronnie@dio.jp
99
+ executables: []
100
+
101
+ extensions: []
102
+
103
+ extra_rdoc_files:
104
+ - LICENSE.txt
105
+ - README.rdoc
106
+ files:
107
+ - .document
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.rdoc
112
+ - Rakefile
113
+ - VERSION
114
+ - app/controllers/hocus_pocus/application_controller.rb
115
+ - app/controllers/hocus_pocus/editor_controller.rb
116
+ - app/controllers/hocus_pocus/generator_controller.rb
117
+ - app/helpers/hocus_pocus/application_helper.rb
118
+ - app/helpers/hocus_pocus/generator_helper.rb
119
+ - app/views/hocus_pocus/editor/_editor.html.erb
120
+ - app/views/hocus_pocus/editor/index.js.erb
121
+ - app/views/hocus_pocus/editor/save.js.erb
122
+ - app/views/hocus_pocus/generator/execute.js.erb
123
+ - app/views/hocus_pocus/generator/index.html.erb
124
+ - app/views/hocus_pocus/generator/scaffold.js.erb
125
+ - config/routes.rb
126
+ - hocus_pocus.gemspec
127
+ - lib/hocus_pocus.rb
128
+ - lib/hocus_pocus/engine.rb
129
+ - lib/hocus_pocus/filter.rb
130
+ - lib/tasks/hocus_pocus_tasks.rake
131
+ - public/javascripts/rails.js
132
+ - test/helper.rb
133
+ - test/test_hocus_pocus.rb
134
+ has_rdoc: true
135
+ homepage: http://github.com/amatsuda/hocus_pocus
136
+ licenses:
137
+ - MIT
138
+ post_install_message:
139
+ rdoc_options: []
140
+
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ hash: 3
149
+ segments:
150
+ - 0
151
+ version: "0"
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ hash: 3
158
+ segments:
159
+ - 0
160
+ version: "0"
161
+ requirements: []
162
+
163
+ rubyforge_project:
164
+ rubygems_version: 1.3.7
165
+ signing_key:
166
+ specification_version: 3
167
+ summary: An Isolated Engine that casts a spell on your Rails 3.1 app
168
+ test_files:
169
+ - test/helper.rb
170
+ - test/test_hocus_pocus.rb