jelly 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/.gitignore +3 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README.markdown +112 -0
  4. data/Rakefile +39 -0
  5. data/VERSION +1 -0
  6. data/VERSION.yml +4 -0
  7. data/generators/jelly/USAGE +11 -0
  8. data/generators/jelly/jelly_generator.rb +12 -0
  9. data/generators/jelly/templates/javascripts/ajax_with_jelly.js +32 -0
  10. data/generators/jelly/templates/javascripts/jelly.js +81 -0
  11. data/generators/jelly/templates/javascripts/jquery/jquery-1.3.2.js +4376 -0
  12. data/generators/jelly/templates/javascripts/jquery/jquery.protify-0.3.js +345 -0
  13. data/install.rb +1 -0
  14. data/jelly.gemspec +77 -0
  15. data/lib/jelly.rb +16 -0
  16. data/lib/jelly/jelly_controller.rb +23 -0
  17. data/lib/jelly/jelly_helper.rb +42 -0
  18. data/spec/controllers/jelly_controller_spec.rb +118 -0
  19. data/spec/helpers/jelly_helper_spec.rb +54 -0
  20. data/spec/rails_root/app/controllers/application_controller.rb +10 -0
  21. data/spec/rails_root/app/helpers/application_helper.rb +3 -0
  22. data/spec/rails_root/config/boot.rb +110 -0
  23. data/spec/rails_root/config/environment.rb +41 -0
  24. data/spec/rails_root/config/environments/development.rb +17 -0
  25. data/spec/rails_root/config/environments/production.rb +28 -0
  26. data/spec/rails_root/config/environments/test.rb +28 -0
  27. data/spec/rails_root/config/initializers/backtrace_silencers.rb +7 -0
  28. data/spec/rails_root/config/initializers/inflections.rb +10 -0
  29. data/spec/rails_root/config/initializers/mime_types.rb +5 -0
  30. data/spec/rails_root/config/initializers/new_rails_defaults.rb +19 -0
  31. data/spec/rails_root/config/initializers/session_store.rb +15 -0
  32. data/spec/rails_root/config/routes.rb +43 -0
  33. data/spec/rails_root/test/performance/browsing_test.rb +9 -0
  34. data/spec/rails_root/test/test_helper.rb +38 -0
  35. data/spec/spec_helper.rb +22 -0
  36. data/tasks/jelly_tasks.rake +4 -0
  37. data/uninstall.rb +1 -0
  38. metadata +99 -0
