honkster-jelly 0.7.6 → 0.7.7

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/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 7
3
- :patch: 6
3
+ :patch: 7
4
4
  :major: 0
@@ -2,7 +2,7 @@
2
2
  * Jelly. a sweet unobtrusive javascript framework
3
3
  * for jQuery and Rails
4
4
  *
5
- * version 0.7.6
5
+ * version 0.7.7
6
6
  *
7
7
  * Copyright (c) 2009 Pivotal Labs
8
8
  * Licensed under the MIT license.
data/jelly.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{jelly}
8
- s.version = "0.7.5"
8
+ s.version = "0.7.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Pivotal Labs, Inc"]
@@ -3,22 +3,40 @@ module JellyController
3
3
  include Jelly::Common
4
4
 
5
5
  def jelly_callback(callback_base_name = @action_name, options = {}, &block)
6
+ options[:format] ||= if request.xhr?
7
+ :json
8
+ elsif params[:callback]
9
+ :jsonp
10
+ else
11
+ :iframe
12
+ end
6
13
  render :inline => jelly_callback_erb("on_#{callback_base_name}", options, block)
7
14
  end
8
15
 
9
16
  def jelly_callback_erb(callback_name, options, block)
17
+ options[:format] ||= :json
10
18
  @callback_name = callback_name
11
19
  @options = options
12
20
  @block = block
13
- erb = <<-ERB
21
+ case options[:format].to_sym
22
+ when :iframe
23
+ "<textarea>#{jelly_callback_erb_template}</textarea>"
24
+ when :jsonp
25
+ @jsonp_callback = params[:callback]
26
+ jelly_callback_erb_template
27
+ else
28
+ jelly_callback_erb_template
29
+ end
30
+ end
31
+
32
+ def jelly_callback_erb_template
33
+ <<-ERB
14
34
  <%= begin
15
35
  args = @block ? instance_eval(&@block) : []
16
36
  args = [args] unless args.is_a?(Array)
17
- jelly_callback_hash(@callback_name, *args).reverse_merge(@options).to_json
37
+ json = {"method" => @callback_name, "arguments" => args}.reverse_merge(@options).to_json
38
+ @jsonp_callback ? "\#{@jsonp_callback}(\#{json});" : json
18
39
  end %>
19
40
  ERB
20
- request.xhr? ? erb : "<textarea>#{erb}</textarea>"
21
41
  end
22
-
23
-
24
42
  end
@@ -3,25 +3,78 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
3
3
  describe ApplicationController do
4
4
 
5
5
  describe "#jelly_callback" do
6
+ attr_reader :response
6
7
  before do
7
- @controller.stub!(:render)
8
+ @response = Struct.new(:body).new
9
+ stub(@controller).render do |params|
10
+ response.body = ERB.new(params[:inline]).result(@controller.send(:binding))
11
+ end
8
12
  end
9
13
 
10
14
  it "have the method included" do
11
15
  @controller.respond_to?(:jelly_callback).should be_true
12
16
  end
13
17
 
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)
18
+ context "when the request is XHR" do
19
+ before do
20
+ stub(request).xhr? {true}
21
+ end
22
+
23
+ it "responds with a json hash" do
24
+ @controller.send(:jelly_callback, 'foo', {'bar' => 'baz'}) do
25
+ "grape"
26
+ end
27
+ callback = JSON.parse(response.body)
28
+ callback["method"].should == "on_foo"
29
+ callback["arguments"].should == ["grape"]
30
+ callback["bar"].should == "baz"
31
+ end
32
+
33
+ end
34
+
35
+ context "when the request is not XHR" do
36
+ before do
37
+ stub(request).xhr? {false}
38
+ end
39
+
40
+ context "when there is a callback param" do
41
+ before do
42
+ @controller.params[:callback] = "Jelly.notifyObservers"
43
+ end
44
+
45
+ it "responds with a call to the given callback method with the json as an argument" do
46
+ @controller.send(:jelly_callback, 'foo', {'bar' => 'baz'}) do
47
+ "grape"
48
+ end
49
+ json = Regexp.new('Jelly\.notifyObservers\((.*)\);').match(response.body)[1]
50
+ callback = JSON.parse(json)
51
+ callback["method"].should == "on_foo"
52
+ callback["arguments"].should == ["grape"]
53
+ callback["bar"].should == "baz"
54
+ end
55
+ end
56
+
57
+ context "when there is not a callback param" do
58
+ it "wraps the json response in a textarea tag to support File Uploads in an iframe target (see: http://malsup.com/jquery/form/#code-samples)" do
59
+ @controller.send(:jelly_callback, 'foo', {'bar' => 'baz'}) do
60
+ "grape"
61
+ end
62
+ body = response.body
63
+ body.should =~ /^<textarea>/
64
+ body.should =~ /<\/textarea>$/
65
+ doc = Nokogiri::HTML(body)
66
+
67
+ callback = JSON.parse(doc.at("textarea").inner_html)
68
+ callback["method"].should == "on_foo"
69
+ callback["arguments"].should == ["grape"]
70
+ callback["bar"].should == "baz"
71
+ end
72
+ end
20
73
  end
