hocus_pocus 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/CHANGELOG ADDED
@@ -0,0 +1,7 @@
1
+ == 0.1.0
2
+
3
+ * Works with Rails 3.1.0.beta1
4
+
5
+ == 0.0.1
6
+
7
+ * First release
data/Gemfile CHANGED
@@ -1,13 +1,4 @@
1
1
  source "http://rubygems.org"
2
- # Add dependencies required to use your gem here.
3
- # Example:
4
- # gem "activesupport", ">= 2.3.5"
5
2
 
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
3
+ # Specify your gem's dependencies in hocus_pocus.gemspec
4
+ gemspec
data/Rakefile CHANGED
@@ -1,53 +1,2 @@
1
- require 'rubygems'
2
1
  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
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,8 @@
1
+ module HocusPocus
2
+ class SpecController < ActionController::Base
3
+ # XHR
4
+ def destroy
5
+ flash.discard HocusPocus::SPEC
6
+ end
7
+ end
8
+ end
@@ -4,7 +4,10 @@
4
4
  <%= form_tag save_editor_path, :remote => true, :id => HocusPocus::EDITOR do %>
5
5
  <%= hidden_field_tag :path, @path %>
6
6
  <%= text_area_tag :file, @file, :size => '100x30' %>
7
- <%= submit_tag 'save' %>
7
+ <div align="right">
8
+ <%= button_to_function 'Hide', "$('#__editor__').hide()" %>&nbsp;
9
+ <%= submit_tag 'Save!' %>
10
+ </div>
8
11
  <% end %>
9
12
 
10
13
  <hr>
@@ -1,8 +1,8 @@
1
1
  <html>
2
2
  <head>
3
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>
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>
6
6
  <script type="text/javascript">
7
7
  $('#add_attr_type').live('click', function() {
8
8
  var i = $('#attr_type input').length;
@@ -0,0 +1 @@
1
+ $('#<%= HocusPocus::CONTAINER %> div.spec pre').html('Cleared.');
data/config/routes.rb CHANGED
@@ -1,7 +1,16 @@
1
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 => /.*/}
2
+ if HocusPocus.config.enable_generator
3
+ post '/generator/scaffold', :to => 'generator#scaffold', :as => 'scaffold_generator'
4
+ post '/generator/execute', :to => 'generator#execute', :as => 'execute_generator'
5
+ end
6
+ if HocusPocus.config.enable_editor
7
+ get '/editor', :to => 'editor#index'
8
+ post '/editor/save', :to => 'editor#save', :as => 'save_editor'
9
+ end
10
+ if HocusPocus.config.enable_scenario_recorder
11
+ delete '/spec', :to => 'spec#destroy'
12
+ end
13
+ if HocusPocus.config.enable_generator
14
+ get '/:anything', :to => 'generator#index', :constraints => {:anything => /.*/}
15
+ end
7
16
  end
data/hocus_pocus.gemspec CHANGED
@@ -1,83 +1,26 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'hocus_pocus/version'
5
4
 
6
5
  Gem::Specification.new do |s|
7
- s.name = %q{hocus_pocus}
8
- s.version = "0.0.1"
6
+ s.name = 'hocus_pocus'
7
+ s.version = HocusPocus::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Akira Matsuda']
10
+ s.email = ['ronnie@dio.jp']
11
+ s.homepage = 'http://github.com/amatsuda/hocus_pocus'
12
+ s.summary = 'An Isolated Engine that casts a spell on your Rails 3.1 app'
13
+ s.description = 'An Isolated Engine that casts a spell on your Rails 3.1 app'
9
14
 
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-23}
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
- ]
15
+ s.rubyforge_project = 'hocus_pocus'
57
16
 
58
- if s.respond_to? :specification_version then
59
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
60
- s.specification_version = 3
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ['lib']
61
21
 
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
22
+ s.extra_rdoc_files = ['README.rdoc']
23
+ s.licenses = ['MIT']
83
24
 