@@ -0,0 +1,42 @@
1
+ module JellyHelper
2
+
3
+ def application_jelly_files(jelly_files_path_from_javascripts = '', rails_root = RAILS_ROOT)
4
+ Dir["#{rails_root}/public/javascripts/#{jelly_files_path_from_javascripts}/pages/**/*.js"].map do |path|
5
+ path.gsub("#{rails_root}/public/javascripts/", "").gsub(/\.js$/, "")
6
+ end
7
+ end
8
+
9
+ def spread_jelly
10
+ javascript_tag <<-JS
11
+ window._token = '#{form_authenticity_token}'
12
+ Jelly.activatePage('#{controller.controller_path.camelcase}', '#{controller.action_name}');
13
+ #{@content_for_javascript}
14
+ $(document).ready(function() {
15
+ #{@content_for_javascript_on_ready}
16
+ });
17
+ JS
18
+ end
19
+
20
+ def clear_jelly_attached()
21
+ @jelly_attached_components = []
22
+ end
23
+
24
+ def attach_javascript_component(component_name, *args)
25
+ @jelly_attached_components ||= []
26
+ key = "page.attach(#{component_name}, #{args.to_json});"
27
+ unless @jelly_attached_components.include? key
28
+ @jelly_attached_components << key
29
+ content_for(:javascript, key)
30
+ end
31
+ end
32
+
33
+ def attach_javascript_component_on_ready(component_name, *args)
34
+ @jelly_attached_components_on_ready ||= []
35
+ key = "page.attach(#{component_name}, #{args.to_json});"
36
+ unless @jelly_attached_components_on_ready.include? key
37
+ @jelly_attached_components_on_ready << key
38
+ content_for(:javascript_on_ready, key)
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,118 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe ApplicationController do
4
+
5
+ describe "#jelly_callback" do
6
+ before do
7
+ @controller.stub!(:render)
8
+ end
9
+
10
+ it "have the method included" do
11
+ @controller.respond_to?(:jelly_callback).should be_true
12
+ end
13
+
14
+ it "should render inline the return of jelly_callback_erb" do
15
+ block = lambda{'foo yo'}
16
+ mock_erb = "whatever"
17
+ @controller.should_receive(:jelly_callback_erb).with("on_foo", {}, block).and_return(mock_erb)
18
+ @controller.should_receive(:render).with(:inline => mock_erb)
19
+ @controller.send(:jelly_callback, "foo", &block)
20
+ end
21
+
22
+ describe "#jelly_callback_erb" do
23
+ before do
24
+ request.stub!(:xhr?).and_return(true)
25
+ end
26
+
27
+ context "with options" do
28
+ it "should work with a block" do
29
+ erb = @controller.send(:jelly_callback_erb, 'foo', {'bar' => 'baz'}, lambda{'grape'})
30
+ JSON.parse(ERB.new(erb).result(@controller.send(:binding))).should == {
31
+ 'method' => 'foo',
32
+ 'arguments' => ['grape'],
33
+ 'bar' => 'baz'
34
+ }
35
+ end
36
+
37
+ it "should work without a block" do
38
+ erb = @controller.send(:jelly_callback_erb, 'foo', {'bar' => 'baz'}, nil)
39
+ JSON.parse(ERB.new(erb).result(@controller.send(:binding))).should == {
40
+ 'method' => 'foo',
41
+ 'arguments' => [],
42
+ 'bar' => 'baz'
43
+ }
44
+ end
45
+
46
+ it "should work if options are passed with symbol keys" do
47
+ erb = @controller.send(:jelly_callback_erb, 'foo', {:bar => 'baz'}, nil)
48
+ JSON.parse(ERB.new(erb).result(@controller.send(:binding))).should == {
49
+ 'method' => 'foo',
50
+ 'arguments' => [],
51
+ 'bar' => 'baz'
52
+ }
53
+ end
54
+ end
55
+
56
+ context "without options" do
57
+ it "should work with a block" do
58
+ erb = @controller.send(:jelly_callback_erb, 'foo', {}, lambda{'grape'})
59
+ JSON.parse(ERB.new(erb).result(@controller.send(:binding))).should == {
60
+ 'method' => 'foo',
61
+ 'arguments' => ['grape']
62
+ }
63
+ end
64
+
65
+ it "should work with a block of more than one thing" do
66
+ erb = @controller.send(:jelly_callback_erb, 'foo', {}, lambda{['grape','tangerine']})
67
+ JSON.parse(ERB.new(erb).result(@controller.send(:binding))).should == {
68
+ 'method' => 'foo',
69
+ 'arguments' => ['grape','tangerine']
70
+ }
71
+ end
72
+
73
+ it "should work without a block" do
74
+ erb = @controller.send(:jelly_callback_erb, 'foo', {}, nil)
75
+ JSON.parse(ERB.new(erb).result(@controller.send(:binding))).should == {
76
+ 'method' => 'foo',
77
+ 'arguments' => []
78
+ }
79
+ end
80
+ end
81
+
82
+ it "should escape html in the arguments" do
83
+ block = lambda{'<div class="foo"></div>'}
84
+ erb = @controller.send(:jelly_callback_erb, 'foo', {}, block)
85
+ JSON.parse(ERB.new(erb).result(@controller.send(:binding))).should == {
86
+ 'method' => 'foo',
87
+ 'arguments' => ['<div class="foo"></div>']
88
+ }
89
+ end
90
+
91
+ context "when the request is not an XHR" do
92
+ before do
93
+ request.stub!(:xhr?).and_return(false)
94
+ end
95
+
96
+ it "should wrap json response in a textarea tag to support File Uploads in an iframe target (see: http://malsup.com/jquery/form/#code-samples)" do
97
+ erb = @controller.send(:jelly_callback_erb, 'foo', {'bar' => 'baz'}, lambda{'grape'})
98
+ result = ERB.new(erb).result(@controller.send(:binding))
99
+ result.should =~ /^<textarea>/
100
+ result.should =~ /<\/textarea>$/
101
+ end
102
+ end
103
+
104
+ context "when the request is an XHR" do
105
+ before do
106
+ request.stub!(:xhr?).and_return(true)
107
+ end
108
+
109
+ it "should not do the textarea nonsense" do
110
+ erb = @controller.send(:jelly_callback_erb, 'foo', {'bar' => 'baz'}, lambda{'grape'})
111
+ ERB.new(erb).result(@controller.send(:binding)).should_not =~ /textarea/
112
+ end
113
+
114
+ end
115
+
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,54 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "JellyHelper" do
4
+
5
+ describe "#init_specific_javascript" do
6
+ it "should create a javascript include tag to initialize the Page object" do
7
+ stub_controller = mock(Object, :controller_path => 'my_fun_controller', :action_name => 'super_good_action')
8
+ helper.should_receive(:controller).any_number_of_times.and_return(stub_controller)
9
+ helper.should_receive(:form_authenticity_token).and_return('areallysecuretoken')
10
+ output = helper.spread_jelly
11
+ output.should include('<script type="text/javascript">')
12
+ output.should include("Jelly.activatePage('MyFunController', 'super_good_action');")
13
+ end
14
+ end
15
+
16
+ describe "#application_jelly_files" do
17
+ it "returns the javascript files in the given path" do
18
+ my_rails_root = File.join(File.dirname(__FILE__), '/../fixtures')
19
+ files = helper.application_jelly_files("foo", my_rails_root)
20
+ files.should_not be_empty
21
+ files.should =~ ['foo/pages/lions', 'foo/pages/tigers', 'foo/pages/bears']
22
+ end
23
+ end
24
+
25
+ describe "#attach_javascript_component" do
26
+
27
+ after do
28
+ #need to clear this since it's saving state between tests
29
+ assigns[:content_for_javascript] = ""
30
+ helper.clear_jelly_attached()
31
+ end
32
+
33
+ it "fails to add multiple calls to page.attach for the same component" do
34
+ helper.attach_javascript_component("MyComponent", 'arg1', 'arg2', 'arg3')
35
+ helper.attach_javascript_component("MyComponent", 'arg1', 'arg2', 'arg3')
36
+ helper.attach_javascript_component("MyComponent", 'arg1', 'arg2', 'arg5')
37
+ assigns[:content_for_javascript].should == 'page.attach(MyComponent, ["arg1","arg2","arg3"]);page.attach(MyComponent, ["arg1","arg2","arg5"]);'
38
+ end
39
+
40
+ it "adds a call to page.attach in the javascript content" do
41
+ helper.attach_javascript_component("MyComponent", 'arg1', 'arg2', 'arg3')
42
+ expected_args = ['arg1','arg2','arg3'].to_json
43
+ assigns[:content_for_javascript].should == "page.attach(MyComponent, #{expected_args});"
44
+ end
45
+
46
+ it "adds a call to page.attach in the javascript_on_ready content" do
47
+ helper.attach_javascript_component_on_ready("MyComponent", 'arg1', 'arg2', 'arg3')
48
+ expected_args = ['arg1','arg2','arg3'].to_json
49
+ assigns[:content_for_javascript_on_ready].should == "page.attach(MyComponent, #{expected_args});"
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -0,0 +1,10 @@
1
+ # Filters added to this controller apply to all controllers in the application.
2
+ # Likewise, all the methods added will be available for all controllers.
3
+
4
+ class ApplicationController < ActionController::Base
5
+ helper :all # include all helpers, all the time
6
+ protect_from_forgery # See ActionController::RequestForgeryProtection for details
7
+
8
+ # Scrub sensitive parameters from your log
9
+ # filter_parameter_logging :password
10
+ end
@@ -0,0 +1,3 @@
1
+ # Methods added to this helper will be available to all templates in the application.
2
+ module ApplicationHelper
3
+ end
@@ -0,0 +1,110 @@
1
+ # Don't change this file!
2
+ # Configure your app in config/environment.rb and config/environments/*.rb
3
+
4
+ RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
5
+
6
+ module Rails
7
+ class << self
8
+ def boot!
9
+ unless booted?
10
+ preinitialize
11
+ pick_boot.run
12
+ end
13
+ end
14
+
15
+ def booted?
16
+ defined? Rails::Initializer
17
+ end
18
+
19
+ def pick_boot
20
+ (vendor_rails? ? VendorBoot : GemBoot).new
21
+ end
22
+
23
+ def vendor_rails?
24
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
25
+ end
26
+
27
+ def preinitialize
28
+ load(preinitializer_path) if File.exist?(preinitializer_path)
29
+ end
30
+
31
+ def preinitializer_path
32
+ "#{RAILS_ROOT}/config/preinitializer.rb"
33
+ end
34
+ end
35
+
36
+ class Boot
37
+ def run
38
+ load_initializer
39
+ Rails::Initializer.run(:set_load_path)
40
+ end
41
+ end
42
+
43
+ class VendorBoot < Boot
44
+ def load_initializer
45
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
46
+ Rails::Initializer.run(:install_gem_spec_stubs)
47
+ Rails::GemDependency.add_frozen_gem_path
48
+ end
49
+ end
50
+
51
+ class GemBoot < Boot
52
+ def load_initializer
53
+ self.class.load_rubygems
54
+ load_rails_gem
55
+ require 'initializer'
56
+ end
57
+
58
+ def load_rails_gem
59
+ if version = self.class.gem_version
60
+ gem 'rails', version
61
+ else
62
+ gem 'rails'
63
+ end
64
+ rescue Gem::LoadError => load_error
65
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
66
+ exit 1
67
+ end
68
+
69
+ class << self
70
+ def rubygems_version
71
+ Gem::RubyGemsVersion rescue nil
72
+ end
73
+
74
+ def gem_version
75
+ if defined? RAILS_GEM_VERSION
76
+ RAILS_GEM_VERSION
77
+ elsif ENV.include?('RAILS_GEM_VERSION')
78
+ ENV['RAILS_GEM_VERSION']
79
+ else
80
+ parse_gem_version(read_environment_rb)
81
+ end
82
+ end
83
+
84
+ def load_rubygems
85
+ require 'rubygems'
86
+ min_version = '1.3.1'
87
+ unless rubygems_version >= min_version
88
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
89
+ exit 1
90
+ end
91
+
92
+ rescue LoadError
93
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
94
+ exit 1
95
+ end
96
+
97
+ def parse_gem_version(text)
98
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
99
+ end
100
+
101
+ private
102
+ def read_environment_rb
103
+ File.read("#{RAILS_ROOT}/config/environment.rb")
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ # All that for this:
110
+ Rails.boot!
@@ -0,0 +1,41 @@
1
+ # Be sure to restart your server when you modify this file
2
+
3
+ # Specifies gem version of Rails to use when vendor/rails is not present
4
+ RAILS_GEM_VERSION = '2.3.3' unless defined? RAILS_GEM_VERSION
5
+
6
+ # Bootstrap the Rails environment, frameworks, and default configuration
7
+ require File.join(File.dirname(__FILE__), 'boot')
8
+
9
+ Rails::Initializer.run do |config|
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Add additional load paths for your own custom dirs
15
+ # config.load_paths += %W( #{RAILS_ROOT}/extras )
16
+
17
+ # Specify gems that this application depends on and have them installed with rake gems:install
18
+ # config.gem "bj"
19
+ # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
20
+ # config.gem "sqlite3-ruby", :lib => "sqlite3"
21
+ # config.gem "aws-s3", :lib => "aws/s3"
22
+
23
+ # Only load the plugins named here, in the order given (default is alphabetical).
24
+ # :all can be used as a placeholder for all plugins not explicitly named
25
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
26
+
27
+ # Skip frameworks you're not going to use. To use Rails without a database,
28
+ # you must remove the Active Record framework.
29
+ # config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
30
+
31
+ # Activate observers that should always be running
32
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
33
+
34
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
35
+ # Run "rake -D time" for a list of tasks for finding time zone names.
36
+ config.time_zone = 'UTC'
37
+
38
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
39
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
40
+ # config.i18n.default_locale = :de
41
+ end
@@ -0,0 +1,17 @@
1
+ # Settings specified here will take precedence over those in config/environment.rb
2
+
3
+ # In the development environment your application's code is reloaded on
4
+ # every request. This slows down response time but is perfect for development
5
+ # since you don't have to restart the webserver when you make code changes.
6
+ config.cache_classes = false
7
+
8
+ # Log error messages when you accidentally call methods on nil.
9
+ config.whiny_nils = true
10
+
11
+ # Show full error reports and disable caching
12
+ config.action_controller.consider_all_requests_local = true
13
+ config.action_view.debug_rjs = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Don't care if the mailer can't send
17
+ config.action_mailer.raise_delivery_errors = false
@@ -0,0 +1,28 @@
1
+ # Settings specified here will take precedence over those in config/environment.rb
2
+
3
+ # The production environment is meant for finished, "live" apps.
4
+ # Code is not reloaded between requests
5
+ config.cache_classes = true
6
+
7
+ # Full error reports are disabled and caching is turned on
8
+ config.action_controller.consider_all_requests_local = false
9
+ config.action_controller.perform_caching = true
10
+ config.action_view.cache_template_loading = true
11
+
12
+ # See everything in the log (default is :info)
13
+ # config.log_level = :debug
14
+
15
+ # Use a different logger for distributed setups
16
+ # config.logger = SyslogLogger.new
17
+
18
+ # Use a different cache store in production
19
+ # config.cache_store = :mem_cache_store
20
+
21
+ # Enable serving of images, stylesheets, and javascripts from an asset server
22
+ # config.action_controller.asset_host = "http://assets.example.com"
23
+
24
+ # Disable delivery errors, bad email addresses will be ignored
25
+ # config.action_mailer.raise_delivery_errors = false
26
+
27
+ # Enable threaded mode
28
+ # config.threadsafe!
@@ -0,0 +1,28 @@
1
+ # Settings specified here will take precedence over those in config/environment.rb
2
+
3
+ # The test environment is used exclusively to run your application's
4
+ # test suite. You never need to work with it otherwise. Remember that
5
+ # your test database is "scratch space" for the test suite and is wiped
6
+ # and recreated between test runs. Don't rely on the data there!
7
+ config.cache_classes = true
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.action_controller.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+ config.action_view.cache_template_loading = true
16
+
17
+ # Disable request forgery protection in test environment
18
+ config.action_controller.allow_forgery_protection = false
19
+
20
+ # Tell Action Mailer not to deliver emails to the real world.
21
+ # The :test delivery method accumulates sent emails in the
22
+ # ActionMailer::Base.deliveries array.
23
+ config.action_mailer.delivery_method = :test
24
+
25
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
26
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
27
+ # like if you have constraints or database-specific column types
28
+ # config.active_record.schema_format = :sql