21
74
 
22
75
  describe "#jelly_callback_erb" do
23
76
  before do
24
- request.stub!(:xhr?).and_return(true)
77
+ stub(request).xhr? {true}
25
78
  end
26
79
 
27
80
  context "with options" do
@@ -30,7 +83,8 @@ describe ApplicationController do
30
83
  JSON.parse(ERB.new(erb).result(@controller.send(:binding))).should == {
31
84
  'method' => 'foo',
32
85
  'arguments' => ['grape'],
33
- 'bar' => 'baz'
86
+ 'bar' => 'baz',
87
+ 'format' => 'json'
34
88
  }
35
89
  end
36
90
 
@@ -39,7 +93,8 @@ describe ApplicationController do
39
93
  JSON.parse(ERB.new(erb).result(@controller.send(:binding))).should == {
40
94
  'method' => 'foo',
41
95
  'arguments' => [],
42
- 'bar' => 'baz'
96
+ 'bar' => 'baz',
97
+ 'format' => 'json'
43
98
  }
44
99
  end
45
100
 
@@ -48,7 +103,8 @@ describe ApplicationController do
48
103
  JSON.parse(ERB.new(erb).result(@controller.send(:binding))).should == {
49
104
  'method' => 'foo',
50
105
  'arguments' => [],
51
- 'bar' => 'baz'
106
+ 'bar' => 'baz',
107
+ 'format' => 'json'
52
108
  }
53
109
  end
54
110
  end
@@ -58,7 +114,8 @@ describe ApplicationController do
58
114
  erb = @controller.send(:jelly_callback_erb, 'foo', {}, lambda{'grape'})
59
115
  JSON.parse(ERB.new(erb).result(@controller.send(:binding))).should == {
60
116
  'method' => 'foo',
61
- 'arguments' => ['grape']
117
+ 'arguments' => ['grape'],
118
+ 'format' => 'json'
62
119
  }
63
120
  end
64
121
 
@@ -66,7 +123,8 @@ describe ApplicationController do
66
123
  erb = @controller.send(:jelly_callback_erb, 'foo', {}, lambda{['grape','tangerine']})
67
124
  JSON.parse(ERB.new(erb).result(@controller.send(:binding))).should == {
68
125
  'method' => 'foo',
69
- 'arguments' => ['grape','tangerine']
126
+ 'arguments' => ['grape','tangerine'],
127
+ 'format' => 'json'
70
128
  }
71
129
  end
72
130
 
@@ -74,7 +132,8 @@ describe ApplicationController do
74
132
  erb = @controller.send(:jelly_callback_erb, 'foo', {}, nil)
75
133
  JSON.parse(ERB.new(erb).result(@controller.send(:binding))).should == {
76
134
  'method' => 'foo',
77
- 'arguments' => []
135
+ 'arguments' => [],
136
+ 'format' => 'json'
78
137
  }
79
138
  end
80
139
  end
@@ -84,35 +143,10 @@ describe ApplicationController do
84
143
  erb = @controller.send(:jelly_callback_erb, 'foo', {}, block)
85
144
  JSON.parse(ERB.new(erb).result(@controller.send(:binding))).should == {
86
145
  'method' => 'foo',
87
- 'arguments' => ['<div class="foo"></div>']
146
+ 'arguments' => ['<div class="foo"></div>'],
147
+ 'format' => 'json'
88
148
  }
89
149
  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
150
  end
117
151
  end
118
152
  end
@@ -8,9 +8,12 @@ describe JellyHelper do
8
8
 
9
9
  describe "#spread_jelly" do
10
10
  before do
11
- stub_controller = mock(Object, :controller_path => 'my_fun_controller', :action_name => 'super_good_action')
12
- helper.should_receive(:controller).any_number_of_times.and_return(stub_controller)
13
- helper.should_receive(:form_authenticity_token).and_return('areallysecuretoken')
11
+ stub_controller = mock! do |controller|
12
+ controller.controller_path {'my_fun_controller'}
13
+ controller.action_name {'super_good_action'}
14
+ end
15
+ stub(helper).controller {stub_controller}
16
+ mock(helper).form_authenticity_token {'areallysecuretoken'}
14
17
  end
15
18
 
16
19
  it "should create a javascript include tag that attaches the Jelly.Location and Jelly.Page components" do
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,7 @@ require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
6
6
  require 'rubygems'
7
7
  gem "test-unit"
8
8
  require 'test/unit'
9
+ require 'rr'
9
10
 
10
11
  class Test::Unit::TestCase
11
12
  class << self
@@ -27,6 +28,7 @@ $LOAD_PATH.unshift(File.expand_path("#{File.dirname(__FILE__)}/../lib"))
27
28
  require "jelly"
28
29
 
29
30
  Spec::Runner.configure do |configuration|
31
+ configuration.mock_with :rr
30
32
  end
31
33
 
32
34
  class Spec::ExampleGroup
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: honkster-jelly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.6
4
+ version: 0.7.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pivotal Labs, Inc