25
+ s.add_runtime_dependency 'nested_scaffold', ['>= 0.1.0']
26
+ end
@@ -0,0 +1,16 @@
1
+ module HocusPocus
2
+ module Generators
3
+ class ConfigGenerator < Rails::Generators::Base
4
+ source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
5
+
6
+ desc <<DESC
7
+ Description:
8
+ Copies hobus_pocus configuration file to your application's initializer directory.
9
+ DESC
10
+
11
+ def copy_config_file
12
+ template 'hocus_pocus_config.rb', 'config/initializers/hocus_pocus_config.rb'
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,6 @@
1
+ HocusPocus.configure do |config|
2
+ # config.enable_generator = true
3
+ # config.enable_editor = true
4
+ # config.enable_scenario_recorder = true
5
+ # config.enable_command_line = false
6
+ end if defined? HocusPocus
data/lib/hocus_pocus.rb CHANGED
@@ -1,43 +1 @@
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
1
+ require File.join(File.dirname(__FILE__), 'hocus_pocus/railtie')
@@ -0,0 +1,30 @@
1
+ require 'active_support/configurable'
2
+
3
+ module HocusPocus
4
+ # Configures global settings for HocusPocus
5
+ # HocusPocus.configure do |config|
6
+ # config.enable_generator = false
7
+ # end
8
+ def self.configure(&block)
9
+ yield @config ||= HocusPocus::Configuration.new
10
+ end
11
+
12
+ # Global settings for HocusPocus
13
+ def self.config
14
+ @config
15
+ end
16
+
17
+ # need a Class for 3.0
18
+ class Configuration #:nodoc:
19
+ include ActiveSupport::Configurable
20
+ config_accessor :enable_generator, :enable_editor, :enable_scenario_recorder, :enable_command_line
21
+ end
22
+
23
+ # this is ugly. why can't we pass the default value to config_accessor...?
24
+ configure do |config|
25
+ config.enable_generator = true
26
+ config.enable_editor = true
27
+ config.enable_scenario_recorder = true
28
+ config.enable_command_line = false
29
+ end
30
+ end
@@ -0,0 +1,76 @@
1
+ module HocusPocus
2
+ class ContentAppender
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ # controller.flash[HocusPocus::SPEC] = controller.params[HocusPocus::SPEC] if controller.params[HocusPocus::SPEC]
9
+ # controller.flash.keep(HocusPocus::SPEC)
10
+
11
+ @app.call(env).tap do |status, headers, body|
12
+ if body.respond_to? :body
13
+ @body = body.body
14
+ @controller_name = env['action_dispatch.request.path_parameters'][:controller]
15
+ @action_name = env['action_dispatch.request.path_parameters'][:action]
16
+ unless headers['Content-Type'].starts_with?('text/html')
17
+ #FIXME avoid loading jquery twice
18
+ # add_jquery
19
+ add_steak_recorder
20
+ # add_js
21
+ unless controller.start_with? 'hocus_pocus/'
22
+ add_buttons
23
+ end
24
+ # add_command_line
25
+ body.body = @body
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+ def add_steak_recorder
32
+ #TODO exclude EDITOR and GENERATOR
33
+ insert_text :before, /<\/body>/i, <<-TEPPAN
34
+ <script type="text/javascript">
35
+ $('form').live('submit', function() {
36
+ var scenario = [" scenario '#{@action_name.humanize} #{@controller_name.humanize}' do"];
37
+ scenario.push(" visit '" + window.location.href + "'");
38
+ $(this).find('input[type=text]').each(function() {
39
+ scenario.push(" fill_in '" + $(this).attr('id') + "', :with => '" + $(this).val() + "'");
40
+ });
41
+ scenario.push(" click_button '" + $(this).find('input[type=submit]').val() + "'");
42
+ $(this).append('<textarea id="#{HocusPocus::SPEC}" name="#{HocusPocus::SPEC}" style="height: 0px; width: 0px;" />');
43
+ $('##{HocusPocus::SPEC}').val(scenario.join('\\n'));
44
+ })
45
+ </script>
46
+ TEPPAN
47
+ end
48
+
49
+ def add_buttons
50
+ #FIXME path
51
+ #FIXME use @template somehow?
52
+ 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>]
53
+ spec_link = %Q[<a href="#" onclick="$(this).closest('div').find('div.spec').toggle();">spec</a>]
54
+ 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>]
55
+ #FIXME more assertions
56
+ spec = %Q[<div class="spec" style="display:none"><pre>#{CGI.unescape(@controller.flash[HocusPocus::SPEC]) + "\n end" if @controller.flash[HocusPocus::SPEC]}</pre></div>]
57
+
58
+ insert_text :before, /<\/body>/i, %Q[<div style="position:absolute; top:0; right: 0;">#{edit_link} | #{spec_link}<br>#{partials}<br>#{spec}</div>]
59
+ Thread.current[HocusPocus::VIEW_FILENAMES] = nil
60
+ end
61
+
62
+ def insert_text(position, pattern, new_text)
63
+ index = case pattern
64
+ when Regexp
65
+ if match = @body.match(pattern)
66
+ match.offset(0)[position == :before ? 0 : 1]
67
+ else
68
+ @body.size
69
+ end
70
+ else
71
+ pattern
72
+ end
73
+ @body.insert index, new_text
74
+ end
75
+ end
76
+ end
@@ -12,12 +12,12 @@ module HocusPocus
12
12
  filter = self.new(controller)
13
13
  #FIXME avoid loading jquery twice
14
14
  # filter.add_jquery
15
- filter.add_steak_recorder
15
+ filter.add_steak_recorder if HocusPocus.config.enable_scenario_recorder
16
16
  # filter.add_js
17
17
  unless controller.is_a?(HocusPocus::EditorController) || controller.is_a?(HocusPocus::GeneratorController)
18
18
  filter.add_buttons
19
19
  end
20
- # filter.add_command_line
20
+ filter.add_command_line if HocusPocus.config.enable_command_line
21
21
  controller.response.body = filter.body
22
22
  end
23
23
  end
@@ -30,7 +30,7 @@ module HocusPocus
30
30
 
31
31
  def add_jquery
32
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>'
33
+ insert_text :before, /<\/head>/i, '<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>'
34
34
  end
35
35
 
36
36
  def add_steak_recorder
@@ -40,9 +40,12 @@ module HocusPocus
40
40
  $('form').live('submit', function() {
41
41
  var scenario = [" scenario '#{@controller.action_name.humanize} #{@controller.controller_name.humanize}' do"];
42
42
  scenario.push(" visit '" + window.location.href + "'");
43
- $(this).find('input[type=text]').each(function() {
43
+ $(this).find('input[type=text],textarea').each(function() {
44
44
  scenario.push(" fill_in '" + $(this).attr('id') + "', :with => '" + $(this).val() + "'");
45
45
  });
46
+ $(this).find('select').each(function() {
47
+ scenario.push(" select '" + $(this).val() + "', :from => '" + $(this).attr('id') + "'");
48
+ });
46
49
  scenario.push(" click_button '" + $(this).find('input[type=submit]').val() + "'");
47
50
  $(this).append('<textarea id="#{HocusPocus::SPEC}" name="#{HocusPocus::SPEC}" style="height: 0px; width: 0px;" />');
48
51
  $('##{HocusPocus::SPEC}').val(scenario.join('\\n'));
@@ -57,20 +60,12 @@ TEPPAN
57
60
  end
58
61
 
59
62
  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>]
63
+ 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>]
69
64
  Thread.current[HocusPocus::VIEW_FILENAMES] = nil
70
65
  end
71
66
 
72
67
  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>]
68
+ 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>]
74
69
  end
75
70
 
76
71
  private
@@ -87,5 +82,28 @@ TEPPAN
87
82
  end
88
83
  @body.insert index, new_text
89
84
  end
85
+
86
+ def edit_link
87
+ return nil unless HocusPocus.config.enable_editor
88
+ #FIXME path
89
+ #FIXME use @template somehow?
90
+ %Q[<a href="/editor?template=#{@controller.controller_name}/#{@controller.action_name}" data-remote="true" onclick="$(this).closest('div').find('div.partials').toggle()">edit</a>]
91
+ end
92
+
93
+ def spec_link
94
+ return nil unless HocusPocus.config.enable_scenario_recorder
95
+ %Q[<a href="#" onclick="$(this).closest('div').find('div.spec').toggle();">spec</a>]
96
+ end
97
+
98
+ def partials
99
+ return nil unless HocusPocus.config.enable_editor
100
+ %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>]
101
+ end
102
+
103
+ def spec
104
+ return nil unless HocusPocus.config.enable_scenario_recorder
105
+ #FIXME more assertions
106
+ %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>]
107
+ end
90
108
  end
