honkster-jelly 0.13.1 → 0.13.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Jelly. a sweet unobtrusive javascript framework for Rails
3
3
  *
4
- * version 0.13.1
4
+ * version 0.13.2
5
5
  *
6
6
  * Copyright (c) 2009 Pivotal Labs
7
7
  * Licensed under the MIT license.
@@ -4,7 +4,7 @@ module JellyHelper
4
4
  def application_jelly_files(jelly_files_path_from_javascripts = '', rails_root = RAILS_ROOT)
5
5
  rails_root = File.expand_path(rails_root)
6
6
  (
7
- Dir[File.expand_path("#{rails_root}/public/javascripts/#{jelly_files_path_from_javascripts}/components/**/*.js")] +
7
+ Dir[File.expand_path("#{rails_root}/public/javascripts/#{jelly_files_path_from_javascripts}/components/**/*.js")] +
8
8
  Dir[File.expand_path("#{rails_root}/public/javascripts/#{jelly_files_path_from_javascripts}/pages/**/*.js")]
9
9
  ).map do |path|
10
10
  path.gsub("#{rails_root}/public/javascripts/", "").gsub(/\.js$/, "")
@@ -29,17 +29,21 @@ module JellyHelper
29
29
  jelly_ops.clear
30
30
  end
31
31
 
32
- def jelly_attach(component_name, *args)
33
- op = jelly_attach_op(component_name, *args)
34
- unless jelly_ops.include? op
35
- jelly_ops << op
36
- end
32
+ def jelly_attach(component_name, *args, &block)
33
+ jelly_add_op jelly_attach_op(component_name, *args), &block
34
+ end
35
+
36
+ def jelly_notify(message_name, *args, &block)
37
+ jelly_add_op jelly_notify_op(message_name, *args), &block
37
38
  end
38
39
 
39
- def jelly_notify(message_name, *args)
40
- op = jelly_notify_op(message_name, *args)
40
+ def jelly_add_op(op, &block)
41
41
  unless jelly_ops.include? op
42
- jelly_ops << op
42
+ if block
43
+ yield(op)
44
+ else
45
+ jelly_ops << op
46
+ end
43
47
  end
44
48
  end
45
49
 
@@ -2,24 +2,28 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
2
2
 
3
3
  describe JellyHelper, :type => :helper do
4
4
 
5
- def jelly_ops(html)
5
+ after do
6
+ helper.jelly_clear_ops()
7
+ end
8
+
9
+ def parse_jelly_ops(html)
6
10
  JSON.parse(Regexp.new('Jelly\.run\.apply\(Jelly, (.*)\);').match(html)[1])
7
11
  end
8
12
 
9
13
  describe "#spread_jelly" do
10
14
  before do
11
15
  stub_controller = mock! do |controller|
12
- controller.controller_path {'my_fun_controller'}
13
- controller.action_name {'super_good_action'}
16
+ controller.controller_path { 'my_fun_controller' }
17
+ controller.action_name { 'super_good_action' }
14
18
  end
15
- stub(helper).controller {stub_controller}
19
+ stub(helper).controller { stub_controller }
16
20
  end
17
21
 
18
22
  it "should create a javascript include tag that attaches the Jelly.Location and Jelly.Page components" do
19
23
  output = helper.spread_jelly
20
24
  output.should include('<script type="text/javascript">')
21
25
  doc = Nokogiri::HTML(output)
22
- ops = jelly_ops(doc.at("script").inner_html)
26
+ ops = parse_jelly_ops(doc.at("script").inner_html)
23
27
  ops.should include(["attach", "Jelly.Location"])
24
28
  ops.should include(["attach", "Jelly.Page", 'MyFunController', 'super_good_action'])
25
29
  end
@@ -46,40 +50,144 @@ describe JellyHelper, :type => :helper do
46
50
  end
47
51
 
48
52
  describe "#jelly_attach" do
49
- before do
50
- def helper.form_authenticity_token
51
- "12345"
53
+ context "when not given a block" do
54
+ it "fails to add multiple calls to Jelly.attach for the same component" do
55
+ helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3')
56
+ helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3')
57
+ helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg5')
58
+ helper.jelly_ops.should == [
59
+ ["attach", "MyComponent", 'arg1', 'arg2', 'arg3'],
60
+ ["attach", "MyComponent", 'arg1', 'arg2', 'arg5'],
61
+ ]
52
62
  end
53
- end
54
63
 
55
- after do
56
- helper.jelly_clear_ops()
64
+ it "adds an attach op to jelly_ops" do
65
+ helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3')
66
+ helper.jelly_ops.should(
67
+ include(
68
+ ["attach", "MyComponent", 'arg1', 'arg2', 'arg3']
69
+ )
70
+ )
71
+
72
+ html = helper.spread_jelly
73
+ doc = Nokogiri::HTML(html)
74
+ document_ready_tag = doc.at("script")
75
+ document_ready_part = document_ready_tag.inner_html.split("\n")[2]
76
+ arguments = parse_jelly_ops(document_ready_part)
77
+ arguments.should include(["attach", "MyComponent", 'arg1', 'arg2', 'arg3'])
78
+ end
57
79
  end
58
80
 
