honkster-jelly 0.10.0 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- data/generators/jelly/templates/javascripts/jelly.js +26 -12
- data/lib/jelly/common.rb +4 -0
- data/lib/jelly/jelly_helper.rb +3 -11
- data/spec/javascript/jelly_spec.js +123 -0
- data/spec/jelly/jelly_helper_spec.rb +8 -9
- metadata +1 -1
@@ -1,7 +1,7 @@
|
|
1
1
|
/**
|
2
2
|
* Jelly. a sweet unobtrusive javascript framework for Rails
|
3
3
|
*
|
4
|
-
* version 0.
|
4
|
+
* version 0.11.0
|
5
5
|
*
|
6
6
|
* Copyright (c) 2009 Pivotal Labs
|
7
7
|
* Licensed under the MIT license.
|
@@ -42,22 +42,36 @@
|
|
42
42
|
if (this === Jelly) {
|
43
43
|
return Jelly.Observers.attach.apply(this.observers, arguments);
|
44
44
|
}
|
45
|
+
var self = Jelly.Observers;
|
45
46
|
for (var i = 0; i < arguments.length; i++) {
|
46
47
|
var definitionOrComponent = arguments[i];
|
47
48
|
if (definitionOrComponent.component) {
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
49
|
+
self.attachFromDefinition.call(
|
50
|
+
this,
|
51
|
+
self.evaluateComponent(definitionOrComponent.component),
|
52
|
+
definitionOrComponent.arguments
|
53
|
+
);
|
54
|
+
} else if (Object.prototype.toString.call(definitionOrComponent) === "[object Array]") {
|
55
|
+
self.attachFromDefinition.call(
|
56
|
+
this,
|
57
|
+
self.evaluateComponent(definitionOrComponent[0]),
|
58
|
+
definitionOrComponent.slice(1)
|
59
|
+
);
|
60
|
+
} else {
|
61
|
+
self.pushIfObserver.call(this, Jelly.Observers.evaluateComponent(definitionOrComponent));
|
62
|
+
}
|
63
|
+
}
|
64
|
+
},
|
65
|
+
|
66
|
+
attachFromDefinition:function (component, args) {
|
67
|
+
if (component.init) {
|
68
|
+
var initReturnValue = component.init.apply(component, args);
|
69
|
+
if (initReturnValue === false || initReturnValue === null) {
|
58
70
|
} else {
|
59
|
-
Jelly.Observers.pushIfObserver.call(this,
|
71
|
+
Jelly.Observers.pushIfObserver.call(this, initReturnValue || component);
|
60
72
|
}
|
73
|
+
} else {
|
74
|
+
Jelly.Observers.pushIfObserver.call(this, component);
|
61
75
|
}
|
62
76
|
},
|
63
77
|
|
data/lib/jelly/common.rb
CHANGED
data/lib/jelly/jelly_helper.rb
CHANGED
@@ -14,18 +14,10 @@ module JellyHelper
|
|
14
14
|
def spread_jelly
|
15
15
|
attach_javascript_component("Jelly.Location")
|
16
16
|
attach_javascript_component("Jelly.Page", controller.controller_path.camelcase, controller.action_name)
|
17
|
-
|
18
|
-
#{window_token_javascript_tag}
|
19
|
-
#{attach_javascript_component_javascript_tag(jelly_attachments)}
|
20
|
-
HTML
|
17
|
+
attach_javascript_component_javascript_tag(jelly_attachments)
|
21
18
|
end
|
22
19
|
|
23
|
-
def
|
24
|
-
javascript_tag("window._token = '#{form_authenticity_token}';")
|
25
|
-
end
|
26
|
-
|
27
|
-
def attach_javascript_component_javascript_tag(*components)
|
28
|
-
components = [components].flatten
|
20
|
+
def attach_javascript_component_javascript_tag(components)
|
29
21
|
javascript_tag <<-JS
|
30
22
|
jQuery(document).ready(function() {
|
31
23
|
Jelly.attach.apply(Jelly, #{components.to_json});
|
@@ -38,7 +30,7 @@ module JellyHelper
|
|
38
30
|
end
|
39
31
|
|
40
32
|
def attach_javascript_component(component_name, *args)
|
41
|
-
key =
|
33
|
+
key = jelly_attachment_array(component_name, *args)
|
42
34
|
unless jelly_attachments.include? key
|
43
35
|
jelly_attachments << key
|
44
36
|
end
|
@@ -155,6 +155,129 @@ describe("Jelly", function() {
|
|
155
155
|
});
|
156
156
|
});
|
157
157
|
|
158
|
+
describe("when the argument is an array", function() {
|
159
|
+
describe("when the component does not respond to init", function() {
|
160
|
+
describe("when the component is referenced as a String", function() {
|
161
|
+
beforeEach(function() {
|
162
|
+
window.MyComponent = {
|
163
|
+
};
|
164
|
+
});
|
165
|
+
|
166
|
+
afterEach(function() {
|
167
|
+
delete window.MyComponent;
|
168
|
+
});
|
169
|
+
|
170
|
+
it("attaches the component to Jelly.observers", function() {
|
171
|
+
Jelly.attach(["MyComponent"]);
|
172
|
+
expect(Jelly.observers).toContain(MyComponent);
|
173
|
+
});
|
174
|
+
});
|
175
|
+
|
176
|
+
describe("when the component is referenced as itself", function() {
|
177
|
+
it("attaches the component to Jelly.observers", function() {
|
178
|
+
var component = {};
|
179
|
+
Jelly.attach([component]);
|
180
|
+
expect(Jelly.observers).toContain(component);
|
181
|
+
});
|
182
|
+
});
|
183
|
+
});
|
184
|
+
|
185
|
+
describe("when component responds to init", function() {
|
186
|
+
describe("when the component's init method returns undefined", function() {
|
187
|
+
describe("when the component is referenced as a String", function() {
|
188
|
+
beforeEach(function() {
|
189
|
+
window.MyComponent = {
|
190
|
+
init: function() {
|
191
|
+
}
|
192
|
+
};
|
193
|
+
});
|
194
|
+
|
195
|
+
afterEach(function() {
|
196
|
+
delete window.MyComponent;
|
197
|
+
});
|
198
|
+
|
199
|
+
it("calls the init method on the component and attaches the component to Jelly.observers", function() {
|
200
|
+
spyOn(MyComponent, 'init');
|
201
|
+
Jelly.attach([MyComponent, 1, 2]);
|
202
|
+
expect(MyComponent.init).wasCalledWith(1, 2);
|
203
|
+
expect(Jelly.observers).toContain(MyComponent);
|
204
|
+
});
|
205
|
+
});
|
206
|
+
|
207
|
+
describe("when the component is referenced as itself", function() {
|
208
|
+
var component;
|
209
|
+
beforeEach(function() {
|
210
|
+
component = {
|
211
|
+
init: function() {
|
212
|
+
}
|
213
|
+
};
|
214
|
+
});
|
215
|
+
|
216
|
+
it("calls the init method on the component and attaches the component to Jelly.observers", function() {
|
217
|
+
spyOn(component, 'init');
|
218
|
+
Jelly.attach([component, 1, 2]);
|
219
|
+
expect(component.init).wasCalledWith(1, 2);
|
220
|
+
expect(Jelly.observers).toContain(component);
|
221
|
+
});
|
222
|
+
});
|
223
|
+
});
|
224
|
+
|
225
|
+
describe("when the component's init method returns false", function() {
|
226
|
+
var component;
|
227
|
+
beforeEach(function() {
|
228
|
+
component = {
|
229
|
+
init: function() {
|
230
|
+
component.initCalled = true;
|
231
|
+
return false;
|
232
|
+
}
|
233
|
+
};
|
234
|
+
});
|
235
|
+
|
236
|
+
it("calls the init method on the component and does not attaches an observer to Jelly.observers", function() {
|
237
|
+
var originalObserversLength = Jelly.observers.length;
|
238
|
+
Jelly.attach([component, 1, 2]);
|
239
|
+
expect(component.initCalled).toBeTruthy();
|
240
|
+
expect(Jelly.observers.length).toEqual(originalObserversLength);
|
241
|
+
expect(Jelly.observers).toNotContain(component);
|
242
|
+
});
|
243
|
+
});
|
244
|
+
|
245
|
+
describe("when the component's init method returns null", function() {
|
246
|
+
var component;
|
247
|
+
beforeEach(function() {
|
248
|
+
component = {
|
249
|
+
init: function() {
|
250
|
+
component.initCalled = true;
|
251
|
+
return null;
|
252
|
+
}
|
253
|
+
};
|
254
|
+
});
|
255
|
+
|
256
|
+
it("calls the init method on the component and does not attaches an observer to Jelly.observers", function() {
|
257
|
+
var originalObserversLength = Jelly.observers.length;
|
258
|
+
Jelly.attach([component, 1, 2]);
|
259
|
+
expect(component.initCalled).toBeTruthy();
|
260
|
+
expect(Jelly.observers.length).toEqual(originalObserversLength);
|
261
|
+
expect(Jelly.observers).toNotContain(component);
|
262
|
+
});
|
263
|
+
});
|
264
|
+
|
265
|
+
describe("when the component's init method returns an object", function() {
|
266
|
+
it("attaches the returned object (instead of the component) to Jelly.observers", function() {
|
267
|
+
var observer = new Object();
|
268
|
+
var component = {
|
269
|
+
init: function() {
|
270
|
+
return observer;
|
271
|
+
}
|
272
|
+
};
|
273
|
+
Jelly.attach([component, 1, 2]);
|
274
|
+
expect(Jelly.observers).toContain(observer);
|
275
|
+
expect(Jelly.observers).toNotContain(component);
|
276
|
+
});
|
277
|
+
});
|
278
|
+
});
|
279
|
+
});
|
280
|
+
|
158
281
|
describe("when the argument does not contain a 'component' key", function() {
|
159
282
|
describe("when the component is referenced as a String", function() {
|
160
283
|
beforeEach(function() {
|
@@ -13,16 +13,15 @@ describe JellyHelper, :type => :helper do
|
|
13
13
|
controller.action_name {'super_good_action'}
|
14
14
|
end
|
15
15
|
stub(helper).controller {stub_controller}
|
16
|
-
mock(helper).form_authenticity_token {'areallysecuretoken'}
|
17
16
|
end
|
18
17
|
|
19
18
|
it "should create a javascript include tag that attaches the Jelly.Location and Jelly.Page components" do
|
20
19
|
output = helper.spread_jelly
|
21
20
|
output.should include('<script type="text/javascript">')
|
22
21
|
doc = Nokogiri::HTML(output)
|
23
|
-
argument = jelly_attach_arguments(doc.
|
24
|
-
argument.should include(
|
25
|
-
argument.should include(
|
22
|
+
argument = jelly_attach_arguments(doc.at("script").inner_html)
|
23
|
+
argument.should include(["Jelly.Location"])
|
24
|
+
argument.should include(["Jelly.Page", 'MyFunController', 'super_good_action'])
|
26
25
|
end
|
27
26
|
end
|
28
27
|
|
@@ -62,8 +61,8 @@ describe JellyHelper, :type => :helper do
|
|
62
61
|
helper.attach_javascript_component("MyComponent", 'arg1', 'arg2', 'arg3')
|
63
62
|
helper.attach_javascript_component("MyComponent", 'arg1', 'arg2', 'arg5')
|
64
63
|
helper.instance_variable_get(:@jelly_attachments).should == [
|
65
|
-
|
66
|
-
|
64
|
+
["MyComponent", 'arg1', 'arg2', 'arg3'],
|
65
|
+
["MyComponent", 'arg1', 'arg2', 'arg5'],
|
67
66
|
]
|
68
67
|
end
|
69
68
|
|
@@ -71,16 +70,16 @@ describe JellyHelper, :type => :helper do
|
|
71
70
|
helper.attach_javascript_component("MyComponent", 'arg1', 'arg2', 'arg3')
|
72
71
|
expected_args = ['arg1','arg2','arg3'].to_json
|
73
72
|
helper.instance_variable_get(:@jelly_attachments).should == [
|
74
|
-
|
73
|
+
["MyComponent", 'arg1', 'arg2', 'arg3']
|
75
74
|
]
|
76
75
|
|
77
76
|
html = helper.spread_jelly
|
78
77
|
doc = Nokogiri::HTML(html)
|
79
|
-
document_ready_tag = doc.
|
78
|
+
document_ready_tag = doc.at("script")
|
80
79
|
document_ready_tag.inner_html.should include("jQuery(document).ready(function() {")
|
81
80
|
document_ready_part = document_ready_tag.inner_html.split("\n")[3]
|
82
81
|
arguments = jelly_attach_arguments(document_ready_part)
|
83
|
-
arguments.should include(
|
82
|
+
arguments.should include(["MyComponent", 'arg1', 'arg2', 'arg3'])
|
84
83
|
end
|
85
84
|
|
86
85
|
end
|