91
109
  end
@@ -0,0 +1,53 @@
1
+ require 'rails'
2
+ require File.join(File.dirname(__FILE__), 'config')
3
+
4
+ if Rails.env.development?
5
+ require 'hocus_pocus/engine'
6
+ require 'hocus_pocus/filter'
7
+
8
+ module HocusPocus
9
+ VIEW_FILENAMES = :__hocus_pocus_view_filenames__
10
+ CONTAINER = :__hocus_pocus_container__
11
+ EDITOR = :__hocus_pocus_editor__
12
+ GENERATOR = :__hocus_pocus_generator__
13
+ SPEC = :__hocus_pocus_spec__
14
+
15
+ class Railtie < ::Rails::Railtie #:nodoc:
16
+ initializer 'hocus_pocus' do |app|
17
+ ActiveSupport.on_load(:after_initialize) do
18
+ Rails.application.routes.append do
19
+ mount HocusPocus::Engine, :at => '/'
20
+ end
21
+ end
22
+ ActiveSupport.on_load(:action_controller) do
23
+ class ::ActionController::Base
24
+ before_filter HocusPocus::Filter
25
+ after_filter HocusPocus::Filter
26
+ end
27
+ end
28
+ ActiveSupport.on_load(:action_view) do
29
+ class ::ActionView::Base
30
+ def method_missing(method, args = {}, &blk)
31
+ if method.to_s =~ /(new_|edit_)?(.*)(_path|_url)\z/
32
+ # to avoid DoubleRenderError
33
+ controller.instance_variable_set :@_response_body, nil
34
+ #FIXME preserve args
35
+ controller.redirect_to "/#{$2.pluralize}?return_path=#{method}(#{args})"
36
+ else
37
+ super
38
+ end
39
+ end
40
+ end
41
+
42
+ class ::ActionView::PartialRenderer
43
+ def render_partial_with_filename_caching
44
+ (Thread.current[HocusPocus::VIEW_FILENAMES] ||= []) << @template unless @view.controller.is_a?(HocusPocus::EditorController)
45
+ render_partial_without_filename_caching
46
+ end
47
+ alias_method_chain :render_partial, :filename_caching
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module HocusPocus
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,289 @@
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 );
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hocus_pocus
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease: false
4
+ hash: 27
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Akira Matsuda
@@ -15,72 +15,12 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-23 00:00:00 +09:00
19
- default_executable:
18
+ date: 2011-05-06 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
21
+ name: nested_scaffold
22
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
23
+ requirement: &id001 !ruby/object:Gem::Requirement
84
24
  none: false