59
- it "fails to add multiple calls to Jelly.attach for the same component" do
60
- helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3')
61
- helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3')
62
- helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg5')
63
- helper.instance_variable_get(:@jelly_ops).should == [
64
- ["attach", "MyComponent", 'arg1', 'arg2', 'arg3'],
65
- ["attach", "MyComponent", 'arg1', 'arg2', 'arg5'],
66
- ]
81
+ context "when given a block" do
82
+ it "fails to add multiple calls to Jelly.attach for the same component" do
83
+ helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3') do |op|
84
+ helper.jelly_ops.unshift op
85
+ end
86
+ helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3') do |op|
87
+ helper.jelly_ops.unshift op
88
+ end
89
+ helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg5') do |op|
90
+ helper.jelly_ops.unshift op
91
+ end
92
+ helper.jelly_ops.should == [
93
+ ["attach", "MyComponent", 'arg1', 'arg2', 'arg5'],
94
+ ["attach", "MyComponent", 'arg1', 'arg2', 'arg3'],
95
+ ]
96
+ end
97
+
98
+ it "passes the attach op into the block so it can be added to jelly_ops" do
99
+ helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3')
100
+ helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg5') do |op|
101
+ helper.jelly_ops.unshift op
102
+ end
103
+ helper.jelly_ops.should(
104
+ include(
105
+ ["attach", "MyComponent", 'arg1', 'arg2', 'arg3']
106
+ )
107
+ )
108
+
109
+ html = helper.spread_jelly
110
+ doc = Nokogiri::HTML(html)
111
+ document_ready_tag = doc.at("script")
112
+ document_ready_part = document_ready_tag.inner_html.split("\n")[2]
113
+ arguments = parse_jelly_ops(document_ready_part)
114
+ helper.jelly_ops[0..1].should == [
115
+ ["attach", "MyComponent", 'arg1', 'arg2', 'arg5'],
116
+ ["attach", "MyComponent", 'arg1', 'arg2', 'arg3'],
117
+ ]
118
+ end
67
119
  end
68
120
 
69
- it "adds a call to Jelly.attach" do
70
- helper.jelly_attach("MyComponent", 'arg1', 'arg2', 'arg3')
71
- helper.instance_variable_get(:@jelly_ops).should include(
72
- ["attach", "MyComponent", 'arg1', 'arg2', 'arg3']
73
- )
74
-
75
- html = helper.spread_jelly
76
- doc = Nokogiri::HTML(html)
77
- document_ready_tag = doc.at("script")
78
- document_ready_part = document_ready_tag.inner_html.split("\n")[2]
79
- arguments = jelly_ops(document_ready_part)
80
- arguments.should include(["attach", "MyComponent", 'arg1', 'arg2', 'arg3'])
121
+ end
122
+
123
+ describe "#jelly_notify" do
124
+ context "when not given a block" do
125
+ it "fails to add multiple calls to Jelly.attach for the same component" do
126
+ helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg3')
127
+ helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg3')
128
+ helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg5')
129
+ helper.jelly_ops.should == [
130
+ ["notify", "myMessage", 'arg1', 'arg2', 'arg3'],
131
+ ["notify", "myMessage", 'arg1', 'arg2', 'arg5'],
132
+ ]
133
+ end
134
+
135
+ it "adds an attach op to jelly_ops" do
136
+ helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg3')
137
+ helper.jelly_ops.should(
138
+ include(
139
+ ["notify", "myMessage", 'arg1', 'arg2', 'arg3']
140
+ )
141
+ )
142
+
143
+ html = helper.spread_jelly
144
+ doc = Nokogiri::HTML(html)
145
+ document_ready_tag = doc.at("script")
146
+ document_ready_part = document_ready_tag.inner_html.split("\n")[2]
147
+ arguments = parse_jelly_ops(document_ready_part)
148
+ arguments.should include(["notify", "myMessage", 'arg1', 'arg2', 'arg3'])
149
+ end
81
150
  end
82
151
 
152
+ context "when given a block" do
153
+ it "fails to add multiple calls to Jelly.attach for the same component" do
154
+ helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg3') do |op|
155
+ helper.jelly_ops.unshift op
156
+ end
157
+ helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg3') do |op|
158
+ helper.jelly_ops.unshift op
159
+ end
160
+ helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg5') do |op|
161
+ helper.jelly_ops.unshift op
162
+ end
163
+ helper.jelly_ops.should == [
164
+ ["notify", "myMessage", 'arg1', 'arg2', 'arg5'],
165
+ ["notify", "myMessage", 'arg1', 'arg2', 'arg3'],
166
+ ]
167
+ end
168
+
169
+ it "passes the attach op into the block so it can be added to jelly_ops" do
170
+ helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg3')
171
+ helper.jelly_notify("myMessage", 'arg1', 'arg2', 'arg5') do |op|
172
+ helper.jelly_ops.unshift op
173
+ end
174
+ helper.jelly_ops.should(
175
+ include(
176
+ ["notify", "myMessage", 'arg1', 'arg2', 'arg3']
177
+ )
178
+ )
179
+
180
+ html = helper.spread_jelly
181
+ doc = Nokogiri::HTML(html)
182
+ document_ready_tag = doc.at("script")
183
+ document_ready_part = document_ready_tag.inner_html.split("\n")[2]
184
+ arguments = parse_jelly_ops(document_ready_part)
185
+ helper.jelly_ops[0..1].should == [
186
+ ["notify", "myMessage", 'arg1', 'arg2', 'arg5'],
187
+ ["notify", "myMessage", 'arg1', 'arg2', 'arg3'],
188
+ ]
189
+ end
190
+ end
83
191
  end
84
192
 
85
193
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: honkster-jelly
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.13.1
5
+ version: 0.13.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Pivotal Labs, Inc