85
25
  requirements:
86
26
  - - ">="
@@ -91,29 +31,29 @@ dependencies:
91
31
  - 1
92
32
  - 0
93
33
  version: 0.1.0
94
- name: nested_scaffold
95
- requirement: *id005
96
34
  type: :runtime
35
+ version_requirements: *id001
97
36
  description: An Isolated Engine that casts a spell on your Rails 3.1 app
98
- email: ronnie@dio.jp
37
+ email:
38
+ - ronnie@dio.jp
99
39
  executables: []
100
40
 
101
41
  extensions: []
102
42
 
103
43
  extra_rdoc_files:
104
- - LICENSE.txt
105
44
  - README.rdoc
106
45
  files:
107
46
  - .document
47
+ - .gitignore
48
+ - CHANGELOG
108
49
  - Gemfile
109
- - Gemfile.lock
110
50
  - LICENSE.txt
111
51
  - README.rdoc
112
52
  - Rakefile
113
- - VERSION
114
53
  - app/controllers/hocus_pocus/application_controller.rb
115
54
  - app/controllers/hocus_pocus/editor_controller.rb
116
55
  - app/controllers/hocus_pocus/generator_controller.rb
56
+ - app/controllers/hocus_pocus/spec_controller.rb
117
57
  - app/helpers/hocus_pocus/application_helper.rb
118
58
  - app/helpers/hocus_pocus/generator_helper.rb
119
59
  - app/views/hocus_pocus/editor/_editor.html.erb
@@ -122,16 +62,22 @@ files:
122
62
  - app/views/hocus_pocus/generator/execute.js.erb
123
63
  - app/views/hocus_pocus/generator/index.html.erb
124
64
  - app/views/hocus_pocus/generator/scaffold.js.erb
65
+ - app/views/hocus_pocus/spec/destroy.js.erb
125
66
  - config/routes.rb
126
67
  - hocus_pocus.gemspec
68
+ - lib/generators/hocus_pocus/config_generator.rb
69
+ - lib/generators/hocus_pocus/templates/hocus_pocus_config.rb
127
70
  - lib/hocus_pocus.rb
71
+ - lib/hocus_pocus/config.rb
72
+ - lib/hocus_pocus/content_appender.rb
128
73
  - lib/hocus_pocus/engine.rb
129
74
  - lib/hocus_pocus/filter.rb
75
+ - lib/hocus_pocus/railtie.rb
76
+ - lib/hocus_pocus/version.rb
130
77
  - lib/tasks/hocus_pocus_tasks.rake
131
- - public/javascripts/rails.js
132
78
  - test/helper.rb
133
79
  - test/test_hocus_pocus.rb
134
- has_rdoc: true
80
+ - vendor/assets/javascripts/jquery_ujs.js
135
81
  homepage: http://github.com/amatsuda/hocus_pocus
136
82
  licenses:
137
83
  - MIT
@@ -160,8 +106,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
106
  version: "0"
161
107
  requirements: []
162
108
 
163
- rubyforge_project:
164
- rubygems_version: 1.3.7
109
+ rubyforge_project: hocus_pocus
110
+ rubygems_version: 1.8.0
165
111
  signing_key:
166
112
  specification_version: 3
167
113
  summary: An Isolated Engine that casts a spell on your Rails 3.1 app
data/Gemfile.lock DELETED
@@ -1,20 +0,0 @@
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/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.0.1
@@ -1,160 +0,0 @@
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
- });